Everything you need to know about NumPy

Hritika Agarwal
4 min readMay 29, 2020

--

NumPy or ‘Numerical Python’ is a library in python consisting of Multidimensional objects and tools for working with arrays.

Numpy enables us to do-

  • Both mathematical and logical operations on arrays.
  • Linear algebra operations
  • Fourier transforms and routines for shape manipulation.

NumPy arrays are stored at one continuous place in memory unlike lists,so ends up providing array objects that are up to 50x faster than Python lists.

Array objects in python are referred as ndarray, and can be created using the array() function

Installation of NumPy-:

C:\Users\Dell>pip install numpy

(after installing python and pip)

Importing NumPy -:

import numpy

OR

Import numpy as np

Example program-

import numpy as np

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

print(arr) -: [1,2,3,4,5]

  • Index numbering starts from zero.

Types of Arrays-

0-D Array

Each element in an array is a 0-D array

Example-:

import numpy as np

x = np.array(69)

print(x.ndim) -:0

ndim attribute tells how many dimensions an array have

1-D Array

It Contains 0D array as is elements

Example-:

import numpy as np

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

print(arr)

print(arr[0]) -:1

We can pass slice instead of index like this:[start: end].(known as slicing)

and can also define the steps: [start:end:step].

print(arr[1:4]) — : [2,3,4]

(Including first index element and excluding ending index number)

print(arr[-2:-4}] — :[ 4, 3]

(negative indexing means counting is done from back side)

print(arr[3:]) — : [4,5]

(including all starting from 3rd index number)

print(arr[1:5:2]) -:[2,4]

(from index no.1 to index no. 4 taking 2 steps)

print(arr.ndim) -:1

2-D array

Array containing 1-D array as its elements

import numpy as np

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

print( arr[0, 1]) -: 2

(1st 1D array have the index number 0, so choosing element of index no. 1 in first 1D array)

In 2-D array , first number tells the index of the !_array in it, while the second one about that element in the selected array)

print( arr[1, 4]) -: 10

print( arr[1, -1]) -: 10

print(arr[1, 1:4]) -: [7,8,9]

print(arr[0:2, 2]) -: [3 8]

( first choosing the 1-D array in it i.e 0 and 1 and then choosing second index element from both)

print(arr[0:2, 1:4]) -:[[2,3,4]

[7,8,9]]

print(arr.ndim) -:2

3-D array

Array containing 2-D arrays as its elements

import numpy as np

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

print(arr[0, 1, 2]) -:6

print(arr.ndim) -:3

What if we want to create a high dimension array may be 7, 8 or higher then this?

We can do that by-

import numpy as np

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

print(arr.ndim) -: 9

NumPy Datatypes

  • i — integer
  • b — boolean
  • u — unsigned integer
  • f — float
  • c — complex float
  • m — timedelta
  • M — datetime
  • O — object
  • S — string
  • U — unicode string
  • V — void ( fixed chunk of memory for other type)

Checking the datatype

dtype tells the data type of the array:

import numpy as np

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

print(arr.dtype)

Defining the Datatype

dtype can also be used to define the datatype of the array elements .

arr = np.array([1, 2, 3, 4,5,6], dtype=’S’)

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

For f, i, u, S and U we can define size as well.

Converting datatype-

The astype() function creates a copy of the array, and allows you to specify the data type as a parameter.

arr = np.array([1.1, 2.1, 3.1])

newarr = arr.astype(‘i/int’)

print(newarr)

print(newarr.dtype)

COPY()

It makes a copy and change the original array

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

x = arr.copy()

arr[0] = 42

print(arr)

print(x)

Output-: [42 2 3 4 5]

[1 2 3 4 5]

View()

It make a view and change the original array

import numpy as np

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

x = arr.view()

arr[0] = 42

print(arr)

print(x)

output-:

[42 2 3 4 5]

[42 2 3 4 5]

It can be said that Copies owns the data, and views doesn’t.

NumPy array has the base attribute that returns None if the array owns the data, otherwise refers to the original object.

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

x = arr.copy()

y = arr.view()

print(x.base)

print(y.base)

Output-:

None

[1 2 3 4 5]

Shape of an array

Shape of an array is referred as the number of elements in each dimension.

NumPy arrays has shape attribute which returns a tuple with each index having the number of corresponding elements.

arr = np.array([[11, 12, 13, 14], [15, 16, 17, 18]])

print(arr.shape)

The above program returns (2, 4), which means that the array has 2 dimensions,i.e. It is 2-D and each dimension has 4 elements.

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

print(arr.shape)

ans-:(1,1,1,1,1,4)

Arrays’s Reshaping

Reshaping means changing the shape of an array.

It enables us to add dimensions or remove dimensions or changing the number of elements in each dimension.

Reshaping can be done from 1-D to 2-D

x= np.array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22])

y= x.reshape(4, 3)

print(y)

output-:

[[ 11 12 13]

[ 14 15 16]

[ 17 18 19]

[20 21 22]]

Reshaping from 1-D to 3-D

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

y = x.reshape(2, 3, 2)

print(y)

Output-:

[[[ 1 2]

[ 3 4]

[ 5 6]]

[[ 7 8]

[ 9 10]

[11 12]]]

To check whether the returned element is a copy or view-:

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

print(x.reshape(2, 4).base)

Converting into 1D array(Flattening arrays)

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

y = x.reshape(-1)

print(y)

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

If we don’t know the exact number for one of the dimensions of our array, then we can pass -1 value in that, but remember only 1 dimension can be calculated by this-

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

y = x.reshape(2, 2, -1)

print(y)

Output-:

[[[1 2]

[3 4]]

[[5 6]

[7 8]]]

--

--

No responses yet