Let’s explore NumPy !!

Bagiyalakshmi
featurepreneur
Published in
3 min readDec 11, 2022

NumPy is a Python library used for working with arrays. It adds powerful data structures to Python that guarantee efficient calculations with arrays and matrices and it supplies an enormous library of high-level mathematical functions that operate on these arrays and matrices.

NumPy stands for numerical python, It is high performance multi-dimensional array data structures. Mostly implemented in C language.

import numpy as np
print(np.__version__)

## 1.22.4

N-Dimensional array

Create rank 1 array:

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

## <class ‘numpy.ndarray’>

Accessing the array using index numbers.

print(x[0], x[1], x[2])

## 1 2 3

Change an element of the array

x[0]=5
print(x)

## [5 2 3]

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

## [[1 2 3]
[4 5 6]]

## (2, 3) →(row, column)

List of lists

l = [[1, 2, 3], [4, 5, 6]]

Converting list to array

x = np.array(l)

Converting the type of array

x1 = x.astype(np.float64)

Create a sequential values

x = np.arange(10, 59)
print(x)

## [10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
58]

Printing first, last 10 numbers and reverse of array.

print(x[:10])
print(x[-10:])
print(x[::-1])

## [10 11 12 13 14 15 16 17 18 19]

## [49 50 51 52 53 54 55 56 57 58]

##[58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35
34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11
10]

Creating an array of all zeros

x = np.zeros((3, 2))
print(x)

## [[0. 0.]
[0. 0.]
[0. 0.]]

To fill an array with same number

x = np.full((5, 5), 7)
print(x)

## [[7 7 7 7 7]
[7 7 7 7 7]
[7 7 7 7 7]
[7 7 7 7 7]
[7 7 7 7 7]]

Creating an array of all ones

x = np.ones((2,2))
print(x)

## [[1. 1.]
[1. 1.]]

Seed the random function for repeatability

np.random.seed(25)

To Read a standard documentation

if you want to read about any function, for example randint

help(np.random.randint)
x1 = np.random.randint(10, size =5)    # one dimensional
x2 = np.random.randint(10, size(2,3)) # two dimensional
print(x1)

## [5 9 2 3 2]

Sliced result is pointer to the actual array, so modifying the sliced array will modify the original array.

Vectorized operations

  1. addition
x1 = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
x2 = np.array([[5,4], [6,7], [9,10], [10,12]])
print(np.add(x1, x2))

## [[ 6 6]
[ 9 11]
[14 16]
[17 20]]

2. subtraction

x1 = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
x2 = np.array([[5,4], [6,7], [9,10], [10,12]])
print(np.subtract(x1, x2))

## [[-4 -2]
[-3 -3]
[-4 -4]
[-3 -4]]

3. Multiply

print(np.multiply(x1,x2))

## [[ 5 8]
[18 28]
[45 60]
[70 96]]

4. Divide

print(np.divide(x1,x2))

## [[0.2 0.5 ]
[0.5 0.57142857]
[0.55555556 0.6 ]
[0.7 0.66666667]]

5. Square root

print(np.sqrt(x1))

## [[1. 1.41421356]
[1.73205081 2. ]
[2.23606798 2.44948974]
[2.64575131 2.82842712]]

Array Transpose

x = np.array([[1,2,3],[5,7,8]])
print(x)
print("transpose= ", x.T)

## [[1 2 3]
[5 7 8]]

## transpose= [[1 5]
[2 7]
[3 8]]

Be tuned for more updates.

Thank you !!

--

--

Bagiyalakshmi
featurepreneur

Learning something new everyday keeps me busy and refresh