Array Processing – Python Numpy – How to work with Arrays in Python
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.
1 2 3 |
import numpy as np import matplotlib.pyplot as plt %matplotlib inline |
1 |
points = np.arange(-5,5,0.01) |
1 |
dx, dy = np.meshgrid(points,points) |
1 |
dx |
1 |
dy |
1 2 |
z = (np.sin(dx) + np.sin(dy)) z |
1 |
plt.imshow(z) |
1 2 3 |
plt.imshow(z) plt.colorbar() plt.title('Plot of sin(x) + sin(y)') |
1 2 3 |
#numpy where A = np.array([1,2,3,4]) B = np.array([100,200,300,400]) |
1 |
condition = np.array([True, True, False, False]) |
1 |
answer = [(A_val if cond else B_val) for A_val, B_val, cond in zip(A, B, condition)] |
1 |
answer |
1 2 |
answer2 = np.where(condition,A,B) answer2 |
1 |
from numpy.random import randn |
1 2 |
arr = randn(5,5) arr |
1 |
np.where(arr<0,0,arr) |
1 2 |
arr2 = np.array([[1,2,3],[4,5,6],[7,8,9]]) arr2 |
1 |
arr2.sum() |
1 |
arr2.sum(0) |
1 |
arr2.sum(1) |