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]:
    
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]:
    
In [151]:
    
xs1=np.arange(-3, 3, 0.1)
plt.plot(xs1, [f(x) for x in xs1])
    
    Out[151]:
    
In [152]:
    
xs2=np.linspace(-2, 2, 100)
plt.plot(xs2, [f(x) for x in xs2])
    
    Out[152]:
    
In [153]:
    
# arithmetic works on ndarray returned from arange() and linspace()
plt.plot(xs2, xs2**2)
    
    Out[153]:
    
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]:
    
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')
    
    
    Out[232]:
    
In [157]:
    
w
    
    Out[157]:
In [158]:
    
a=np.array([3,2,5,1])
print a
print type(a)
    
    
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
    
    
In [160]:
    
plt.hist(2*np.random.randn(1e4))
    
    Out[160]:
    
In [161]:
    
a = np.eye(3)
print a
    
    
In [162]:
    
print a.dtype
print a.shape
    
    
In [163]:
    
a[1, :]
    
    Out[163]:
In [164]:
    
# the end index is exclusive
a[0:2, 0:2]
    
    Out[164]:
In [165]:
    
#assignment
a[0:2, 0:2] = 2
a
    
    Out[165]:
In [166]:
    
a[1:3, 1:3 ] = np.random.rand(2,2)
a
    
    Out[166]:
In [167]:
    
a >= 2
    
    Out[167]:
In [168]:
    
# boolean indexing with assignment using another array
a[a>=2] = np.array([4, 5, 6])
a
    
    Out[168]:
In [169]:
    
a+2*a
    
    Out[169]:
In [171]:
    
# element-wise product. Not matrix product
a*a
    
    Out[171]:
In [179]:
    
np.log(1+a)
    
    Out[179]:
In [199]:
    
a = np.ones((2,2))
print 'a:'
print a
b = np.array([[2],[3]])
print 'b:'
print b
    
    
In [209]:
    
a*b
    
    Out[209]:
In [211]:
    
print b.T
    
    
In [203]:
    
m = np.array([[1,2], [3,4]])
m
    
    Out[203]:
In [205]:
    
np.sum(m)
    
    Out[205]:
In [206]:
    
np.sum(m, 0)
    
    Out[206]:
In [207]:
    
np.sum(m, 1)
    
    Out[207]:
In [216]:
    
print 'm:'
print m
print 'b:'
print b
    
    
In [217]:
    
m*b
    
    Out[217]:
In [218]:
    
# note that b is a matrix (2d array). 
# The result is a 2d array.
np.dot(m, b)
    
    Out[218]:
In [226]:
    
# what if c is just an array
c = np.array([ b[0,0], b[1,0] ]);
c
    
    Out[226]:
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]:
In [227]:
    
    
In [ ]: