Basic NumPy array dimension visualization in Python
Hello programmers, in this tutorial we will understand the basics of NumPy array and visualize them in Python.
NumPy is a module in Python which is mainly used for scientific computing. It is mainly used for working with numerical data in Python. This module provides a wide range of functionalities such as array objects and matrix data structures. The array of objects created from the NumPy module is very fast than the creation of a normal list in Python.
Run the following command in your command prompt to install the NumPy library.
pip install numpy
Import the NumPy library using the following command in your python console.
#Import library import numpy as np from numpy import *
Creating a single-dimension NumPy array
singleDimArray = np.array([1,2,3,4,5]) #creating a single dimension array print(f"The numpy array created is: {singleDimArray}") print(f"The type of the array is: {type(singleDimArray)}") print(f"The shape of the array is: {singleDimArray.shape}") print(f"The dimension of the array is: {singleDimArray.ndim}")
Output
The numpy array created is: [1 2 3 4 5] The type of the array is: <class 'numpy.ndarray'> The shape of the array is: (5,) The dimension of the array is: 1
Explanation
We created a NumPy array using the np.array() method. We used the type() method to check the type of the array, the shape() method to check the shape of the data, and the ndim() method to check the dimension of the array created.
Creating a multi-dimensional array
multDimArray = np.array([[1,2,3],[4,5,6],[7,8,9]]) #creating a single dimension array print(f"The numpy array created is: \n{multDimArray}") print(f"The type of the array is: {type(multDimArray)}") print(f"The shape of the array is: {multDimArray.shape}") print(f"The dimension of the array is: {multDimArray.ndim}")
Output
The numpy array created is: [[1 2 3] [4 5 6] [7 8 9]] The type of the array is: <class 'numpy.ndarray'> The shape of the array is: (3, 3) The dimension of the array is: 2
Explanation
We created a NumPy array using the np.array() method. We used the type() method to check the type of the array, the shape() method to check the shape of the data, and the ndim() method to check the dimension of the array created.
Creating a random value NumPy array
randNpArray = np.random.randint(8, size = (5,6)) #creating a random numpy array print(f"The numpy array created is: \n{randNpArray}")
Output
The numpy array created is: [[7 4 6 1 5 1] [2 3 5 5 3 5] [5 6 5 2 4 4] [6 4 7 7 7 1] [0 2 3 6 1 4]]
print(f"The shape of the array is: {randNpArray.shape}")
Output
The shape of the array is: (5, 6)
Reshaping the matrix into a different shape matrix using the reshape() method
#Reshaping the matrix to a 10x3 matrix reshapedArray = randNpArray.reshape(10,3) print(f"The numpy array created is: \n{reshapedArray}")
Output
The numpy array created is: [[7 4 6] [1 5 1] [2 3 5] [5 3 5] [5 6 5] [2 4 4] [6 4 7] [7 7 1] [0 2 3] [6 1 4]]
Changing the dimension of the matrix created above using the reshape() method
#Changing the dimension of the matrix to a 1x30 matrix reshapedArray = randNpArray.reshape(1,30) print(f"The numpy array created is: \n{reshapedArray}")
Output
The numpy array created is: [[7 4 6 1 5 1 2 3 5 5 3 5 5 6 5 2 4 4 6 4 7 7 7 1 0 2 3 6 1 4]]
Reshaping the NumPy array along the row
reshapedArray = reshapedArray.reshape(10,3,order = 'C') #reshaping along the row print(f"The numpy array created is: \n{reshapedArray}")
Output
The numpy array created is: [[7 4 6] [1 5 1] [2 3 5] [5 3 5] [5 6 5] [2 4 4] [6 4 7] [7 7 1] [0 2 3] [6 1 4]]
Reshaping the NumPy array along the column
reshapedArray = reshapedArray.reshape(3,10,order = 'F') #reshaping along the row print(f"The numpy array created is: \n{reshapedArray}")
Output
The numpy array created is: [[7 5 6 6 3 4 2 1 5 1] [1 5 7 4 3 4 1 5 4 3] [2 2 0 5 6 7 6 5 7 4]]
Reshaping the NumPy array into a single-dimension array using the ravel() method
#Reshaping the matrix into a single dimension array reshapedArray = reshapedArray.ravel(order = 'C') print(f"The numpy array created is: \n{reshapedArray}")
Output
The numpy array created is: [7 5 6 6 3 4 2 1 5 1 1 5 7 4 3 4 1 5 4 3 2 2 0 5 6 7 6 5 7 4]
Explanation
The ravel() method is used to reshape the n-dimensional matrix into a single dimension matrix. The order = ‘C’, means that we are flattening the array along the row which is the default in nature.
#Reshaping the matrix into a single dimension array reshapedArray = reshapedArray.ravel(order = 'F') print(f"The numpy array created is: \n{reshapedArray}")
Output
The numpy array created is: [7 5 6 6 3 4 2 1 5 1 1 5 7 4 3 4 1 5 4 3 2 2 0 5 6 7 6 5 7 4]
Explanation
The ravel() method is used to reshape the n-dimensional matrix into a single dimension matrix. The order = ‘F’, means that we are flattening the array along the column.
Leave a Reply