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

In [148]:
plt.plot([1,2,3], [5,4, 6])


Out[148]:
[<matplotlib.lines.Line2D at 0x9b63090>]

In [1]:
#help(plt.plot)

In [150]:
def f(x):
    return x**2

xs = range(-4, 4)
plt.plot(xs, [f(x) for x in xs])


Out[150]:
[<matplotlib.lines.Line2D at 0x9b8d610>]

In [151]:
xs1=np.arange(-3, 3, 0.1)
plt.plot(xs1, [f(x) for x in xs1])


Out[151]:
[<matplotlib.lines.Line2D at 0x9ebae50>]

In [152]:
xs2=np.linspace(-2, 2, 100)
plt.plot(xs2, [f(x) for x in xs2])


Out[152]:
[<matplotlib.lines.Line2D at 0xa18e750>]

In [153]:
# arithmetic works on ndarray returned from arange() and linspace()
plt.plot(xs2, xs2**2)


Out[153]:
[<matplotlib.lines.Line2D at 0xa2b19d0>]

In [154]:
# linear regression
def f(x):
    return 2*X + 0.2*np.random.randn(x.size)

X = np.random.randn(100)
Y = f(X)

In [155]:
# plot X, Y
plt.rc('font', size=15)
plt.plot(X, Y, 'o')
plt.title('Training data')


Out[155]:
<matplotlib.text.Text at 0xa4da610>

In [232]:
# regression
w = np.dot(X,Y)/sum(X**2)
print 'w: ', w
dom = np.linspace(-3, 3, 100);
plt.plot(X, Y, 'o', label='training points')
plt.plot(dom, w*dom, '-r', linewidth=2, label='regression')
plt.legend(loc='upper left')
plt.xlabel('X')
plt.ylabel('Y')


w:  2.01589039623
Out[232]:
<matplotlib.text.Text at 0xb190f10>

In [157]:
w


Out[157]:
2.0158903962340133

ndarray


In [158]:
a=np.array([3,2,5,1])
print a
print type(a)


[3 2 5 1]
<type 'numpy.ndarray'>

In [159]:
# be careful about the array type. uint32 can't represent a negative number
b=np.array([2,1,-5], dtype=np.uint32)
print b


[         2          1 4294967291]

In [160]:
plt.hist(2*np.random.randn(1e4))


Out[160]:
(array([   18.,   124.,   604.,  1609.,  2743.,  2682.,  1575.,   529.,
          101.,    15.]),
 array([-7.33737047, -5.85578932, -4.37420817, -2.89262702, -1.41104587,
         0.07053528,  1.55211642,  3.03369757,  4.51527872,  5.99685987,
         7.47844102]),
 <a list of 10 Patch objects>)

In [161]:
a = np.eye(3)
print a


[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

In [162]:
print a.dtype
print a.shape


float64
(3, 3)

In [163]:
a[1, :]


Out[163]:
array([ 0.,  1.,  0.])

In [164]:
# the end index is exclusive
a[0:2, 0:2]


Out[164]:
array([[ 1.,  0.],
       [ 0.,  1.]])

In [165]:
#assignment
a[0:2, 0:2] = 2
a


Out[165]:
array([[ 2.,  2.,  0.],
       [ 2.,  2.,  0.],
       [ 0.,  0.,  1.]])

In [166]:
a[1:3, 1:3 ] = np.random.rand(2,2)
a


Out[166]:
array([[ 2.        ,  2.        ,  0.        ],
       [ 2.        ,  0.50949118,  0.82296396],
       [ 0.        ,  0.2216654 ,  0.85486547]])

Boolean indexing


In [167]:
a >= 2


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

In [168]:
# boolean indexing with assignment using another array
a[a>=2] = np.array([4, 5, 6])
a


Out[168]:
array([[ 4.        ,  5.        ,  0.        ],
       [ 6.        ,  0.50949118,  0.82296396],
       [ 0.        ,  0.2216654 ,  0.85486547]])

Operations


In [169]:
a+2*a


Out[169]:
array([[ 12.        ,  15.        ,   0.        ],
       [ 18.        ,   1.52847354,   2.46889187],
       [  0.        ,   0.66499621,   2.56459641]])

In [171]:
# element-wise product. Not matrix product
a*a


Out[171]:
array([[ 16.        ,  25.        ,   0.        ],
       [ 36.        ,   0.25958126,   0.67726967],
       [  0.        ,   0.04913555,   0.73079497]])

In [179]:
np.log(1+a)


Out[179]:
array([[ 1.60943791,  1.79175947,  0.        ],
       [ 1.94591015,  0.41177263,  0.60046372],
       [ 0.        ,  0.20021501,  0.61781217]])

Broadcasting array size


In [199]:
a = np.ones((2,2))
print 'a:'
print a
b = np.array([[2],[3]])
print 'b:'
print b


a:
[[ 1.  1.]
 [ 1.  1.]]
b:
[[2]
 [3]]

In [209]:
a*b


Out[209]:
array([[ 2.,  2.],
       [ 3.,  3.]])

In [211]:
print b.T


[[2 3]]

Aggregate


In [203]:
m = np.array([[1,2], [3,4]])
m


Out[203]:
array([[1, 2],
       [3, 4]])

In [205]:
np.sum(m)


Out[205]:
10

In [206]:
np.sum(m, 0)


Out[206]:
array([4, 6])

In [207]:
np.sum(m, 1)


Out[207]:
array([3, 7])

Matrix


In [216]:
print 'm:'
print m
print 'b:'
print b


m:
[[1 2]
 [3 4]]
b:
[[2]
 [3]]

In [217]:
m*b


Out[217]:
array([[ 2,  4],
       [ 9, 12]])

In [218]:
# note that b is a matrix (2d array). 
# The result is a 2d array.
np.dot(m, b)


Out[218]:
array([[ 8],
       [18]])

In [226]:
# what if c is just an array
c = np.array([ b[0,0], b[1,0] ]);
c


Out[226]:
array([2, 3])

In [224]:
# matrix product gives the same answer but the returned type is different.
# The result is as array.
np.dot(m , c)


Out[224]:
array([ 8, 18])

In [227]:


In [ ]: