top of page

Beginners NumPy

I recently started learning Python and came across a topic called NumPy. As a beginner, I had lots of questions on What is NumPy? Why NumPy? Is NumPy different from Python?

Well, after spending a few hours on the internet I’ve got a better understanding of what NumPy is now. For the benefits of other beginners like me, I’m writing this blog. Without further ado, let’s get started…

So, what is NumPy?

It is basically a Python library used for working with multidimensional arrays which stands for NUMerical Python. We can perform any mathematical and logical operations on arrays using NumPy.

Why NumPy? Why not Python?

Firstly, NumPy is written in C and executes much faster whereas Python is a dynamic language that is first interpreted, converted, and then executed.

Python stores its data in the form of a list, whereas NumPy stores the data in the form of a multidimensional array, thus making the data retrieval faster.

Is NumPy different from Python?

Python was not designed for numerical computation. When the need aroused, NumPy was created with a little Python and more of C as source code.

The main data structure of NumPy is the ndarray (N-dimensional array), which is a homogenous array that makes the mathematical operation extremely easier to perform, unlike Python’s list.

NumPy’s table elements are all of the same type (usually numbers), indexed by tuples of integers.

For the demonstration, I’m using You can follow along by signing up into the above link and creating a new notebook by clicking the New Notebook option.

Remember, the foremost step is loading the NumPy Library.

import numpy as np

Performing some basics operations:

# to initialize array:
b= np.array([[1,2,1],[2,1,0]])
o/p:[[1 2 1]
 [2 1 0]]
#to get dimension of b
o/p:(2, 3)

#to get datatype of b

# to get size of b

#To replace a number
o/p:[[1 2 1]
 [2 4 8]
 [3 2 6]]
c[1,2] =20
o/p:[[ 1  2  1]
 [ 2  4 20]
 [ 3  2  6]]

#get specific row
o/p:array([1, 2, 1])

#get specific element[r,c]

#to generate a random integer matrix:
o/p:array([[0, 1, 2],
       [3, 2, 2],
       [2, 2, 0]])
#to generate identity matrix:
o/p:array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]]) 
#to change shape of array:
s = np.arange(0,20).reshape(5,4)
r = np_arr.ravel()
t = r.reshape(4,5)
o/p:[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
# Arithmetic operations: 
 x = np.array([[11,22,33],[4,5,6]])
 y = np.array([[1,2,3],[14,15,16]])
 z = x + y
 print (z)
 z = x - y
 print (z)
 z = x * y
 print (z)
 z = x / y
 print (z)


Play around on the different sets of arrays, apply different functions, and see how it works. I hope you had fun learning the basics of NumPy.

86 views0 comments

Recent Posts

See All

Snowflake is a cloud native database and can be hosted on any of the clouds namely AWS, GCP or Azure. While using snowflake we pay for only the storage we use and the compute which was actually used d

bottom of page