NumPy Guide


NumPy is a Python library used for working with arrays. NumPy (short form of ‘Numerical Python’) is a python module which is used in domain of linear algebra, matrices, fourier transform and in Data science field. NumPy arrays are faster than Python Lists because they provide an array object. An array object is called ndarray in NumPy.

PROUNOUNCED AS: Numb-Pye. Usually I prounce it as Numpee.


What is Numpy?

It is creating a Homogeneous n-dimensional arrays (n = 1..n).

Think about real life Example- our day today life where we buy a produce in larger quantities like for example -a box of croissant or eggs from Costco. They come in big box of trays, so these trays are homogeneous because size will be huge it’s like 30 eggs in a tray, so think it as arrays which has rows & columns.


Why use NumPy?

In Python we have lists that serve the purpose of arrays, but they are slow to process. NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.

The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with ndarray very easy.

Arrays are very frequently used in data science, where speed and resources are very important.


Why NumPy is faster than the List?

NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently.

This behavior is called locality of reference. This is the main reason why NumPy is faster than lists. Also, it is optimized to work with latest CPU architectures

Installation of NumPy


To start working with NumPy Arrays, we must first install the NumPy package as it doesn't come with basic Python by default. Use pip to install NumPy package on your command prompt.


pip install numpy


Import NumPy

Once NumPy is installed, import it in your applications by adding the import keyword:

import numpy

Now NumPy is imported and ready to use.

Example

import numpy arr = numpy.array([1, 2, 3, 4, 5]) print(arr)


Create an alias with the as keyword while importing:

import numpy as np


Example

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


Output:

[1 2 3 4 5]


Creating 1-D array is same the above ie


Create a 1-D array containing the values 1,2,3,4,5:


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


Output:

[1 2 3 4 5]


Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:

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

Output:

[[1 2 3]

[4 5 6]]


np.arrange() What arrange([start],stop,[step]) does is that it arranges numbers

from starting to stop, in steps of step. Here is what it means for np.arrange (0,10,2) Output: array([0, 2, 4, 6, 8]) np.zeros() What will np.zeros(4,3) do Output: array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]]) reshape() It is a method that changes the dimensions of your original matrix

into your desired dimension. B = [1, 2, 3, 4, 5, 6, 7, 8, 9] B.reshape(3,3) Output: array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) np.sum() It adds up all the elements of the array array([[ 1., 2., 3.], [ 4., 5., 6.], [ 7., 8., 9.]]) np.sum() Output: 45

To add or sum along the rows # sum along the rows np.sum(M,axis=1) Output: array([ 6., 15., 24.]) 6 is the sum of 1st row (1, 2, 3). 15 is the sum of 2nd row (4, 5, 6). 24 is the sum of 3rd row (7, 8, 9).

Sum along the columns # sum along the cols np.sum(M,axis=0) Output: array([ 12., 15., 18.]) 12 is the sum of 1st col (1, 4, 7). 15 is the sum of 2nd col (2, 5, 8). 18 is the sum of 3rd col (3, 6, 9). Access the 2nd element on 1st dim: import numpy as np arr = np.array([[1,2,3,4,5], [6,7,8,9,10]]) print('2nd element on 1st dim: ', arr[0, 1]) Output: 2 Access the 5th element on 2nd dim: import numpy as np arr = np.array([[1,2,3,4,5], [6,7,8,9,10]]) print('5th element on 2nd dim: ', arr[1, 4]) Output: 10


Slicing arrays


Slicing in python means taking elements from one given index to another given index.

We pass slice instead of index like this: [start:end].

We can also define the step, like this: [start:end:step].

If we don't pass start its considered 0

If we don't pass end its considered length of array in that dimension

If we don't pass step its considered 1

import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[1:5])

Output:

[2,3,4,5]


Slice elements from index 4 to the end of the array:


import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[4:])


Output:

[5,6,7]


Slice elements from the beginning to index 4 (not included):


import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[:4])


Output:

[1,2,3,4]


Data Types in NumPy

NumPy has some extra data types, and refer to data types with one character, like i for integers, u for unsigned integers etc.

Below is a list of all data types in NumPy and the characters used to represent them.

  • i - integer

  • b - boolean

  • u - unsigned integer

  • f - float

  • c - complex float

  • m - timedelta

  • M - datetime

  • O - object

  • S - string

  • U - unicode string

  • V - fixed chunk of memory for other type (void)

Checking the Data Type of an Array

The NumPy array object has a property called dtype that returns the data type of the array:

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

Output:

Int64


There are many more functions like copy, shape, view and re shape and many more..

I have here attached the link for cheap sheet too.


https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Numpy_Python_Cheat_Sheet.pdf


Conclusion:


In this blog I have explained What is NumPy? And its uses and certain important and basic commands that NumPy can perform. Hope this blog was helpful and you enjoyed reading…

If you could appreciate the blog with claps it would be more encouraging me to write more blogs.

21 views0 comments

Recent Posts

See All

Mac - Step by Step - Install Eclipse, TestNg, Maven

Install Eclipse: Goto https://www.eclipse.org/downloads/packages/ Click on: Download Eclipse IDE for Java Developers Unzip the downloaded zip file and double click eclipse.exe. Choose a folder to set