For any queries you can reach us at infovistarindia@gmail.com / WhatsApp us: +919158876092

NumPy Functions

NumPy Functions

  1. numpy.type()

    Syntax:

    type(numpy.ndarray)

    This function is used to return the type of the parameter passed. In case of the NumPy array, it will return numpy.ndarray.

    import numpy as np  
    a = np.array([[1,2,3],[4,5,6]])  
    print(type(a)) 

    The above code will return numpy.ndarray

  2. numpy.zeros()

    Syntax:

    numpy.zeros((rows,columns), dtype)

    This function will create a numpy array of the given the dimensions with each item being zero. If no dtype is specified, default dtype is taken.

    import numpy as np  
    np.zeros((3,3))  
    print(a)

    The above code will result in a 3x3 numpy array with each item being zero.

  3. numpy.ones()

    Syntax:

    numpy.ones((rows,columns), dtype)

    The above function will create a numpy array of the given dimensions. If no dtype is specified with each item being one, default dtype is taken.

    import numpy as np    
    np.ones((3,3))    
    print(a)

    The above code will return a 3x3 numpy array with each item being one.

  4. numpy.empty()

    Syntax

    numpy.empty((rows,columns)) 

    The above function creates an array whose initial content is random and depends on the state of the memory.

    import numpy as np      
    np.empty((3,3))      
    print(a) 

    The above code will result in a 3x3 numpy array with each item is random.

  5. numpy.arange()

    Syntax:

    numpy.arange(start, stop, step)

    This function is used to make a numpy array with items in the range between the start and stop value with the difference of step value.

    import numpy as np  
    a=np.arange(5,25,4)  
    print(a)  

    The output of the above code will be [ 5 9 13 17 21 ]

  6. numpy.linspace()

    Syntax:

    numpy.linspace(start, stop, num_of_elements)

    This function is used to make a numpy array with items in the range between the start and stop value and num_of_elements as the size of the numpy array. The default dtype of numpy array is float64. All the items will be spanned over the logarithmic scale i.e the resulting elements are the log of the corresponding item.

    import numpy as np  
    a=np.linspace(5,25,5)  
    print(a) 

    The output of the above code will be [ 5 10 15 20 25 ]

  7. numpy.logspace()

    Syntax:

    numpy.logspace(start, stop, num_of_elements)

    The above function is used to make a numpy array with elements in the range between the start and stop value and num_of_elements as the size of the numpy array. The default dtype of numpy array is float64. All the elements will be spanned over the logarithmic scale i.e the resulting elements are the log of the corresponding element.

    import numpy as np  
    a=np.logspace(5,25,5)  
    print(a)

    The output of the above code will be [1.e+05 1.e+10 1.e+15 1.e+20 1.e+25]

  8. numpy.sin()

    Syntax:

    numpy.sin(numpy.ndarray)

    This function will return the sin of the provided parameter.

    import numpy as np  
    a=np.logspace(5,25,2)  
    print(np.sin(a)) 

    The output of the above code will be [ 0.0357488 -0.3052578]

    Similarly, there are cos() ,tan(), etc.

  9. numpy.reshape()

    Syntax:

    numpy.resahpe(dimensions)

    This function is used to change the dimension of a numpy array. The number of arguments in the reshape determines the dimensions of the numpy array.

    import numpy as np  
    a=np.arange(9).reshape(3,3)  
    print(a)

    The output of the above code will be a 2D array with 3x3 dimensions

  10. numpy.random.random()

    Syntax:

    numpy.random.random( (rows, column) )

    This function is used to return a numpy ndarray with the given dimensions and each item of ndarray being randomly generated.

    a = np.random.random((2,2))  

    The above code will return a 2x2 ndarray

  11. numpy.exp()

    Syntax:

    numpy.exp(numpy.ndarray)

    This function returns a ndarray with exponential of every item

    b = np.exp([10]) 

    The above code returns the value 22026.4657948

  12. numpy.sqrt()

    Syntax:

    numpy.sqrt(numpy.ndarray)

    This function returns a ndarray with ex of every item

    b = np.sqrt([16])

    The above code returns the value 4