NumPy Tutorial Python Scientific library

NumPy is very useful Python library which is used for numerical purposes hence the name “NumPy”.

We can do complex scientific calculations very easy and in faster way.It is also known as array processing package of python.

Setup: We need to install this package before using it.We can install this python package using below command on python terminal.

pip install numpy   

Lets dive into one working example of NumPy:

import numpy as np
xArray = np.array([5, 18,
28, 30,
34, 89])

print(type(xArray))

print(xArra

Output:

<class 'numpy.ndarray'>
[ 5 18 28 30 34 89]

Here we are creating Nd array with 6 elements and then printing the data type first and the content of the array.

Array attributes

Below are the various array attributes we will discuss shortly:

ndim : It tells us about the number of dimensions of the array.

shape: It returns the number of elements along each dimension.

size : Returns total number of elements in an array.

dtype Data type of the elements in an array.

itemsize Return total size of each element in Bytes.

nbytes Return the total bytes consumed by all elements.

import numpy as np
xArray = np.array([5, 18,
28, 30,
34, 89])

print(type(xArray))

print(xArray)

print('Number of dimensions:',xArray.ndim)
print('Shape:',xArray.shape)
print('Number of elements:',xArray.size)
print('Data type of elements:',xArray.dtype)
print('ItemSize:',xArray.itemsize)
print('No of bytes cons

Output:

<class 'numpy.ndarray'>
[ 5 18 28 30 34 89]
Number of dimensions: 1
Shape: (6,)
Number of elements: 6
Data type of elements: int64
ItemSize: 8
No of bytes consumed by element: 48

Leave a comment