top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

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 https://www.kaggle.com/. 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]])
print(b)
o/p:[[1 2 1]
 [2 1 0]]
 
#to get dimension of b
b.ndim
o/p:(2, 3)

#to get datatype of b
b.dtype
o/p:dtype('int64')

# to get size of b
b.itemsize
o/p:8

#To replace a number
c=np.array([[1,2,1],[2,4,8],[3,2,6]])
print(c)
o/p:[[1 2 1]
 [2 4 8]
 [3 2 6]]
 
c[1,2] =20
print(c)
o/p:[[ 1  2  1]
 [ 2  4 20]
 [ 3  2  6]]

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

#get specific element[r,c]
c[1,2]
o/p:8

#to generate a random integer matrix:
np.random.randint(4,size=(3,3))
o/p:array([[0, 1, 2],
       [3, 2, 2],
       [2, 2, 0]])
       
#to generate identity matrix:
np.identity(4)
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()
print(r)
t = r.reshape(4,5)
print(t)     
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)

Conclusion:

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.


88 views0 comments

Recent Posts

See All
bottom of page