In [1]:
%matplotlib inline
import numpy as np

In [19]:
arr = np.array([[1,2,3],[4,5,6]], dtype=np.float64)

In [28]:
arr


Out[28]:
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

In [23]:
arr*arr


Out[23]:
array([[  1.,   4.,   9.],
       [ 16.,  25.,  36.]])

In [24]:
np.dot(arr.T,arr)


Out[24]:
array([[ 17.,  22.,  27.],
       [ 22.,  29.,  36.],
       [ 27.,  36.,  45.]])

In [25]:
np.dot(arr,arr.T)


Out[25]:
array([[ 14.,  32.],
       [ 32.,  77.]])

In [26]:
arr+5


Out[26]:
array([[  6.,   7.,   8.],
       [  9.,  10.,  11.]])

In [31]:
arr+np.array([[6],[7]]) # Broadcast along dim 1


Out[31]:
array([[  7.,   8.,   9.],
       [ 11.,  12.,  13.]])

In [32]:
arr+np.array([6,7,8]) # Breadcast along dim 0


Out[32]:
array([[  7.,   9.,  11.],
       [ 10.,  12.,  14.]])

In [49]:
# Slicing - always returns a view
arr[:,1:]


Out[49]:
array([[ 2.,  3.],
       [ 5.,  6.]])

In [34]:
# Boolean indexing - always creates a copy
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
data = np.random.randn(names.shape[0],4)

In [35]:
data


Out[35]:
array([[ 2.2171817 ,  0.67226976,  2.31712369,  0.49070477],
       [ 2.32763422,  0.08300936,  0.35822391, -1.50494927],
       [-0.89431731,  0.8288176 ,  0.27408803, -1.12127598],
       [ 0.50249704, -0.28295892,  1.63194931,  0.30591545],
       [ 0.04230235, -2.06103025, -0.18247306,  0.23332462],
       [-1.45881178,  2.58254017, -2.35010784,  0.50945329],
       [ 0.8353959 ,  0.11027689, -1.03667897, -0.2630439 ]])

In [36]:
names


Out[36]:
array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'], 
      dtype='|S4')

In [38]:
data[names=='Bob', :2]


Out[38]:
array([[ 2.2171817 ,  0.67226976],
       [ 0.50249704, -0.28295892]])

In [46]:
data[[4,2],[1,2]] # Fancy indexing - extract the specified elements


Out[46]:
array([-2.06103025,  0.27408803])

In [47]:
data[np.ix_([4,2], [1,2])] # Get the square region


Out[47]:
array([[-2.06103025, -0.18247306],
       [ 0.8288176 ,  0.27408803]])

In [50]:
# Vectorization
points = np.arange(-5,5,0.01)
xs, ys = np.meshgrid(points, points)

In [51]:
import matplotlib.pyplot as plt

In [52]:
z = np.sqrt( xs**2 + ys**2 )

In [112]:
# Alternative: no need for intermediate 2D arrays if use broadcasting
z2 = np.sqrt(points**2 + points[:,None]**2)

In [108]:
np.arange(4)+np.arange(4)[:,None]


Out[108]:
array([[0, 1, 2, 3],
       [1, 2, 3, 4],
       [2, 3, 4, 5],
       [3, 4, 5, 6]])

In [109]:
points.shape


Out[109]:
(1000,)

In [110]:
points.T.shape


Out[110]:
(1000,)

In [111]:
points[:,None].shape #Convert to column vector


Out[111]:
(1000, 1)

In [120]:
points[None,:].T.shape


Out[120]:
(1000, 1)

In [118]:
plt.imshow(z2, cmap=plt.cm.gray); plt.colorbar
plt.title("Image plot of $\sqrt{x^2 + y^2}$ for a grid of values")


Out[118]:
<matplotlib.text.Text at 0x7f10aaacfe90>

In [89]:
# Random walk - NB: using 
x = np.hstack((np.zeros((10,1)), np.random.normal(loc=0, scale=0.25, size=(10,100)).cumsum(1))).T
plt.plot(x)


Out[89]:
[<matplotlib.lines.Line2D at 0x7f10aaf0eb50>,
 <matplotlib.lines.Line2D at 0x7f10aaf0edd0>,
 <matplotlib.lines.Line2D at 0x7f10aaf1a050>,
 <matplotlib.lines.Line2D at 0x7f10aaf1a210>,
 <matplotlib.lines.Line2D at 0x7f10aaf1a3d0>,
 <matplotlib.lines.Line2D at 0x7f10aaf1a590>,
 <matplotlib.lines.Line2D at 0x7f10aaf1a750>,
 <matplotlib.lines.Line2D at 0x7f10aaf46190>,
 <matplotlib.lines.Line2D at 0x7f10aaf1aad0>,
 <matplotlib.lines.Line2D at 0x7f10aaf1ac90>]

In [92]:
x[:10,:5]


Out[92]:
array([[ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [-0.02288209, -0.05414666, -0.13648867,  0.56972125,  0.26997755],
       [-0.1517502 ,  0.21386738, -0.29196164,  0.33200461,  0.32402513],
       [-0.24815818,  0.47422752, -0.69644876,  0.02825518,  0.46701033],
       [-0.49072601,  0.8494735 , -0.64153827,  0.40120279,  0.23043877],
       [-0.63316226,  0.91179533, -0.57108858,  0.1927183 ,  0.32795271],
       [-0.06159424,  1.07599577, -0.91562608,  0.1814614 ,  0.1356199 ],
       [ 0.11703752,  1.27618662, -0.82662229,  0.37877727,  0.19524912],
       [-0.35589012,  1.60679983, -1.12278289,  0.50953823, -0.07081754],
       [-0.46063098,  1.76031973, -1.14535241,  0.55471056, -0.1504698 ]])

In [ ]: