NumPy Ndarray

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.

Creating a ndarray object

The ndarray object can be created by using the array routine of the numpy module. For this purpose, we need to import the numpy.

Consider the below image.

NumPy Ndarray

We can also pass a collection object into the array routine to create the equivalent n-dimensional array. The syntax is given below.

The parameters are described in the following table.

SNParameterDescription
1objectIt represents the collection object. It can be a list, tuple, dictionary, set, etc.
2dtypeWe can change the data type of the array elements by changing this option to the specified type. The default is none.
3copyIt is optional. By default, it is true which means the object is copied.
4orderThere can be 3 possible values assigned to this option. It can be C (column order), R (row order), or A (any)
5subokThe 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.
6ndminIt represents the minimum dimensions of the resultant array.

To create an array using the list, use the following syntax.

NumPy Ndarray

To create a multi-dimensional array object, use the following syntax.

NumPy Ndarray

To change the data type of the array elements, mention the name of the data type along with the collection.

NumPy Ndarray

Finding the dimensions of the Array

The ndim function can be used to find the dimensions of the array.

NumPy Ndarray

Finding the size of each array element

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.

Example

Output:

Each item contains 8 bytes.

Finding the data type of each array item

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.

Example

Output:

Each item is of the type int64

Finding the shape and size of the array

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.

Example

Output:

Array Size: 7
Shape: (1, 7)

Reshaping the array objects

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.

NumPy Ndarray

Example

Output:

printing the original array..
[[1 2]
 [3 4]
 [5 6]]
printing the reshaped array..
[[1 2 3]
 [4 5 6]]

Slicing in the Array

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.

Example

Output:

2
5

The above program prints the 2nd element from the 0th index and 0th element from the 2nd index of the array.

Linspace

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

Example

Output:

[ 5.          6.11111111  7.22222222  8.33333333  9.44444444 10.55555556
 11.66666667 12.77777778 13.88888889 15.        ]

Finding the maximum, minimum, and sum of the array elements

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.

Example

Output:

The array: [ 1  2  3 10 15  4]
The maximum element: 15
The minimum element: 1
The sum of the elements: 35

NumPy Array Axis

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.

NumPy Ndarray

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.

Example

Output:

The array: [[1  2  30]
		   [10  15  4]]
The maximum elements of columns: [10 15 30]
The minimum element of rows [1 4]
The sum of all rows [33 29]

Finding square root and standard deviation

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.

Example

Output:

[[1.         1.41421356 5.47722558]
 [3.16227766 3.87298335 2.        ]]
10.044346115546242

Arithmetic operations on the array

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.

Example

Array Concatenation

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.

Example

Output:

Arrays vertically concatenated
 [[ 1  2 30]
 [10 15  4]
 [ 1  2  3]
 [12 19 29]]
Arrays horizontally concatenated
 [[ 1  2 30  1  2  3]
 [10 15  4 12 19 29]]

Next TopicNumPy DataTypes




Latest Courses