In [1]:
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

In [14]:
dt = np.random.randn(10)
print dt


[-1.47413611  1.25314031  1.35615477  0.51794507  0.6027534  -0.4123575
 -0.45341072  0.63892764  1.07344602  1.53198561]

In [ ]:


In [20]:
t = np.array(xrange(6)).reshape(2,3)
print t


[[0 1 2]
 [3 4 5]]

In [21]:
t * 5


Out[21]:
array([[ 0,  5, 10],
       [15, 20, 25]])

In [22]:
t + t


Out[22]:
array([[ 0,  2,  4],
       [ 6,  8, 10]])

In [23]:
2*t + 5


Out[23]:
array([[ 5,  7,  9],
       [11, 13, 15]])

In [25]:
t.dtype


Out[25]:
dtype('int64')

In [26]:
arr2 = np.array(t)

In [27]:
arr2


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

In [29]:
arr2.ndim


Out[29]:
2

In [33]:
t2 = np.array(xrange(1,19))

In [34]:
t2.shape


Out[34]:
(18,)

In [37]:
t3 = t2.reshape(3,3,2)
print t3


[[[ 1  2]
  [ 3  4]
  [ 5  6]]

 [[ 7  8]
  [ 9 10]
  [11 12]]

 [[13 14]
  [15 16]
  [17 18]]]

In [38]:
t3.ndim


Out[38]:
3

In [39]:
np.zeros(10)


Out[39]:
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

In [40]:
np.ones(10)


Out[40]:
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])

In [41]:
np.arange(15)


Out[41]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])

In [42]:
arr = np.array(np.random.randn(10))

In [43]:
print arr


[-0.29640234  0.9398378  -1.24518929 -1.12373016  1.11054933 -2.02981932
 -0.01564012 -0.0370334  -0.06927109 -0.20109615]

In [44]:
arr.reshape(2,5)


Out[44]:
array([[-0.29640234,  0.9398378 , -1.24518929, -1.12373016,  1.11054933],
       [-2.02981932, -0.01564012, -0.0370334 , -0.06927109, -0.20109615]])

In [45]:
int_arr = arr.astype(np.int)

In [46]:
int_arr


Out[46]:
array([ 0,  0, -1, -1,  1, -2,  0,  0,  0,  0])

In [47]:
arr * arr


Out[47]:
array([  8.78543491e-02,   8.83295092e-01,   1.55049637e+00,
         1.26276947e+00,   1.23331981e+00,   4.12016646e+00,
         2.44613310e-04,   1.37147282e-03,   4.79848419e-03,
         4.04396606e-02])

In [48]:
arr = np.arange(10)

In [49]:
arr[5:8]


Out[49]:
array([5, 6, 7])

In [50]:
arr[:]=88

In [51]:
arr


Out[51]:
array([88, 88, 88, 88, 88, 88, 88, 88, 88, 88])

In [52]:
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

In [53]:
arr2d[2]


Out[53]:
array([7, 8, 9])

In [ ]:


In [54]:
arr2d[:2]


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

In [55]:
arr2d[:2, :1]


Out[55]:
array([[1],
       [4]])

In [57]:
arr2d.dtype


Out[57]:
dtype('int64')

In [58]:
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])

In [60]:
data = np.random.randn(7, 4)

In [61]:
print data


[[ 0.08252843  1.35361751 -0.32881569  0.20071368]
 [ 0.98127093 -0.12521473 -0.46544672 -0.40467531]
 [ 0.08931104  0.07220005  2.40272779 -0.52271605]
 [ 1.15114824 -0.03043028  0.23658547  0.47596508]
 [-0.78886958 -2.36637138 -0.69186572 -0.43742823]
 [ 0.45190007  0.97534734  0.15921104  0.05601124]
 [-0.79897652 -0.01952584  1.55890749  0.92383247]]

In [62]:
names=='Bob'


Out[62]:
array([ True, False, False,  True, False, False, False], dtype=bool)

In [65]:
print data[0]
data[0, names=='Bob']


