price = [5.99, 10.25, 2.0, 40.99, 5.60, 63.49] price*2 price+0.5 for i in range(len(price)): price[i] = price[i]*2 price[i] = price[i]+0.5 print price from numpy import * sin(3*pi/2)*exp(2) import numpy as np price = np.array([5.99, 10.25, 2.0, 40.99, 5.60, 63.49]) print type(price) price = price*2+0.5 print price x = np.linspace(-1,1,5) print x y = np.arange(0,1,0.2) print y # type your solution here #%load solutions/1D_array.py np.arange? A = np.array([[1,2.4,-13],[4.1,5,0],[7.2,8,9]]) print "A = ", A print type(A) print A.shape print A.dtype A = np.array([[1,2+4j,3j],[4j,5,6-5j],[7j,8,9+3j]]) # dtype=complex print A.conjugate() print A.size np.random.uniform(0,1,size=(5,5)) # 5 x 5 matrix with elements from a uniform distribution print np.zeros((3,4)) print np.ones((2,3)) print np.eye(3) print np.diag(x,0) print np.diag([2,3,4]) print np.random.random((2,2)) A = np.arange(1,10) print "A = ", A A = A.reshape((3,3)) print A B = arange(15).reshape((3,5)) print "B = ", B print B.ravel() # flatten the array print A print A[0,0] print A[1:3,0:2] print A[1:3,[0,1]] print A print A[0,:] # row selection print A[:,0] # column selection print A[:,1:] # all rows, from 2nd column until the end print A[-1,:] # last row, all columns A[:,2] = [.3,.4,9.12] print A print A.dtype A = A.astype(float) A[:,2] = [.3,.4,9.12] print A a = arange(10) print a print a[: :-1] # reverse a b = a>5 print b a[b] = 0 # All elements of 'a' higher than 5 become 0 print a print a**2 print 10*sin(np.pi/a[1:5]) # avoiding division with zero! print A*A np.dot(A,A) A = array(A,dtype='int') print A print A.sum() # sum of all elements print A.min() print A.max() print A.sum(axis=0) # sum of each column print A.min(axis=1) # min of each row print A.cumsum(axis=1) # cumulative sum along each row A = floor(10*random.random((2,2))) print A B = floor(10*random.random((2,2))) print B print np.vstack((A,B)) print np.hstack((B,A)) A = floor(10*random.random((2,12))) print A print np.hsplit(A,3) # Split A into 3 arrays print np.hsplit(A,(3,4)) # Split A after the third and the fourth column # type your solution here #%load solutions/array_operations.py import os filename = 'data/stockholm_temp.txt' if os.path.exists(filename): # look at the first 3 lines with open(filename,'r') as f: print '\n'.join(f.readlines()[:3]) # alternative look at the first head lines !head 'data/stockholm_temp.txt' data = np.loadtxt(filename) # alternative we can use getfromtxt command else: print 'Weather data does not exist, please download "stockholm_temp.txt" from Github.' dt = np.dtype([('Year', 'int16'), ('Month', 'int8'), ('Day', 'int8'), ('Temp', 'float64')]) data = np.loadtxt(filename,dtype=dt) data[:10] # first 10 entries of our data data['Temp'] years = np.unique(data['Year']) # making an array with each year appearing only once leap_years = np.zeros((1,),dtype='int16') # initializing array for leap years for i in range(len(years)): if np.fmod(years[i],4)==0 and ((np.fmod(years[i],100)==0 and np.fmod(years[i],400)==0) or np.fmod(years[i],100)!=0): leap_years = np.append(leap_years,years[i]) # adding a leap year leap_years = leap_years[1:] # removing dummy first value print leap_years # type your solution here #%load solutions/temperatures.py x = np.random.random(10) print x np.linalg.norm(x,np.inf) # maximum norm of a vector A = np.array([[0,2],[8,0]]) b = np.array([1,2]) print A print b x = np.linalg.solve(A,b) print x lamda,V = np.linalg.eig(A) print lamda print V # type your solution here #%load solutions/linear_algebra.py from scipy import * from scipy import special #help(special) from scipy.special import jn, jn_zeros x = 0.0 n = 0 # order # Bessel function of first kind, zero order print "J_%d(%f) = %f" % (n, x, jn(n, x)) n = 1 # order # Bessel function of first kind, first order print "J_%d(%f) = %f" % (n, x, jn(n, x)) # The next two %pylab inline --no-import-all import matplotlib.pyplot as plt x = linspace(0, 12, 100) fig, ax = plt.subplots() ax.plot(x,zeros(len(x)),'--k') for n in range(4): ax.plot(x, jn(n, x), label=r"$J_%d(x)$" % n) ax.legend(); # zeros of Bessel functions n = 0 # order m = 4 # number of roots to compute jn_zeros(n, m) from scipy.integrate import quad xl = 0 # the lower limit of x xu = 10 # the upper limit of x val, abserr = quad(lambda x: jn(0,x), xl, xu) print "integral value =", val, ", absolute error =", abserr # type your solution here #%load solutions/bessel_integral.py from scipy.interpolate import interp1d x = np.linspace(0, 20, 20) y = lambda x: jn(5,x) # sampled on given values x f1 = interp1d(x, y(x)) # linear interpolation f2 = interp1d(x, y(x), kind='quadratic') f3 = interp1d(x, y(x), kind='cubic') # new points where we want to evaluate the interpoland xnew = np.linspace(0, 20, 100) fig, ax = plt.subplots(figsize=(10,4)) plt.plot(x,y(x),'o',xnew,f1(xnew),'-', xnew, f2(xnew),'--',xnew, f3(xnew),'--',xnew,y(xnew),'k') plt.legend(['data', 'linear', 'quadratic','cubic','true'], loc='best') plt.show() from scipy.sparse import * # dense matrix M = array([[10,0,0,0], [0,.3,0,0], [0,2,1.5,0], [-3,0,0,100]]) print M # convert from dense to sparse M_sparse = csr_matrix(M); # or csc_matrix(M) print M_sparse print M_sparse print M_sparse.todense() A = lil_matrix((4,4)) # empty 4x4 sparse matrix using a linked list format A[0,0] = 1 A[1,1] = 3 A[2,2] = A[2,1] = 1 A[3,3] = A[3,0] = 1 A A = csr_matrix(A); A # if the rows are sparse A = csc_matrix(A); A # if the columns are sparse # type your solution here #%load solutions/sparse_matrices.py