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.

snippet
>>> a = numpy.array

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.

snippet
>>> 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.

snippet
>>> a = numpy.array([1, 2, 3])
NumPy Ndarray

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

snippet
>>> a = numpy.array([[1, 2, 3], [4, 5, 6]])
NumPy Ndarray

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

snippet
>>> a = numpy.array([1, 3, 5, 7], complex)
NumPy Ndarray

Finding the dimensions of the Array

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

snippet
>>> import numpy as np
>>> arr = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [9, 10, 11, 23]])

>>> print(arr.ndim)
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

snippet
#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:

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

snippet
#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:

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

snippet
import numpy as np
a = np.array([[1,2,3,4,5,6,7]])
print("Array Size:",a.size)
print("Shape:",a.shape)

Output:

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

snippet
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:

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

snippet
import numpy as np
a = np.array([[1,2],[3,4],[5,6]])
print(a[0,1])
print(a[2,0])

Output:

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

snippet
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:

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

snippet
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:

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

snippet
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:

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

snippet
import numpy as np
a = np.array([[1,2,30],[10,15,4]])
print(np.sqrt(a))
print(np.std(a))

Output:

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

snippet
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)

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

snippet
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:

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]]
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +