It is a python function is used to return the type of the parameter passed. In the case of numpy array, it will return numpy.ndarray
2. numpy.zeros()
numpy.zeros((rows,columns), dtype)
The above function will create a NumPy array of the given dimensions with each element being zero. If no dtype is defined, 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 element 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 defined with each element being one, the default dtype is taken.
- import numpy as np
- np.ones((3,3))
- print(a)
The above code will result in a 3x3 numpy array with each element 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 element being random.
5. numpy.arange()
Syntax
numpy.arange(start, stop, step)
The above function is used to make a NumPy array with elements 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)
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
- 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 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)
The above code will return the sin of the given 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)
The above function is used to change the dimension of a NumPy array. The number of arguments in the reshape decides 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
The above function is used to return a numpy ndarray with the given dimensions and each element of ndarray being randomly generated.
NumPy Array Basic Operations
- a = np.array( [[1,1], [0,1]])
- b = np.array( [[2,0],[3,4]])
1. The below code will return the elementwise product of both the arrays
2. The below code will return the matrix product of both the arrays
Conclusion
In this article, we studied NumPy, installing NumPy, NumPy array, numpy array attributes, numpy functions, numpy basic operations, and numpy array basic operations. Hope you were able to understand each and everything. For any doubts, please comment on your query.
In the next article, we will learn about pandas, the next library in the series.
Congratulations!!! you have climbed your next step in becoming a successful ML engineer.
Next Article In this Series >>
Pandas