By:
Chethana Ramachandran

APR 12,2023

NumPy is a Python package that is used for array processing. NumPy stands for Numeric Python. It supports the processing and computation of multidimensional array elements.

For the efficient calculation of arrays and matrices, NumPy adds a powerful data structure to Python, and it supplies a boundless library of high-level mathematical functions.

NumPy provides N-dimensional array types. It can be used for different mathematical operations on arrays.

The pip command is used to install the numpy package. Run the below command in the

terminal.

> pip install numpy

If you don’t have pip on your system, you should first install Pip.

Import NumPy

To access the NumPy, we should import the numpy into our Python code. For that, we can use the command.

> import numpy as np

The np is used as an alias for the numpy.

Creating an array using numpy

Example:

>>> import numpy as np

>>> a = np.array([1,2,3,4])

>>> print(a)

Output:

[1 2 3]

Ndarray

The most important feature of numpy is Ndarray. It is an N-dimensional array that stores a collection of similar types of elements.

Example:

>>> import numpy as np

>>> a = np.array( [1,2,3],[4,5,6] )

>>> print( “Type:”, type(a))

Output:

Type: <class 'numpy.ndarray'>

Like Python, NumPy supports different types of data types. A list of scalar data types is demonstrated in the following table:

The data type of the NumPy array is obtained by using the object known as dtype.

Syntax:

numpy.dtype(object,align,copy)

Example:

import numpy as np

arr = np.array([1, 2, 3, 4])

print(arr.dtype)

Output:

Int64

We can represent the Numpy data types with characters like i,b,u,S etc.

Example:

import numpy as np

arr = np.array([1, 2, 3, 4], dtype='i4')

print(arr)

print(arr.dtype)

Output:

[1 2 3 4]

int32

Array indexing in numpy is similar to accessing an array element. The indexing of the numpy array is started with 0, which means the first element index is 0, the second element index is 1, and so on.

Example:

>>> import numpy as np

>>> a = np.array([1,2,3,4])

>>> print(a[3])

Output:

4

NumPy arrays have the attribute called shape. It will return the number of elements in each dimension in a tuple.

Example:

>>>import numpy as np

>>>ar = np.array([[1, 2, 3], [5, 6, 7]])

>>>print(ar.shape)

Output:

(2,3)

In NumPy, which has one dimensional, two dimensional and three-dimensional arrays, we can change the shape of the array by using reshape.

Example:

>>>import numpy as np

>>>ar1 = np.array([1, 2, 4, 8, 5, 6, 7, 3, 9, 10, 11, 12])

>>>newarr = ar1.reshape(4, 3)

>>>print(newarr)

Output:

[[1 2 4]

[8 5 6]

[7 3 9]

[10 11 12]]

NumPy array slicing means the extraction of a portion of an array from the original array.

It can be demonstrated by the syntax: [start: stop: step]

The start, stop, step are the parameters passed to the array; start indicates the start index of the array to be sliced, the default index value is 0; stop indicates the length of the array to be sliced, the step will consider the default value is 1 if we don't pass any value.

Example:

>>>import numpy as np

>>>arr = np.array([3, 5, 7, 8, 11, 14, 18])

>>>arr2 = arr[1:6]

>>>print(arr2)

Output:

[ 5 7 8 11 14]

NumPy provides different kinds of functions to combine the arrays.

numpy.concatenate

numpy.stack

By using the concatenate function, it will join two or more arrays along a specified axis. The default axis value is 0.

Example:

import numpy as np

array_1 = np.array([1, 2])

array_2 = np.array([3, 4])

array_new = np.concatenate((array_1, array_2))

print(array_new)

Output:

[1 2 3 4]

By using the stack function, it will join two or more arrays along a new axis.

Example:

import numpy as np

arr1 = np.array([1, 2, 3])

arr2 = np.array([4, 5, 6])

arr = np.stack((arr1, arr2), axis=1)

print(arr)

Output:

[[1 4]

[2 5]

[3 6]]

In NumPy, some functions return a copy of an array or some return the view while executing a function, when the contents are physically stored in another location, it is called copy. The view will return a new array that looks like the exact location data of the original array.

Example:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

x = arr.view() //declare the view

y = arr.copy() //declare the copy

arr[0] = 16

print(arr)

print(x)

print(y)

Output:

[16 2 3 4 5]

[16 2 3 4 5]

[1 2 3 4 5]

Overall Numpy arrays are faster than Python lists. And it will take less memory to process.

If you need any assistance in odoo, we are online, please chat with us.

0

Comments
Calicut

Cybrosys Technologies Pvt. Ltd.

Neospace, Kinfra Techno Park

Kakkancherry, Calicut

Kerala, India - 673635

London

Cybrosys Limited

Alpha House,

100 Borough High Street, London,

SE1 1LB, United Kingdom

Kochi

Cybrosys Technologies Pvt. Ltd.

1st Floor, Thapasya Building,

Infopark, Kakkanad,

Kochi, India - 682030.

Bangalore

Cybrosys Techno Solutions

The Estate, 8th Floor,

Dickenson Road,

Bangalore, India - 560042

Send Us A Message