Codied To Clipboard !
Home > Notes > Python Numpy
NumPy is used to work with arrays. The array object in NumPy is called ndarray. We can create a NumPy ndarray object by using the array() function
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr) print(type(arr))
To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray
import numpy as np arr = np.array((1, 2, 3, 4, 5)) print(arr)
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
import numpy as np arr = np.array(42) print(arr)
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array. These are the most common and basic arrays.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr)
An array that has 1-D arrays as its elements is called a 2-D array. These are often used to represent matrix or 2nd order tensors.
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr)
An array that has 2-D arrays (matrices) as its elements is called 3-D array. These are often used to represent a 3rd order tensor Create a 3-D array with two 2-D arrays, both containing two arrays with the values 1,2,3 and 4,5,6:
import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) print(arr)
NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have.
import numpy as np a = np.array(42) b = np.array([1, 2, 3, 4, 5]) c = np.array([[1, 2, 3], [4, 5, 6]]) d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) print(a.ndim) print(b.ndim) print(c.ndim) print(d.ndim)
An array can have any number of dimensions. When the array is created, you can define the number of dimensions by using the ndmin argument
import numpy as np arr = np.array([1, 2, 3, 4], ndmin=5) print(arr) print('number of dimensions :', arr.ndim)
Create a NumPy ndarray Object NumPy is used to work with arrays. The array object in NumPy is called ndarray. We can create a NumPy ndarray object by using the array() function.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr) print(type(arr))
To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray:
import numpy as np arr = np.array((1, 2, 3, 4, 5)) print(arr)
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array
import numpy as np arr = np.array(42) print(arr)
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array. These are the most common and basic arrays.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr)
An array that has 1-D arrays as its elements is called a 2-D array. These are often used to represent matrix or 2nd order tensors.
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr)
An array that has 2-D arrays (matrices) as its elements is called 3-D array. These are often used to represent a 3rd order tensor.
import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) print(arr)
NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have
import numpy as np a = np.array(42) b = np.array([1, 2, 3, 4, 5]) c = np.array([[1, 2, 3], [4, 5, 6]]) d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) print(a.ndim) print(b.ndim) print(c.ndim) print(d.ndim)
An array can have any number of dimensions. When the array is created, you can define the number of dimensions by using the ndmin argument
import numpy as np arr = np.array([1, 2, 3, 4], ndmin=5) print(arr) print('number of dimensions :', arr.ndim)