# import library, standard convention import numpy as np # To see what's in a package, type the name, a period, then hit tab #np? #np. np.pi? # Some examples of numpy functions and "things": print(np.sqrt(4)) print(np.pi) # Not a function, just a variable print(np.sin(np.pi)) # A function on a variable :) arr1 = np.array([1, 2.3, 4]) print(type(arr1)) print(arr1.dtype) strarr = np.array([x for x in 'hello']) strarr.dtype [x for x in 'hello'] strarr arr2 = np.array([1, 2.56, 4], dtype=int) print(type(arr2)) print(arr2.dtype) print arr2 arr3 = np.ndarray((2, 3, 4), dtype=complex) # Notice : `ndarray`, not `array`! print(type(arr3)) ## what are the values in arr3?? arr3 print(arr3.shape) arr4 = np.arange(2, 5) print(arr4) arr5 = np.arange(1, 5, 2) print(arr5) arr6 = np.arange(2, 10, 2) print arr6 np.arange(10) np.arange? np.arange(0, 10.5, .5) A = np.arange(5) B = np.arange(5, 10) print 'A is ', A print 'B is ', B print (A+B) print(B-A) print(A*B) A_10 = np.ones(10) B_5 = np.zeros(5) print A_10, B_5 A_10 + B_5 A = np.arange(5) A+10 2*A A**2 A, B print A.dot(B) print np.dot(A, B) A.shape big_a = np.ones((2,5)) print big_a.shape big_a a = np.array([1,2]) b = np.array([4,5]) def angle_between_vectors(a,b): """ find the angle between two vectors """ norm_a = np.sqrt(a.dot(a)) norm_b = np.sqrt(b.dot(b)) ab_dot = a.dot(b) theta = np.arccos(ab_dot / (norm_a * norm_b)) return theta theta = angle_between_vectors(a,b) print theta ## create rotation matrix def rotation_matrix(theta): """ create a rotation matrix given theta""" rot = np.array([ [np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)], ]) return rot ab_rot = rotation_matrix(theta) print ab_rot ## we know the angle between a and b ## find a vector rotated by the same amount relative to c = [7,8,9], c = np.array([7,8]) new = c.dot(ab_rot) print angle_between_vectors(c,new) arr1 = np.array([1, 2, 3, 4, 5]) arr2 = np.array([1, 1, 3, 3, 5]) print(arr1 == arr2) c = (arr1 == arr2) print c.dtype print c.sum() arr1[c] for mybool, item in zip(c, arr1): if mybool: print item **2 arr2[c]**2 print(np.all(c)) print(c.all()) print(c.any()) a = np.array([1,2,3]) print a[0:2] a[1:2] c = np.random.rand(3,3) print(c) #print(c[1:,0:2]) #print a c[0,:] = a print c[0,:] = 42 print(c) myvals = np.ones(100) * 13 + np.random.random(100) print myvals # create A and slice to get upper corner # create code that generates a square array, and outputs upper 4th quarter A = np.arange(5, 10) print(A) print(A[[0, 2, 3]]) A[[0, 2, 3]] = 0 print(A) random = np.random A = np.array([random.randint(0, 10) for i in range(10)]) # Check out the list comprehension! print 'A', (A) A[A>5] = 5 print(A) stuff_lessthan5 = A[A<5] print stuff_lessthan5 b = np.array([4,5,6]) print (a) print (b) print (a > 2) print (a[a > 2]) print (b[a > 2]) b[a == 3] = 77 print(b) # There are handy ways to make arrays full of ones and zeros print(np.zeros(5)) print np.ones(5) print np.identity(5), '\n' A = np.arange(5)*2 print(A) B = range(5)*2 print(B) A = np.arange(5) + np.arange(5) print(A) B =range(5) + range(5) print(B) A = np.arange(5) B = A[0:1] B[0] = 42 print(A) """ A = range(5) B = A[0:1] B[0] = 42 print(A) """ B = A[:2].copy() print B B[0] = 0 print A ## create an array of 10 random numbers ## np.random.random? # create a view and change the first element in the view ## what is the value in the original ## now create a copy A = np.arange(16).reshape(4, 4) print(A) print(A.T) # transpose print(A.trace()) la = np.linalg A = np.matrix([[3, 2, -1], [2, -2, 4], [-1, .5, -1]]) B = np.array([1, -2, 0]) print(la.solve(A, B)) A = np.arange(1,10) print(np.log10(A)) # create list, and array ## calculate log on all elements in list (list comprehension) ## calculate log on all elements in array ## use timeit to compare processing time