# # Numpy introduction 2 (walkthrough) # # using arange to get evenly spaced values in an array: import numpy as np a = np.arange(1, 10) print(a) # compare to range: x = range(1,10) print(x) # x is an iterator print(list(x)) # some more arange examples: x = np.arange(10.4) print(x) x = np.arange(0.5, 10.4, 0.8) print(x) x = np.arange(0.5, 10.4, 0.8, int) print(x) # another related function: linspace # 50 values between 1 and 10: print(np.linspace(1, 10)) # 7 values between 1 and 10: print(np.linspace(1, 10, 7)) # excluding the endpoint: print(np.linspace(1, 10, 7, endpoint=False)) # dimension of arrays: x = np.array(42) print("x: ", x) print("The type of x: ", type(x)) print("The dimension of x:", np.ndim(x)) F = np.array([1, 1, 2, 3, 5, 8, 13, 21]) V = np.array([3.4, 6.9, 99.8, 12.8]) print("F: ", F) print("V: ", V) print("Type of F: ", F.dtype) print("Type of V: ", V.dtype) print("Dimension of F: ", np.ndim(F)) print("Dimension of V: ", np.ndim(V)) A = np.array([ [3.4, 8.7, 9.9], [1.1, -7.8, -0.7], [4.1, 12.3, 4.8]]) print(A) print(A.ndim) # shape of arrays: print(x.shape) print(F.shape) print(V.shape) print(A.shape) # reshaping: x = np.array([ [67, 63, 87], [77, 69, 59], [85, 87, 99], [79, 72, 71], [63, 89, 93], [68, 92, 78]]) print(np.shape(x)) print(x.shape) #an equivalent array property x.shape = (3, 6) #reshaped! print(x) x.shape = (4,4) #whoops, error! # more on array indexing: F = np.array([1, 1, 2, 3, 5, 8, 13, 21]) # print the first element of F, i.e. the element with the index 0 print(F[0]) # print the last element of F print(F[-1]) B = np.array([ [[111, 112], [121, 122]], [[211, 212], [221, 222]], [[311, 312], [321, 322]] ]) print(B[0][1][0]) A = np.array([ [3.4, 8.7, 9.9], [1.1, -7.8, -0.7], [4.1, 12.3, 4.8]]) print(A[1][0]) print(A[1, 0]) # equivalent syntax # more slicing examples S = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) print(S[2:5]) print(S[:4]) print(S[6:]) print(S[:]) # multidimensional slicing: A = np.array([ [11,12,13,14,15], [21,22,23,24,25], [31,32,33,34,35], [41,42,43,44,45], [51,52,53,54,55]]) print(A[:3,2:]) print(A[3:,:]) print(A[:,4:]) X = np.arange(28).reshape(4,7) print(X) print(X[::2, ::3]) # uses the 'step' parameter print(X[::, ::3]) Z = np.zeros((8,8),dtype=int) Z[1::2,::2] = 1 Z[::2,1::2] = 1 print(Z) # shortcuts for array creation: E = np.ones((2,3)) print(E) F = np.ones((3,4),dtype=int) print(F) Z = np.zeros((2,4)) print(Z) x = np.array([2,5,18,14,4]) E = np.ones_like(x) print(E) I = np.identity(4) print(I) J = np.eye(5, 8, k=1, dtype=int) print(J) # Broadcasting: # We will add the vector v to each row of the matrix x, # storing the result in the matrix y x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]]) v = np.array([1, 0, 1]) y = np.empty_like(x) # Create an empty matrix with the same shape as x # Add the vector v to each row of the matrix x with an explicit loop (SLOW!) for i in range(4): y[i, :] = x[i, :] + v # Now y is the following # [[ 2 2 4] # [ 5 5 7] # [ 8 8 10] # [11 11 13]] print y # We will add the vector v to each row of the matrix x, # storing the result in the matrix y x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]]) v = np.array([1, 0, 1]) y = x + v # Add v to each row of x using broadcasting print y # Prints "[[ 2 2 4] # [ 5 5 7] # [ 8 8 10] # [11 11 13]]"