Hands-on with NumPy basics

Pavini Jain
3 min readJun 13, 2021

NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

Prerequisite

You should be familiar with the basics of Jupyter Notebook or Google Colab.

Table of Content

  1. Installing NumPy
  2. Importing Library
  3. Introducing Array
  4. Indexing
  5. Some Inbuilt Functions
  6. Reference Type
  7. Random Distribution

Note

If you encounter any problem while coding then you can refer to my Jupyter notebook on Github.

To get the maximum benefit from this tutorial code along with me line-by-line.

Installing NumPy

If you are working on Google Colab then there is no need to install NumPy as it already has all the.

If you are working in Jupyter Notebook, you can install NumPy by running the following command in Anaconda Prompt:

pip install numpy

If you use conda, you can install NumPy from the defaults or conda-forge channels:

# Best practice, use an environment rather than install in the base env
conda create -n my-env
conda activate my-env
# If you want to install from conda-forge
conda config --env --add channels conda-forge
# The actual install command
conda install numpy

Importing Library

To import the NumPy library use the following code in your Colab or notebook:

import numpy as np

Introducing Array

Declare an array:

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

To know the type of array:

type(arr)

View the array:

arr

Find the shape of the array:

arr.shape

Converting a 1D array to a 3D array:

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

Now, if you check the shape, it comes out to be (3,5) which denotes 3 rows and 5 columns.

arr.shape

You can reshape it to 5 rows and 3 columns. But make sure that the number of elements in the original array is the same as in the new array else it will throw an error.

arr.reshape(5,3)

Indexing

Accessing the array elements and printing the third index (according to 0 based indexing):

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

Making a new array as we did earlier:

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

Printing elements from 1st row to the last row and columns from the 3rd row to the last row:

## arr[<row_start>:<row_end+1>,<col_start>:<col_end+1>,<step>]
arr[1:,3:]

Some Inbuilt Functions

‘arange’ : is an in-built function which gives all the values from the first parameter to the last parament (last parameter exclusive).

arr=np.arange(0,10)
arr

‘linespace’ : it prints the given number of values (third parameter) from first parameter to the last (last parameter exclusive).

np.linspace(1,10,50)

Reference Type

Copy function and broadcasting: The following code copies 100 from the 3rd element to the last .

arr[3:]=100
arr

You will get the following output:

array([ 0, 1, 2, 100, 100, 100, 100, 100, 100, 100])

Now try:

arr1=arr
arr1[3:]=500
print(arr1)
print(arr)

you will get:

[  0   1   2 500 500 500 500 500 500 500]
[ 0 1 2 500 500 500 500 500 500 500]

This clearly shows that any change in ‘arr1’ is being reflected in the ‘arr’. But that’s not we want. We just want to copy the array. To achieve that we can use the copy function:

arr1=arr.copy()
print(arr)
arr1[3:]=1000
print(arr1)

Now the change is not getting reflected in the original array:

[   0    1    2 1000 1000 1000 1000 1000 1000 1000]
[ 0 1 2 500 500 500 500 500 500 500]

If you want to know which elements are less than a particular number:

val=2
arr<2

But this does not tells us the values of the elements which are less the 2 . For that you can do:

arr[arr<2]

To make an array of ones:

np.ones(4)

To make an array of ones as an integer:

np.ones(4,dtype=int)

To make a 2-D array of ones as an float:

np.ones((2,5),dtype=float)

Random Distribution

To get a 2-D array of random elements in range of (0,1]:

np.random.rand(3,3)

To get integer values from the ‘standard normal distribution’:

arr_ex=np.random.randn(4,4)
arr_ex

To get 8 integer values from 0 to 100:

np.random.randint(0,100,8)

To get random floats in the half-open interval [0.0, 1.0):

np.random.random_sample((1,5))

That’s all forks! I hope you all enjoyed learning NumPy. Don’t forget to clap if you liked my blog and follow me to stay updated. Keep learning:)

--

--

Pavini Jain

Student at Jaypee Institute of Information Technology