Codementor Events

NumPy in Python

Published Feb 04, 2022Last updated Aug 02, 2022
NumPy in Python

Introduction :

NumPy is a python library used for working with arrays.
NumPy stands for Numerical Python.

Why use NumPy?

  • Lists in python serve the same purpose as arrays, but they are to slow to process.
  • NumPy provides an array object that is upto 50x times faster than traditional python lists.

Importing NumPy :

import numpy 

Now, it is ready to use.

import numpy 
a=numpy.array([1,2,3])
print(a)

Importing NumPy with alias :

Usually, numpy is imported with np alias.

alias : alias is alternate name for referencing the same thing.

import numpy as np
a=np.array([1,2,3])
print(a)

Create Arrays with NumPy :

array() method of numpy is used to create arrays in numpy.
The array object in numpy is called ndarray.

import numpy as np
a=np.array([1,2,3])
print(a)
print(type(a))

type() function will tell the type of object passed to it.

You can pass any lists,tuple,dictionary in array and it will be converted into ndarray object.

Dimensions In Array :

  • 0-D Array :
import numpy as np
a=np.array(42)
print(a)
  • 1-D Array :
import numpy as np
a=np.array([1,2,3])
print(a)
  • 2-D Array :
import numpy as np
a=np.array([[1,2,3],[4,5,6]])
print(a)
  • 3-D Array :
import numpy as np
a=np.array([[[1,2,3],[4,5,6]],[7,8,9],[9,1,2]]])
print(a)

You can also check the number of dimensions in an array with the help of ndim attribute in python.

import numpy as np
a=np.array([[1,2,3],[4,5,6]])
print(a.ndim)

NumPy Indexing :

We can access array elements by referring to its index number.
Indexing in numpy starts with 0, first element has index 0 and second element has index 1 and so on.

#Get the first element from an array : 
import numpy as np
a=np.array([1,2,3])
print(a[0])
# Get the second and third element and add them :
import numpy as np
a=np.array([1,2,3,4,5])
print(a[1]+a[2])

Accessing 2-D Array :

To access the elements from 2-D array you can write it as [row index number,column index number]

#Access the element on the first row and second column:
import numpy as np
a=np.array([[1,2,3,4]],[5,6,7,8]])
print(a[0,1])

Negative Indexing :

Negative indexing starts with -1 from the end.

#Print the last element from 2nd dimension :
import numpy as np
a=np.array([[1,2,3,4],[5,6,7,8]])
print(a[1,-1])

Slicing in NumPy:

Slicing is fetching elements from one given index to other given index.
We can pass slice like this:
[start:end:step]

  • if you don't pass the start by default it is 0.
  • if you don't pass the end by default it is length of dimension.
  • if you don't pass the step by default it is 1.
#Slice elements from index 1 to index 5 :
import numpy as np
a=np.array([1,2,3,4,5,6])
print(a[1:5])
#Slice elements from index 4 to the end of the array :
import numpy as np
a=np.array([1,2,3,4,5,6])
print(a[4::])

Negative Slicing :

#Slice from index 3 from the end to index 1 from the end :
import numpy as np
a=np.array([1,2,3,4,5,6])
print(a[-3:-1])

Step Slicing :

#Return every other element from index 1 to index 5 :
import numpy as np
a=np.array([1,2,3,4,5,6])
print(a[1:5:2])

Slicing 2-D Array:

# From the second array slice the elements from index 1 to index 4 :
import numpy as np
a=np.array([[1,2,3,4,5,6,7],[7,8,9,8,7,6,5]])
print(a[1,1:4]

Checking the datatype of an Array:

You can check the data type of elements present in an array using dtype attribute.

import numpy as np
a=np.array([1,2,3,4,5])
print(a.dtype)

Shape of an Array :

Shape of an array is the number of rows and columns in an array.
shape attribute of numpy returns a tuple of number of rows and columns.

#Print the shape of 2-D Array :
import numpy as np
a=np.array([[1,2,3],[4,5,6]])
print(a.shape)

Reshaping an Array :

Reshaping an array means changing the shape of an array.

#Convert the following 1-D Array into 2-D Array :
import numpy as np
a=np.array([1,2,3,4,5,6,7,8,9,10,11,12])
x=a.reshape(3,4)
print(x)
Discover and read more posts from anshika vohra
get started
post comments7Replies
Dave Seth
2 years ago

Can i use this script for my https://local910.com/

Carlos Oscar
2 years ago

I want to share this on my site https://www.theroofingandgutters.com/

Alex Raza
2 years ago

Can i use this script for my https://www.ourlime.rocks/mcdonalds-modele-commercial/ blog?

Show more replies