Ndarray is the n-dimensional array object defined in the numpy which stores the collection of the similar type of elements. In other words, we can define a ndarray as the collection of the data type (dtype) objects.
The ndarray object can be accessed by using the 0 based indexing. Each element of the Array object contains the same size in the memory.
The ndarray object can be created by using the array routine of the numpy module. For this purpose, we need to import the numpy.
>>> a = numpy.array
Consider the below image.
We can also pass a collection object into the array routine to create the equivalent n-dimensional array. The syntax is given below.
>>> numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
The parameters are described in the following table.
SN | Parameter | Description |
---|---|---|
1 | object | It represents the collection object. It can be a list, tuple, dictionary, set, etc. |
2 | dtype | We can change the data type of the array elements by changing this option to the specified type. The default is none. |
3 | copy | It is optional. By default, it is true which means the object is copied. |
4 | order | There can be 3 possible values assigned to this option. It can be C (column order), R (row order), or A (any) |
5 | subok | The returned array will be base class array by default. We can change this to make the subclasses passes through by setting this option to true. |
6 | ndmin | It represents the minimum dimensions of the resultant array. |
To create an array using the list, use the following syntax.
>>> a = numpy.array([1, 2, 3])
To create a multi-dimensional array object, use the following syntax.
>>> a = numpy.array([[1, 2, 3], [4, 5, 6]])
To change the data type of the array elements, mention the name of the data type along with the collection.
>>> a = numpy.array([1, 3, 5, 7], complex)
The ndim function can be used to find the dimensions of the array.
>>> import numpy as np >>> arr = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [9, 10, 11, 23]]) >>> print(arr.ndim)
The itemsize function is used to get the size of each array item. It returns the number of bytes taken by each array element.
Consider the following example.
#finding the size of each item in the array import numpy as np a = np.array([[1,2,3]]) print("Each item contains",a.itemsize,"bytes")
Output:
To check the data type of each array item, the dtype function is used. Consider the following example to check the data type of the array items.
#finding the data type of each array item import numpy as np a = np.array([[1,2,3]]) print("Each item is of the type",a.dtype)
Output:
To get the shape and size of the array, the size and shape function associated with the numpy array is used.
Consider the following example.
import numpy as np a = np.array([[1,2,3,4,5,6,7]]) print("Array Size:",a.size) print("Shape:",a.shape)
Output:
By the shape of the array, we mean the number of rows and columns of a multi-dimensional array. However, the numpy module provides us the way to reshape the array by changing the number of rows and columns of the multi-dimensional array.
The reshape() function associated with the ndarray object is used to reshape the array. It accepts the two parameters indicating the row and columns of the new shape of the array.
Let's reshape the array given in the following image.
import numpy as np a = np.array([[1,2],[3,4],[5,6]]) print("printing the original array..") print(a) a=a.reshape(2,3) print("printing the reshaped array..") print(a)
Output:
Slicing in the NumPy array is the way to extract a range of elements from an array. Slicing in the array is performed in the same way as it is performed in the python list.
Consider the following example to print a particular element of the array.
import numpy as np a = np.array([[1,2],[3,4],[5,6]]) print(a[0,1]) print(a[2,0])
Output:
The above program prints the 2nd element from the 0th index and 0th element from the 2nd index of the array.
The linspace() function returns the evenly spaced values over the given interval. The following example returns the 10 evenly separated values over the given interval 5-15
import numpy as np a=np.linspace(5,15,10) #prints 10 values which are evenly spaced over the given interval 5-15 print(a)
Output:
The NumPy provides the max(), min(), and sum() functions which are used to find the maximum, minimum, and sum of the array elements respectively.
Consider the following example.
import numpy as np a = np.array([1,2,3,10,15,4]) print("The array:",a) print("The maximum element:",a.max()) print("The minimum element:",a.min()) print("The sum of the elements:",a.sum())
Output:
A NumPy multi-dimensional array is represented by the axis where axis-0 represents the columns and axis-1 represents the rows. We can mention the axis to perform row-level or column-level calculations like the addition of row or column elements.
To calculate the maximum element among each column, the minimum element among each row, and the addition of all the row elements, consider the following example.
import numpy as np a = np.array([[1,2,30],[10,15,4]]) print("The array:",a) print("The maximum elements of columns:",a.max(axis = 0)) print("The minimum element of rows",a.min(axis = 1)) print("The sum of all rows",a.sum(axis = 1))
Output:
The sqrt() and std() functions associated with the numpy array are used to find the square root and standard deviation of the array elements respectively.
Standard deviation means how much each element of the array varies from the mean value of the numpy array.
Consider the following example.
import numpy as np a = np.array([[1,2,30],[10,15,4]]) print(np.sqrt(a)) print(np.std(a))
Output:
The numpy module allows us to perform the arithmetic operations on multi-dimensional arrays directly.
In the following example, the arithmetic operations are performed on the two multi-dimensional arrays a and b.
import numpy as np a = np.array([[1,2,30],[10,15,4]]) b = np.array([[1,2,3],[12, 19, 29]]) print("Sum of array a and b\n",a+b) print("Product of array a and b\n",a*b) print("Division of array a and b\n",a/b)
The numpy provides us with the vertical stacking and horizontal stacking which allows us to concatenate two multi-dimensional arrays vertically or horizontally.
Consider the following example.
import numpy as np a = np.array([[1,2,30],[10,15,4]]) b = np.array([[1,2,3],[12, 19, 29]]) print("Arrays vertically concatenated\n",np.vstack((a,b))); print("Arrays horizontally concatenated\n",np.hstack((a,b)))
Output: