import numpy as np a = np.linspace(start = 1, stop = 10, num = 5) print a ''' Create an array from a list object ''' a = ([1.0, 2.0], [3.0, 4.0]) print 'The Type of a is:' print type(a) print '' a = np.array(a) print 'The type of a is now:' print type(a) print '' print 'The shape of a is:' print a.shape print '' print 'This is a:' print a print '' print 'The transpose of a:' print np.transpose(a) print '' print 'The inverse of a:' print np.linalg.inv(a) print '' b = np.zeros([2,2]) print 'The array b:' print b print '' c = np.ones([2,1]) print 'The array c:' print c print '' d = np.identity(2) print 'The array d:' print d print '' print 'The top left element of d' print d[0,0] print '' print 'The left column of d' print d[:,0] print '' print 'The bottom Right element of d:' print d[1,1] print 'Or alternatively' print d[-1,-1] a = np.linspace(0,5,6) print 'The array a:' print a print '' print 'Extracting elements of a:' print a[-1] print a[-2] print a[-3] print a[2] print a[1] print a[0] a = np.array([4, 2, 3, 1]) print 'The original array' print a print '' print 'We can sum it up' print a.sum() print '' print 'Find the mean' print a.mean() print '' print 'Find the max' print a.max() print '' print 'Find the standard deviation' print a.std() A = np.ones([2,2]) B = np.ones([2,2]) print 'Scalar multiplication' print A * 2 print '' print 'Addition of two matrices' print A + B print '' print 'Scalar addition' print A + 2 print '' print 'Bad matrix multiplication' print A * B print '' print 'Good matrix multiplication' print np.dot(A, B) print '' x = np.array([1,2,3]) y = np.array([3,2,1]) print 'The dot product of x and y' print np.dot(x,y) print '' A = np.array([[2,1],[3,2]]) b = np.array([1,1]) print 'The matrix A is' print A print '' print 'The vector b is:' print b print '' print 'Find the determinant of A' print np.linalg.det(A) print '' print 'Find the inverse of A' print np.linalg.inv(A) print '' print 'Solve Ax = b' print np.linalg.solve(A,b) print'' for i in range(4): print i iterate = 0 max_it = 4 while iterate < max_it: print iterate iterate += 1 x = 2 y = 3 print x < y print y < x if x < y: print 'x is less than y' else: print 'x is greater than y' import numpy as np from matplotlib import pyplot as plt def poly(x): return 1.0 + 2.0 * x + 3.0 * x ** 2.0 + 4.0 * x ** 3.0 xgrid = np.linspace(start = -5, stop = 5, num = 50) plt.plot(xgrid, poly(xgrid)) plt.show() ''' Changing the line width and colour. Also this is how your write a multi line comment! See!!! ''' plt.plot(xgrid, poly(xgrid), 'r-', linewidth = 4) plt.show() #This is a single line comment. #And this is how we change the line type plt.plot(xgrid, poly(xgrid), 'g--', linewidth = 4) plt.show() ''' What about adding labels and a title? ''' from matplotlib import pyplot as plt %matplotlib inline plt.plot(xgrid, poly(xgrid), 'g--', linewidth = 4, label ='f(x)') plt.plot(xgrid, xgrid**2, 'r-', linewidth = 4, label ='g(x)') plt.xlabel('x') plt.ylabel('y') plt.title('A Cubic Polynomial Function') plt.legend(loc = 'upper left') #there are many options for where the legend can go. plt.show() import scipy.stats as ss x = ss.norm.rvs(size = 1, loc = 1.0, scale = 2.0) print 'A random normal draw' print x print '' import numpy as np import scipy.stats as ss # import scipy.stats module under namespace ss. import matplotlib.pyplot as plt #import the pyplot module under namespace plt #So that the plots show up in the web browser %matplotlib inline x = ss.norm.rvs(size = 1000, loc = 1.0, scale = 2.0) grid = np.linspace(-6, 8, 30) plt.hist(x, bins = 40, normed = True) plt.plot(grid, ss.norm.pdf(grid, loc = 1.0, scale = 2.0),'r-', lw=4) plt.show() I = 1000 beta0 = 1.0 beta1 = 2.0 sigma = 3.0 epsilon = ss.norm.rvs(size = I, loc = 0.0, scale = sigma) x = ss.norm.rvs(size = I, loc = 0.0, scale = 1.0) y = beta0 + beta1 * x + epsilon beta1hat, beta0hat, r2, p_value, std_err = ss.linregress(x,y) fitted = beta0hat + beta1hat * x plt.scatter(x,y, label = 'Data') plt.plot(x, fitted, 'r-', linewidth = 4, label = 'Fitted Values') plt.xlabel('x') plt.ylabel('y') plt.title('Linear Regression') plt.legend(loc = 'lower right') plt.show() print 'Intercept Slope' print beta0hat, beta1hat