Hi Guys,

Thanks for all your emails. In this note, we will be looking at Array Processing in Python.

This is part of lectures on Learning Python for Data Analysis and Visualization by Jose Portilla on Udemy.

In [3]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

 

In [4]:
points = np.arange(-5,5,0.01)

 

In [5]:
dx, dy = np.meshgrid(points,points)

 

In [6]:
dx

 

Out[6]:
array([[-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       ..., 
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99]])
In [7]:
dy

 

Out[7]:
array([[-5.  , -5.  , -5.  , ..., -5.  , -5.  , -5.  ],
       [-4.99, -4.99, -4.99, ..., -4.99, -4.99, -4.99],
       [-4.98, -4.98, -4.98, ..., -4.98, -4.98, -4.98],
       ..., 
       [ 4.97,  4.97,  4.97, ...,  4.97,  4.97,  4.97],
       [ 4.98,  4.98,  4.98, ...,  4.98,  4.98,  4.98],
       [ 4.99,  4.99,  4.99, ...,  4.99,  4.99,  4.99]])
In [8]:
z = (np.sin(dx) + np.sin(dy))
z

 

Out[8]:
array([[  1.91784855e+00,   1.92063718e+00,   1.92332964e+00, ...,
         -8.07710558e-03,  -5.48108704e-03,  -2.78862876e-03],
       [  1.92063718e+00,   1.92342581e+00,   1.92611827e+00, ...,
         -5.28847682e-03,  -2.69245827e-03,  -5.85087534e-14],
       [  1.92332964e+00,   1.92611827e+00,   1.92881072e+00, ...,
         -2.59601854e-03,  -5.63993297e-14,   2.69245827e-03],
       ..., 
       [ -8.07710558e-03,  -5.28847682e-03,  -2.59601854e-03, ...,
         -1.93400276e+00,  -1.93140674e+00,  -1.92871428e+00],
       [ -5.48108704e-03,  -2.69245827e-03,  -5.63993297e-14, ...,
         -1.93140674e+00,  -1.92881072e+00,  -1.92611827e+00],
       [ -2.78862876e-03,  -5.85087534e-14,   2.69245827e-03, ...,
         -1.92871428e+00,  -1.92611827e+00,  -1.92342581e+00]])
In [9]:
plt.imshow(z)

 

Out[9]:
<matplotlib.image.AxesImage at 0x1f770247630>
In [10]:
plt.imshow(z)
plt.colorbar()
plt.title('Plot of sin(x) + sin(y)')

 

Out[10]:
<matplotlib.text.Text at 0x1f7702c48d0>
In [11]:
#numpy where
A = np.array([1,2,3,4])
B = np.array([100,200,300,400])

 

In [12]:
condition = np.array([True, True, False, False])

 

In [13]:
answer = [(A_val if cond else B_val) for A_val, B_val, cond in zip(A, B, condition)]

 

In [14]:
answer

 

Out[14]:
[1, 2, 300, 400]
In [15]:
answer2 = np.where(condition,A,B)
answer2

 

Out[15]:
array([  1,   2, 300, 400])
In [16]:
from numpy.random import randn

 

In [19]:
arr = randn(5,5)
arr

 

Out[19]:
array([[ 1.93402005,  0.74248988, -0.25537526, -1.21949914,  0.4997014 ],
       [-1.25508836,  0.8212506 , -1.45533722, -0.62535018,  0.47306087],
       [-1.7655461 ,  0.8858738 ,  0.0649815 ,  0.19169146,  0.48796459],
       [-1.12309409,  1.17273383, -0.04079639, -1.40677238,  0.19283691],
       [ 0.07890262, -0.42802593,  1.14800099, -1.52640459, -0.81729372]])
In [20]:
np.where(arr<0,0,arr)

 

Out[20]:
array([[ 1.93402005,  0.74248988,  0.        ,  0.        ,  0.4997014 ],
       [ 0.        ,  0.8212506 ,  0.        ,  0.        ,  0.47306087],
       [ 0.        ,  0.8858738 ,  0.0649815 ,  0.19169146,  0.48796459],
       [ 0.        ,  1.17273383,  0.        ,  0.        ,  0.19283691],
       [ 0.07890262,  0.        ,  1.14800099,  0.        ,  0.        ]])
In [22]:
arr2 = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr2

 

Out[22]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
In [23]:
arr2.sum()

 

Out[23]:
45
In [24]:
arr2.sum(0)

 

Out[24]:
array([12, 15, 18])
In [29]:
arr2.sum(1)

 

Out[29]:
array([ 6, 15, 24])

 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *