In [2]:
import numpy as np

In [ ]:
# Operations between arrays and scalars
arr = np.array([[1.,2.,3.], [4.,5.,6.]])
print arr * arr
print arr - arr
print 1 / arr
print arr + arr
print arr ** 0.5

In [3]:
# Basic indexing and slicing
arr = np.arange(10)
print arr, arr[5:8]
arr[5:8]= 12
print arr, arr[5:8]
arr_slice = arr[5:8]
arr_slice[1] = 12345
print arr[1:6]
arr[:] = 64
print arr
print
arr2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
print arr2d
print arr2d[2]
print arr2d[0][2], arr2d[0,2]
print arr2d[:2, 1:]
print arr2d[:, :1]
print
arr3d = np.array([[[1,2,3], [4,5,6]], [[7,8,9], [10,11,12]]])
print arr3d[0]
old_values = arr3d[0].copy()
arr3d[0] = 42
print arr3d[0]
print old_values


[0 1 2 3 4 5 6 7 8 9] [5 6 7]
[ 0  1  2  3  4 12 12 12  8  9] [12 12 12]
[ 1  2  3  4 12]
[64 64 64 64 64 64 64 64 64 64]

[[1 2 3]
 [4 5 6]
 [7 8 9]]
[7 8 9]
3 3
[[2 3]
 [5 6]]
[[1]
 [4]
 [7]]

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

In [7]:
# Boolean indexing
# Selecting data from an array by boolean indexing always creates a copy of the data, even if the returned array is unchanged.
data = np.random.randn(7, 4)
print data
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
print names
print names == 'Bob'
print data[names == 'Bob']
print data[names == 'Bob', 2:]
print data[names == 'Bob', 3]
print names != 'Bob'
print data[~(names == 'Bob')]
# The Python keywords and and or do not work with boolean arrays.
mask = (names == 'Bob') | (names == 'Will')
print mask
print data[mask]
data[data < 0] = 0
print data
data[names != 'Joe'] = 7
print data


[[ 2.1229422   0.48249602 -1.12139272  0.01709879]
 [ 0.71612775 -1.44225785  1.10350876 -0.4682693 ]
 [-0.69920819  0.42513066 -1.52922816  0.84566331]
 [ 0.82652789  1.19748675 -0.40340838  0.87000364]
 [-0.12908329 -0.00481165  0.18203485 -0.11595965]
 [ 1.06642111  0.32111707 -0.32965962 -0.88518381]
 [-1.55728653 -0.57387906  0.7810224   0.9980635 ]]
['Bob' 'Joe' 'Will' 'Bob' 'Will' 'Joe' 'Joe']
[ True False False  True False False False]
[[ 2.1229422   0.48249602 -1.12139272  0.01709879]
 [ 0.82652789  1.19748675 -0.40340838  0.87000364]]
[[-1.12139272  0.01709879]
 [-0.40340838  0.87000364]]
[ 0.01709879  0.87000364]
[False  True  True False  True  True  True]
[[ 0.71612775 -1.44225785  1.10350876 -0.4682693 ]
 [-0.69920819  0.42513066 -1.52922816  0.84566331]
 [-0.12908329 -0.00481165  0.18203485 -0.11595965]
 [ 1.06642111  0.32111707 -0.32965962 -0.88518381]
 [-1.55728653 -0.57387906  0.7810224   0.9980635 ]]
[ True False  True  True  True False False]
[[ 2.1229422   0.48249602 -1.12139272  0.01709879]
 [-0.69920819  0.42513066 -1.52922816  0.84566331]
 [ 0.82652789  1.19748675 -0.40340838  0.87000364]
 [-0.12908329 -0.00481165  0.18203485 -0.11595965]]
[[ 2.1229422   0.48249602  0.          0.01709879]
 [ 0.71612775  0.          1.10350876  0.        ]
 [ 0.          0.42513066  0.          0.84566331]
 [ 0.82652789  1.19748675  0.          0.87000364]
 [ 0.          0.          0.18203485  0.        ]
 [ 1.06642111  0.32111707  0.          0.        ]
 [ 0.          0.          0.7810224   0.9980635 ]]
[[ 7.          7.          7.          7.        ]
 [ 0.71612775  0.          1.10350876  0.        ]
 [ 7.          7.          7.          7.        ]
 [ 7.          7.          7.          7.        ]
 [ 7.          7.          7.          7.        ]
 [ 1.06642111  0.32111707  0.          0.        ]
 [ 0.          0.          0.7810224   0.9980635 ]]

In [23]:
# Fancy indexing
# Keep in mind that fancy indexing, unlike slicing, always copies the data into a new array.
arr = np.empty((8, 4))
print arr
for i in range(8):
    arr[i] = i
print arr
print arr[[4, 3, 0, 6]]
print arr[[-3, -5, -7]]
arr = np.arange(32).reshape((8, 4))
print arr
print arr[[1, 5, 7, 2], [0, 3, 1, 2]]
print arr[[1, 5, 7, 2]]
print arr[[1, 5, 7, 2]][:, [0, 3, 1, 2]]
print arr[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])]


[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
[[ 0.  0.  0.  0.]
 [ 1.  1.  1.  1.]
 [ 2.  2.  2.  2.]
 [ 3.  3.  3.  3.]
 [ 4.  4.  4.  4.]
 [ 5.  5.  5.  5.]
 [ 6.  6.  6.  6.]
 [ 7.  7.  7.  7.]]
[[ 4.  4.  4.  4.]
 [ 3.  3.  3.  3.]
 [ 0.  0.  0.  0.]
 [ 6.  6.  6.  6.]]
[[ 5.  5.  5.  5.]
 [ 3.  3.  3.  3.]
 [ 1.  1.  1.  1.]]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]
 [20 21 22 23]
 [24 25 26 27]
 [28 29 30 31]]
[ 4 23 29 10]
[[ 4  5  6  7]
 [20 21 22 23]
 [28 29 30 31]
 [ 8  9 10 11]]
[[ 4  7  5  6]
 [20 23 21 22]
 [28 31 29 30]
 [ 8 11  9 10]]
[[ 4  7  5  6]
 [20 23 21 22]
 [28 31 29 30]
 [ 8 11  9 10]]

In [33]:
# Transporting Arrays and Swapping Axes
arr = np.arange(15).reshape((3, 5))
print arr
arr = np.random.randn(6, 3)
print arr
print arr.T
print np.dot(arr.T, arr)
arr = np.arange(16).reshape((2, 2, 4))
print arr
print arr.transpose((1,0,2))
# swapaxes similarly returns a view on the data without making a copy.
print arr.swapaxes(1, 2)


[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
[[ 0.69762628 -0.68473496  3.18109229]
 [-1.14019494  1.18552392 -0.01580551]
 [-1.89280155 -1.48221945 -0.25303739]
 [ 0.24473843 -1.89445871 -0.04031904]
 [ 2.25316038  0.47207672 -0.3191349 ]
 [ 0.76614571  0.09239248  0.68721708]]
[[ 0.69762628 -1.14019494 -1.89280155  0.24473843  2.25316038  0.76614571]
 [-0.68473496  1.18552392 -1.48221945 -1.89445871  0.47207672  0.09239248]
 [ 3.18109229 -0.01580551 -0.25303739 -0.04031904 -0.3191349   0.68721708]]
[[ 11.09303249   1.64693361   2.51376319]
 [  1.64693361   7.89167     -1.83266567]
 [  2.51376319  -1.83266567  10.7593659 ]]
[[[ 0  1  2  3]
  [ 4  5  6  7]]

 [[ 8  9 10 11]
  [12 13 14 15]]]
[[[ 0  1  2  3]
  [ 8  9 10 11]]

 [[ 4  5  6  7]
  [12 13 14 15]]]
[[[ 0  4]
  [ 1  5]
  [ 2  6]
  [ 3  7]]

 [[ 8 12]
  [ 9 13]
  [10 14]
  [11 15]]]

In [5]:
# Universal Functions: Fast Element-wise Array Functions
# arr = np.arange(10)
# print np.sqrt(arr)
# print np.exp(arr)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
axes1 = fig.add_subplot(111)
?axes1.plot
line, = axes1.plot(np.random.rand(10))
print type(axes1)
print type(line)


<class 'matplotlib.axes._subplots.AxesSubplot'>
<class 'matplotlib.lines.Line2D'>