NumPy Cheat Sheet Python For Data Science Learn NumPy online at www.DataCamp.com Saving & Loading On Disk Saving & Loading Text Files >>> np.save( , a) >>> np.savez( ', a, b) >>> np.load( ) 'my_array' 'array.npz 'my_array.npy' >>> np.loadtxt( ) >>> np.genfromtxt( , delimiter= ) >>> np.savetxt( , a, delimiter= ) "myfile.txt" "my_file.csv" ',' "myarray.txt" " " > I/O Numpy >>> np.int64 >>> np.float32 >>> np.complex >>> np.bool >>> np.object >>> np.string_ >>> np.unicode_ #Signed 64-bit integer types #Standard double-precision floating point #Complex numbers represented by 128 floats #Boolean type storing TRUE and FALSE values #Python object type #Fixed-length string type #Fixed-length unicode type > Data Types >>> np.info(np.ndarray.dtype) > Asking For Help Arithmetic Operations Comparison Aggregate Functions >>> g = a b array([[-0.5, 0. , 0. ], [-3. , -3. , -3. ]]) >>> np.subtract(a,b) >>> b a array([[ 2.5, 4. , 6. ], [ 5. , 7. , 9. ]]) >>> np.add(b,a) >>> a b array([[ 0.66666667, 1. , 1. ], [ 0.25 , 0.4 , 0.5 ]]) >>> np.divide(a,b) >>> a b array([[ 1.5, 4. , 9. ], [ 4. , 10. , 18. ]]) >>> np.multiply(a,b) >>> np.exp(b) >>> np.sqrt(b) >>> np.sin(a) >>> np.cos(b) >>> np.log(a) >>> e.dot(f) array([[ 7., 7.], [ 7., 7.]]) - + / * #Subtraction #Subtraction #Addition Addition #Division #Division #Multiplication #Multiplication #Exponentiation #Square root #Print sines of an array #Element-wise cosine #Element-wise natural logarithm #Dot product >>> a == b array([[ , , ], [ , , ]], dtype=bool) >>> a < 2 array([ , , ], dtype=bool) >>> np.array_equal(a, b) #Element-wise comparison #Element-wise comparison #Array-wise comparison False True True False False False True False False >>> a.sum() >>> a.min() >>> b.max(axis=0) >>> b.cumsum(axis=1) >>> a.mean() >>> np.median(b) >>> np.corrcoef(a) >>> np.std(b) #Array-wise sum #Array-wise minimum value #Maximum value of an array row #Cumulative sum of the elements #Mean #Median #Correlation coefficient #Standard deviation > Array Mathematics Numpy The NumPy library is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays NumPy Arrays >>> import numpy as np Use the following import convention: Initial Placeholders >>> a = np.array([1,2,3]) >>> b = np.array([(1.5,2,3), (4,5,6)], dtype = float) >>> c = np.array([[(1.5,2,3), (4,5,6)],[(3,2,1), (4,5,6)]], dtype = float) >>> np.zeros((3,4)) >>> np.ones((2,3,4),dtype=np.int16) >>> d = np.arange(10,25,5) >>> np.linspace(0,2,9) >>> e = np.full((2,2),7) >>> f = np.eye(2) >>> np.random.random((2,2)) >>> np.empty((3,2)) #Create an array of zeros #Create an array of ones #Create an array of evenly spaced values (step value) #Create an array of evenly spaced values (number of samples) #Create a constant array #Create a 2X2 identity matrix #Create an array with random values #Create an empty array > Creating Arrays >>> h = a.view() >>> np.copy(a) >>> h = a.copy() #Create a view of the array with the same data #Create a copy of the array #Create a deep copy of the array >>> a[2] 3 >>> b[1,2] 6.0 #Select the element at the 2nd index #Select the element at row 1 column 2 (equivalent to b[1][2]) >>> a[0:2] array([1, 2]) >>> b[0:2,1] array([ 2., 5.]) >>> b[:1] array([[1.5, 2., 3.]]) >>> c[1,...] array([[[ 3., 2., 1.], [ 4., 5., 6.]]]) >>> a[ : :-1] #Select items at index 0 and 1 #Select items at rows 0 and 1 in column 1 #Select all items at row 0 (equivalent to b[0:1, :]) #Same as [1,:,:] #Reversed array a array([3, 2, 1]) >>> a[a<2] array([1]) #Select elements from a less than 2 >>> b[[1, 0, 1, 0],[0, 1, 2, 0]] array([ 4. , 2. , 6. , 1.5]) >>> b[[1, 0, 1, 0]][:,[0,1,2,0]] array([[ 4. ,5. , 6. , 4. ], [ 1.5, 2. , 3. , 1.5], [ 4. , 5. , 6. , 4. ], [ 1.5, 2. , 3. , 1.5]]) #Select elements (1,0),(0,1),(1,2) and (0,0) #Select a subset of the matrix’s rows and columns >>> a.sort() >>> c.sort(axis=0) #Sort an array #Sort the elements of an array's axis >>> a.shape >>> len(a) >>> b.ndim >>> e.size >>> b.dtype >>> b.dtype.name >>> b.astype(int) #Array dimensions #Length of array #Number of array dimensions #Number of array elements #Data type of array elements #Name of data type #Convert an array to a different type > Inspecting Your Array > Copying Arrays > Sorting Arrays > Subsetting, Slicing, Indexing Subsetting Slicing Boolean Indexing Fancy Indexing >>> i = np.transpose(b) >>> i.T #Permute array dimensions #Permute array dimensions >>> b.ravel() >>> g.reshape(3,-2) #Flatten the array #Reshape, but don’t change data >>> h.resize((2,6)) >>> np.append(h,g) >>> np.insert(a, 1, 5) >>> np.delete(a,[1]) #Return a new array with shape (2,6) #Append items to an array #Insert items in an array #Delete items from an array >>> np.concatenate((a,d),axis=0) array([ 1, 2, 3, 10, 15, 20]) >>> np.vstack((a,b)) array([[ 1. , 2. , 3. ], [ 1.5, 2. , 3. ], [ 4. , 5. , 6. ]]) >>> np.r_[e,f] >>> np.hstack((e,f)) array([[ 7., 7., 1., 0.], [ 7., 7., 0., 1.]]) >>> np.column_stack((a,d)) array([[ 1, 10], [ 2, 15], [ 3, 20]]) >>> np.c_[a,d] #Concatenate arrays #Stack arrays vertically (row-wise) #Stack arrays vertically (row-wise) #Stack arrays horizontally (column-wise) #Create stacked column-wise arrays #Create stacked column-wise arrays >>> np.hsplit(a,3) [array([1]),array([2]),array([3])] >>> np.vsplit(c,2) [array([[[ 1.5, 2. , 1. ], [ 4. , 5. , 6. ]]]), array([[[ 3., 2., 3.], [ 4., 5., 6.]]])] #Split the array horizontally at the 3rd index #Split the array vertically at the 2nd index > Array Manipulation Transposing Array Changing Array Shape Adding/Removing Elements Combining Arrays Splitting Arrays Learn Data Skills Online at www.DataCamp.com 1 2 3 1 2 3 1 2 3 1.5 4 2 5 3 6 1.5 4 2 5 3 6 1.5 4 2 5 3 6