Welcome back friends, we are back with a new tutorial of NumPy, i.e. NumPy Array in the Python Tutorials series. What is a Numpy array? how it’s different from list arrays of python and also its implementation with examples.
Lets get started now…
What is NumPy Array?
NumPy arrays are the main way we will use Numpy throughout the course. Numpy arrays essentially come in two flavors: vectors and matrices. Vectors are strictly 1-d arrays and matrices are 2-d (but you should note a matrix can still have only one row or one column).
Let’s begin our introduction by exploring how to create NumPy arrays.
How to check NumPy Version?
If you want to know the version string is stored under __version__ attribute then follow below mentioned example:
import numpy as np
print(np.__version__)
Output:
How to create NumPy arrays in Python?
The array object is likewise a list array it is denoted by ndarrays. we can pass lists, tuples in ndarrays.
We can create an ndarray by using the array() function.
import numpy as np
# list is passing in array function.
nparr = np.array([1, 2, 3, 4, 5,6])
print(nparr)
# for knowing the class or type of the object
print(type(nparr))
Output:
[1 2 3 4 5 6]
<class 'numpy.ndarray'>
Dimensions in Arrays
In Python, there are multi-dimension arrays for instance 0-D array, 1-D array, etc.
0 – Dimension Array:
You can consider this by its name 0 dimension array or scaler array.
Example:
import numpy as np
nparr = np.array(52)
print(nparr)
Output:
52
1 – Dimension Array:
1-D array also called unidimensional array.
import numpy as np
nparr = np.array([4,7,5,9])
print(nparr)
Output:[4,7,5,9]
2 – Dimension Array:
import numpy as np
nparr = np.array([[1, 2, 3], [4, 5, 6]])
print(nparr)
Output: [[1 2 3] [4 5 6]]
3 – Dimension Array:
To create a 3-D array with two 2-D arrays, both containing two arrays values:
import numpy as np
nparr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [7, 8, 9]]])
print(nparr)
Output: [[[1 2 3] [4 5 6]] [[1 2 3] [7 8 9]]]
Higher Dimensional Arrays
A higher dimensional array have number of dimensions.
import numpy as np
nparr = np.array([1, 2, 3, 4], ndmin=5)
print(nparr)
print('Total number of dimensions :', nparr.ndim)
Output: [[[[[1 2 3 4]]]]] number of dimensions : 5
Python Built-in Methods
There are lots of built-in ways to generate Arrays, we are describing a few of them.
arange
Return evenly spaced values within a given interval. For instance:
zeros and ones
Generate arrays of zeros or ones.
linspace
Return evenly spaced numbers over a specified interval.
code: np.linspace(0,10,3) Output: array([ 0., 5., 10.])
Code: np.linspace(0,10,50) Output: array([ 0. , 0.20408163, 0.40816327, 0.6122449 , 0.81632653, 1.02040816, 1.2244898 , 1.42857143, 1.63265306, 1.83673469, 2.04081633, 2.24489796, 2.44897959, 2.65306122, 2.85714286, 3.06122449, 3.26530612, 3.46938776, 3.67346939, 3.87755102, 4.08163265, 4.28571429, 4.48979592, 4.69387755, 4.89795918, 5.10204082, 5.30612245, 5.51020408, 5.71428571, 5.91836735, 6.12244898, 6.32653061, 6.53061224, 6.73469388, 6.93877551, 7.14285714, 7.34693878, 7.55102041, 7.75510204, 7.95918367, 8.16326531, 8.36734694, 8.57142857, 8.7755102 , 8.97959184, 9.18367347, 9.3877551 , 9.59183673, 9.79591837, 10. ])
eye
To create an identity matrix.
Code: np.eye(4)
OutPut: array([[ 1., 0., 0., 0.], [ 0., 1., 0., 0.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.]])
randn
Return a sample (or samples) from the “standard normal” distribution. Unlike rand which is uniform:
Code: np.random.randn(5,5)
Output:
This is all about Python NumPy array and some built-in methods. In the next tutorial, we will discuss the NumPy array indexing.
Must Read: