. Advertisement .
..3..
. Advertisement .
..4..
In Python programming language, you might usually hear about the term of NumPy, which is a library containing multidimensional arrays and matrix data.
NumPy will bring out ndarray which is known as an object of a homogeneous n-dimension array with various methods for operating on it efficiently. Particulary, numpy.newaxis function is one of the most used methods for converting arrays in NumPy. Let’s find out more about this function in this blog!
1. Use the numpy.newaxis Function to Convert 1D Array to the Matrix in Python
The numpy.newaxis function is also known as the None used for indexing array in Python. The most common and simplest usage of this method is adding new dimensions to NumPy arrays in Python. For instance, you can use numpy.newaxis
function to convert 1D array to 2D array, 2D array to 3D array, etc.
1.1 Use [np.newaxis] to convert 1D array to the row matrix
By using this method, whether a matrix in row or column is also converted from the 1D array. The below code demonstration will describe how to use numpy.newaxis
to convert the 1D array to the row matrix in Python.
With the above tutorial, you can see the 1D array was converted into the row matrix by using np.newaxis
. In the beginning, to create the 1D array, an np.array()
function was used. After that, we performed [np.newaxis
] to index the array and return it to the row matrix.
import numpy as np
array = np.array([1,2,3,4])
print(array.shape)
array = array[np.newaxis]
print(array.shape)
Output:
(4,)
(1, 4)
1.2 Use [:, np.newaxis] to convert 1D array to the comlumn matrix
The following example will guide how the 1D array can be converted into a column matrix. Similarly, the np.array()
function was used to create the 1D array. Then, to index the array and return it to the column matrix, we conducted the [:, np.newaxis]
function.
import numpy as np
array = np.array([1,2,3,4])
print(array.shape)
array = array[:,np.newaxis]
print(array.shape)
Output:
(4,)
(4, 1)
2. Use the None to Convert 1D Array to the Matrix in Python
This blog already mentioned to you about an alias of the None, which is the numpy.newaxis function. Therefore, instead of using np.newaxis, you can replace it by using the None to implement those discussed operations above.
2.1 Use [None] to convert 1D array to the row matrix
With the guidance below, you will see how a 1D array in Python was converted into the row matrix by using the None. We also start with the function of np.array()
to create the 1D array. After that, [None]
was used to index the array and return it to the row matrix.
import numpy as np
array = np.array([1,2,3,4])
print(array.shape)
array = array[None]
print(array.shape)
Output:
(4,)
(1, 4)
2.2 Use [:, None] to convert 1D array to the column matrix
The code treatment with column matrix by using the None is also similar to the conducted operation with the method of np.newaxis
. 1D array is also formed by performing np.array()
. Then, [;, None]
was conducted to index the array and return it to the column matrix.
import numpy as np
array = np.array([1,2,3,4])
print(array.shape)
array = array[:, None]
print(array.shape)
Output:
(4,)
(4, 1)
Conclusion
This blog has guided you on how to use the numpy.newaxis
function to convert arrays into matrix data structures in Python. You also can acknowledge that there is no difference between np.newaxis and the None. You can definitely you both of these methods for indexing or slicing arrays in Python. By the way, you can often visit our website for more interesting IT solutions!
Leave a comment