Numpy in python

NumPy, which stands for Numerical Python, is a library consisting of multidimensional array objects and a collection of rules for processing those arrays.

Using NumPy, analytical, and logical operations on arrays can be done. In this article, we will explain the basics of NumPy such as its architecture and environment.

NumPy is a very powerful and useful package that contains lots of features that help to do scientific programming in python.

  • A powerful N-dimensional array object
  • Sophisticated functions
  • Tools for blending C/C++ and Fortran code
  • Useful linear algebra, Fourier transform, and random number abilities

er abilities

1.Array

A NumPy array may be a grid of values, all of an equivalent type, and is indexed by a tuple of nonnegative integers. The amount of dimensions is the rank of the array; the shape of an array is a tuple of integers providing the size of the array with each dimension.

We can initialize NumPy arrays of nested Python lists, and access components using square brackets:

Example:

import numpy as np

X = np.array([1, 2, 3])
print(type(X))
print(X.shape)

print(X[0], X[1], X[2])
X[0] = 5
print(X)

Y = np.array([[1,2,3],[4,5,6]])
print(Y.shape)
print(Y[0, 0], Y[0, 1], Y[1, 0])

OutPut:

(3,)
1 2 3
[5 2 3]
(2, 3)
1 2 4

Numpy also gives many functions to create arrays:

import numpy as np

X = np.zeros((2,3))
print(X)
Y = np.ones((2,2))
print(Y)
XY = np.full((2,2), 5)
print(XY)
YZ = np.eye(2)
print(YZ)
XZ = np.random.random((2,2))
print(XZ)

Output:

[[ 0. 0. 0.]
[ 0. 0. 0.]]
[[ 1. 1.]
[ 1. 1.]]
[[ 5. 5.]
[ 5. 5.]]
[[ 1. 0.]
[ 0. 1.]]
[[ 0.69691604 0.31904542]
[ 0.94965845 0.40430043]]

you can also check it out this site for further detail about creating Array

2.Array indexing

Numpy offers many ways to index into arrays like,

  • Slicing
  • Integer array indexing
  • Boolean array indexing
Slicing :

Alike to Python lists, NumPy arrays can be sliced. Since arrays could also be multidimensional, you want to specify a slice for every dimension of the array:

Example:

import numpy as np

X = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
Y = X[:2, 1:4]
print(X[0, 1])
Y[0, 0] = 77
print(X[0, 1])

Output:

2
77

Integer indexing

we can also mix integer indexing with slice indexing. However, doing so will yield an array of inferiority than the first array. it is Quite Different from MATLAB array slicing. array slicing.

import numpy as np

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

row_r1 = X[1, :]
row_r2 = X[1:2, :]
print(row_r1, row_r1.shape)
print(row_r2, row_r2.shape)

col_r1 = X[:, 1]
col_r2 = X[:, 1:2]
print(col_r1, col_r1.shape)
print(col_r2, col_r2.shape)

Output:

5 6 7 8
[[5 6 7 8]] (1, 4)
2 6 10
[[ 2]
[ 6]
[10]] (3, 1)

Boolean array indexing:

Usually, Boolean array indexing is used to select the elements of an array that meet some condition.

import numpy as np

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

bool_idx = (a > 2)
print(bool_idx)
print(a[bool_idx])
print(a[a > 2]

Output:

[[False False]
[ True True]
[ True True]]
[3 4 5 6]
[3 4 5 6]

3.Datatypes

Numpy gives a large set of numeric datatypes that you can use to create arrays. Numpy tries to guess a datatype when you create an array, but functions that construct arrays usually also include an optional argument to explicitly define the datatype.

Example:

import numpy as np

arr = np.array([1, 2])
print(x.dtype)

arr = np.array([1.0, 2.0])
print(x.dtype)

arr = np.array([1, 2], dtype=np.int64)
print(x.dtype)

4.Array math

Using Numpy we can do mathematical functions operate elementwise on the array and is also available in operator overload and as function.

Example:

import numpy as np

x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)

#Addition

print(x + y)
print(np.add(x, y))

#subtraction

print(x – y)
print(np.subtract(x, y))

#multiplication

print(x * y)
print(np.multiply(x, y))

#division

print(x / y)
print(np.divide(x, y))

#squart Root

print(np.sqrt(x))

Output:

[[ 6. 8.]
[ 10. 12.]]
[[ 6. 8.]
[ 10. 12.]]

[[-4. -4.]
[-4. -4.]]
[[-4. -4.]
[-4. -4.]]

[[ 5. 12.]
[ 21. 32.]]
[[ 5. 12.]
[ 21. 32.]]

[[ 0.2 0.33333333]
[ 0.42857143 0.5 ]]
[[ 0.2 0.33333333]
[ 0.42857143 0.5 ]]

[[ 1. 1.41421356]
[ 1.73205081 2. ]]

Numpy gives many useful functions for doing computations on arrays; one of the most useful is Sum.

import numpy as np

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

print(np.sum(x))
print(np.sum(x, axis=0))
print(np.sum(x, axis=1))

Output:

10
[4 6]
[3 7]

5.Broadcasting

Broadcasting is a great mechanism that allows NumPy to work with arrays of different shapes when doing arithmetic operations. Usually, we have a smaller array and a larger array, and we want to use the smaller array multiple times to do some operation on the larger array.

Example:

import numpy as np

x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = np.empty_like(x)

for i in range(4):
y[i, :] = x[i, :] + v

print(y)

Output:

[[ 2 2 4]
[ 5 5 7]
[ 8 8 10]
[11 11 13]]

Numpy broadcasting permits us to do this computation without actually creating multiple copies of Arr. View this version, using programming: see the Example below

import numpy as np

x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
Arr = np.array([1, 0, 1])
y = x + Arr
print(y)

Output:

[[ 2 2 4]
[ 5 5 7]
[ 8 8 10]
[11 11 13]]

it is just a basic feature of NumPy if you want to know about NumPy or get more detail about NumPy please to visit numpy site.

Leave a Comment