[ 0.08252843  1.35361751 -0.32881569  0.20071368]
/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:2: VisibleDeprecationWarning: boolean index did not match indexed array along dimension 1; dimension is 4 but corresponding boolean dimension is 7
  from ipykernel import kernelapp as app
Out[65]:
array([ 0.08252843,  0.20071368])

In [66]:
#1st and 4th row only
data[names=='Bob']


Out[66]:
array([[ 0.08252843,  1.35361751, -0.32881569,  0.20071368],
       [ 1.15114824, -0.03043028,  0.23658547,  0.47596508]])

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


Out[67]:
array([[-0.32881569,  0.20071368],
       [ 0.23658547,  0.47596508]])

In [70]:
print (names!='Bob')
print -(names=='Bob')


[False  True  True False  True  True  True]
[False  True  True False  True  True  True]
/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:2: DeprecationWarning: numpy boolean negative, the `-` operator, is deprecated, use the `~` operator or the logical_not function instead.
  from ipykernel import kernelapp as app

In [71]:
data[-(names=='Bob')]


/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:1: DeprecationWarning: numpy boolean negative, the `-` operator, is deprecated, use the `~` operator or the logical_not function instead.
  if __name__ == '__main__':
Out[71]:
array([[ 0.98127093, -0.12521473, -0.46544672, -0.40467531],
       [ 0.08931104,  0.07220005,  2.40272779, -0.52271605],
       [-0.78886958, -2.36637138, -0.69186572, -0.43742823],
       [ 0.45190007,  0.97534734,  0.15921104,  0.05601124],
       [-0.79897652, -0.01952584,  1.55890749,  0.92383247]])

In [72]:
mask = (names == 'Bob') | (names == 'Will')

In [73]:
data[mask]


Out[73]:
array([[ 0.08252843,  1.35361751, -0.32881569,  0.20071368],
       [ 0.08931104,  0.07220005,  2.40272779, -0.52271605],
       [ 1.15114824, -0.03043028,  0.23658547,  0.47596508],
       [-0.78886958, -2.36637138, -0.69186572, -0.43742823]])

In [76]:
print data < 0


[[False False  True False]
 [False  True  True  True]
 [False False False  True]
 [False  True False False]
 [ True  True  True  True]
 [False False False False]
 [ True  True False False]]

In [77]:
data[data < 0 ]


Out[77]:
array([-0.32881569, -0.12521473, -0.46544672, -0.40467531, -0.52271605,
       -0.03043028, -0.78886958, -2.36637138, -0.69186572, -0.43742823,
       -0.79897652, -0.01952584])

In [78]:
d = data

In [79]:
data[data <0 ] = 0
print data


[[ 0.08252843  1.35361751  0.          0.20071368]
 [ 0.98127093  0.          0.          0.        ]
 [ 0.08931104  0.07220005  2.40272779  0.        ]
 [ 1.15114824  0.          0.23658547  0.47596508]
 [ 0.          0.          0.          0.        ]
 [ 0.45190007  0.97534734  0.15921104  0.05601124]
 [ 0.          0.          1.55890749  0.92383247]]

In [80]:
points = np.arange(-5, 5, 0.01) # 1000 equally spaced points

In [81]:
xs, ys = np.meshgrid(points, points)

In [82]:
xs.shape


Out[82]:
(1000, 1000)

In [83]:
ys.shape


Out[83]:
(1000, 1000)

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

In [85]:
z.shape


Out[85]:
(1000, 1000)

In [91]:
plt.imshow(z)


Out[91]:
<matplotlib.image.AxesImage at 0x7feaa87c0310>

In [92]:
plt.imshow(z, cmap=plt.cm.gray)
plt.colorbar()


Out[92]:
<matplotlib.colorbar.Colorbar at 0x7feaa8676590>

In [96]:
arr = np.random.randn(4, 4)
print arr


[[ 0.98467539 -0.64728288  1.49540818 -0.38771734]
 [ 0.31585595  0.40015662  0.40748191  1.2664255 ]
 [ 1.54410907 -0.29499871  0.22597569  0.57453404]
 [ 0.78824385  0.49671403 -2.06788967 -0.40420376]]

