top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Writer's pictureRubash Mali

A Quick Introduction to Numpy

Numpy stands for Numbers in python and is a powerful library for processing multi-dimensional arrays. Numpy is popular for its unmatchable speed and flexibility. In this blog we will observe some common functionalities and techniques regarding Numpy.


1.Creating a Numpy array :- Numpy package is imported usually with an alias name np. After that we use the array function to create the desired nd array.

import numpy as np
a = np.array([0, 1, 2, 3, 4, 5])
print(type(a))
#Output
<class 'numpy.ndarray'>

Same can be done for multidimensional array also we can check the dimensionality using the ndim function.

arr_0d = np.array(42)
arr_1d=np.array([1,2,3])
arr_2d=np.array([[1,2,3,4],[5,6,7,8]])
print(arr_0d.ndim)
print(arr_1d.ndim)
print(arr_2d.ndim)
print(arr_3d.ndim)
#Output
0
1
2
3

While shape attribute returns a tuple with each index having the number of corresponding elements.

arr_2d=np.array([[1,2,3,4],[5,6,7,8]])
print(arr_2d.shape)
#Output
(2, 4)

Another popular way to create a numpy array is using arange function. Inside the arange function we can simply mention the end value (n) which gives us values from 0 to n-1

a = np.arange(5) # 0.... n-1
a
#Output
array([0, 1, 2, 3, 4])

Arange function also provides us more flexibilty ,we can define the start point endpoint (exclusive) and the increment / decrement.

b = np.arange(2, 30, 4) #start, end (exclusive), step
b
#Output
array([ 2,  6, 10, 14, 18, 22, 26])

2.Speed comparison:- As mentioned earlier in data science one has to deal with huge data and numpy is preferred to list. It is due to its unmatched speed. Below code snippet shows speed of creating a list and the same numpy array of 10000 numbers and finding their cube.