Basic Numerical Computations With Numpy and More

In my last post (click here for the last post) I talked about some basic numpy operations like some numeric computations on arrays, indexing, slicing etc. If that is the beginning of your Numpy usage, you got an idea of how efficient Numpy is and how important Numpy library can be in data science for Python users. Today I am also talking about some more on Numpy. This post will be on about some mathematical operations mainly. Here is the official documentation for beginners .  

I am still trying to explain some operations here that might be helpful. 

x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
Created two arrays x and y.
In [1]:
print(x+y)
[5 7 9]
Adds x and y item wise. [1+4, 2+5, 3+6]
In [2]:
print(x - y)
[-3 -3 -3]
Subtracts x and y item wise. [1-4, 2-5, 3-6]
In [3]:
print(x * y)
[ 4 10 18]
In [4]:
print(x / y)
[0.25 0.4  0.5 ]
x**y shows when y is a power on x. 
In [5]:
print(x**y)
[  1  32 729]
this result comes from [1**4, 2**5, 3**6]
In [6]:
x.dot(y)  
32
Dot product 1*4 + 2*5 + 3*6
 
In [7]:
z = np.array([y, y**2])
z
array([[ 4,  5,  6],
       [16, 25, 36]])
Creates an array where second array is the squared of the first array.
In [8]:
z.shape
(2, 3)
z.shape says z has 2 rows and 3 columns. It’s a two dimensional array. 
In [9]:
z.T
array([[ 4, 16],
       [ 5, 25],
       [ 6, 36]])
z.T alters the row and column as shows here.
In [10]:
z.T.shape
Out[11]:
(3, 2)
a = np.array([-4, -2, 1, 3, 5])
Created a new array a
In [12]:
a.sum()
3
Here a.sum() sums up the array a.
In [13]:
a.max()
5
a.max() gets the biggest element in the array a.
In [14]:
a.min()
-4
Next a.mean() and a.std() will give you the mean and standard deviation respectively.
In [15]:
a.mean()
0.6
In [16]:
a.std()
3.2619012860600183
argmax function gets the index of the biggest element of the array and argmin function gets the index of the smallest element of the array as follows.
In [17]:
a.argmax()
4
In [18]:
a.argmin()
0

#numpy #python #dataanalytics #datascience #dataanalysis

Leave a Reply

Close Menu