In [97]:
np.where(arr >0, 2, -3)


Out[97]:
array([[ 2, -3,  2, -3],
       [ 2,  2,  2,  2],
       [ 2, -3,  2,  2],
       [ 2,  2, -3, -3]])

In [98]:
np.where(arr>0, 2, arr)


Out[98]:
array([[ 2.        , -0.64728288,  2.        , -0.38771734],
       [ 2.        ,  2.        ,  2.        ,  2.        ],
       [ 2.        , -0.29499871,  2.        ,  2.        ],
       [ 2.        ,  2.        , -2.06788967, -0.40420376]])

In [99]:
arr = np.random.randn(5, 4) # normally-distributed data

In [100]:
arr


Out[100]:
array([[ 1.27342935,  1.09653315, -2.12119591, -1.67630388],
       [ 1.99856885, -0.30468642, -0.73306801, -1.09792069],
       [ 0.97935852,  1.105347  , -0.3279526 ,  0.274788  ],
       [ 0.2259718 ,  0.55485817,  0.5538221 , -2.26426719],
       [ 0.10594451,  0.06769698,  0.10206739,  0.41721803]])

In [101]:
arr.sum(axis=0)


Out[101]:
array([ 4.58327304,  2.51974887, -2.52632703, -4.34648573])

In [102]:
arr.sum(axis=1)


Out[102]:
array([-1.42753729, -0.13710628,  2.03154092, -0.92961512,  0.69292692])

In [103]:
arr.mean(axis=1)


Out[103]:
array([-0.35688432, -0.03427657,  0.50788523, -0.23240378,  0.17323173])

In [104]:
arr.mean(axis=0)


Out[104]:
array([ 0.91665461,  0.50394977, -0.50526541, -0.86929715])

In [106]:
arr.std()


Out[106]:
1.1026695315031099

In [107]:
arr.var()


Out[107]:
1.215880095705288

In [110]:
print arr
print arr.argmax()
print arr.argmin()


[[ 1.27342935  1.09653315 -2.12119591 -1.67630388]
 [ 1.99856885 -0.30468642 -0.73306801 -1.09792069]
 [ 0.97935852  1.105347   -0.3279526   0.274788  ]
 [ 0.2259718   0.55485817  0.5538221  -2.26426719]
 [ 0.10594451  0.06769698  0.10206739  0.41721803]]
4
15

In [123]:
arr = np.random.randn(100)
from numpy.random import randn

In [115]:
#how many -ve numbers?
(arr<0).sum()


Out[115]:
57

In [132]:
arr = randn(5, 3)
print arr


[[ 0.37761796 -0.83777419  1.38490965]
 [ 0.58248771  1.42123275  0.28478795]
 [-2.3535501   1.41924088 -0.28110151]
 [-0.66033687  0.93858937  0.63907083]
 [-0.65385116  1.08904642  0.93646077]]

In [134]:
arr.sort(axis=1)
print arr


[[-2.3535501  -0.83777419 -0.28110151]
 [-0.66033687  0.28478795  0.93858937]
 [-0.65385116  0.63907083  1.08904642]
 [ 0.37761796  0.93646077  1.41924088]
 [ 0.58248771  1.38490965  1.42123275]]

In [133]:
arr.sort(axis=0)
print arr


[[-2.3535501  -0.83777419 -0.28110151]
 [-0.66033687  0.93858937  0.28478795]
 [-0.65385116  1.08904642  0.63907083]
 [ 0.37761796  1.41924088  0.93646077]
 [ 0.58248771  1.42123275  1.38490965]]

In [137]:
#Random number generation
print np.random.normal(size=(4,4))


[[-0.63061263  1.21687635 -0.41961929  0.99951535]
 [ 1.54093049  0.61569953  1.01267614 -0.30879977]
 [-0.46570844 -0.27729251 -1.05490937  0.96101692]
 [-1.66549154  0.22143668  1.85829296  1.40564922]]

In [ ]: