《可视化传播PythonCheatSheet (5).pdf》由会员分享,可在线阅读,更多相关《可视化传播PythonCheatSheet (5).pdf(1页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、 2Python For Data Science Cheat SheetNumPy BasicsLearn Python for Data Science Interactively at www.DataCNumPyDataCampLearn Python for Data Science InteractivelyThe NumPy library is the core library for scientific computing in Python.It provides a high-performance multidimensional array object,and t
2、ools for working with these arrays.import numpy as npUse the following import convention:Creating Arrays np.zeros(3,4)Create an array of zeros np.ones(2,3,4),dtype=np.int16)Create an array of ones d=np.arange(10,25,5)Create an array of evenly spaced values(step value)np.linspace(0,2,9)Create an arra
3、y of evenly spaced values(number of samples)e=np.full(2,2),7)Create a constant array f=np.eye(2)Create a 2X2 identity matrix np.random.random(2,2)Create an array with random values np.empty(3,2)Create an empty arrayArray Mathematics g=a-b Subtraction array(-0.5,0.,0.,-3.,-3.,-3.)np.subtract(a,b)Subt
4、raction b+a Addition array(2.5,4.,6.,5.,7.,9.)np.add(b,a)Addition a/b Division array(0.66666667,1.,1.,0.25 ,0.4 ,0.5 )np.divide(a,b)Division a*b Multiplication array(1.5,4.,9.,4.,10.,18.)np.multiply(a,b)Multiplication np.exp(b)Exponentiation np.sqrt(b)Square root np.sin(a)Print sines of an array np.
5、cos(b)Element-wise cosine np.log(a)Element-wise natural logarithm e.dot(f)Dot product array(7.,7.,7.,7.)Subsetting,Slicing,Indexing a.sum()Array-wise sum a.min()Array-wise minimum value b.max(axis=0)Maximum value of an array row b.cumsum(axis=1)Cumulative sum of the elements a.mean()Mean b.median()M
6、edian a.corrcoef()Correlation coefficient np.std(b)Standard deviationComparison a=b Element-wise comparison array(False,True,True,False,False,False,dtype=bool)a np.array_equal(a,b)Array-wise comparison1 231D array 2D array 3D array1.5 234 56Array ManipulationNumPy Arraysaxis 0axis 1axis 0axis 1axis
7、2Arithmetic Operations Transposing Array i=np.transpose(b)Permute array dimensions i.T Permute array dimensions Changing Array Shape b.ravel()Flatten the array g.reshape(3,-2)Reshape,but dont change data Adding/Removing Elements h.resize(2,6)Return a new array with shape(2,6)np.append(h,g)Append ite
8、ms to an array np.insert(a,1,5)Insert items in an array np.delete(a,1)Delete items from an array Combining Arrays np.concatenate(a,d),axis=0)Concatenate arrays array(1,2,3,10,15,20)np.vstack(a,b)Stack arrays vertically(row-wise)array(1.,2.,3.,1.5,2.,3.,4.,5.,6.)np.r_e,f Stack arrays vertically(row-w
9、ise)np.hstack(e,f)Stack arrays horizontally(column-wise)array(7.,7.,1.,0.,7.,7.,0.,1.)np.column_stack(a,d)Create stacked column-wise arrays array(1,10,2,15,3,20)np.c_a,d Create stacked column-wise arrays Splitting Arrays np.hsplit(a,3)Split the array horizontally at the 3rd array(1),array(2),array(3
10、)index np.vsplit(c,2)Split the array vertically at the 2nd indexarray(1.5,2.,1.,4.,5.,6.),array(3.,2.,3.,4.,5.,6.)Also see Lists Subsetting a2 Select the element at the 2nd index 3 b1,2 Select the element at row 1 column 2 6.0 (equivalent to b12)Slicing a0:2 Select items at index 0 and 1 array(1,2)b
11、0:2,1 Select items at rows 0 and 1 in column 1 array(2.,5.)b:1 Select all items at row 0 array(1.5,2.,3.)(equivalent to b0:1,:)c1,.Same as 1,:,:array(3.,2.,1.,4.,5.,6.)a:-1 Reversed array a array(3,2,1)Boolean Indexing aa b1,0,1,0,0,1,2,0 Select elements(1,0),(0,1),(1,2)and(0,0)array(4.,2.,6.,1.5)b1
12、,0,1,0:,0,1,2,0 Select a subset of the matrixs rows array(4.,5.,6.,4.,and columns 1.5,2.,3.,1.5,4.,5.,6.,4.,1.5,2.,3.,1.5)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)Initial PlaceholdersAggregate Functions np.loadtxt(myfile.txt)np.
13、genfromtxt(my_file.csv,delimiter=,)np.savetxt(myarray.txt,a,delimiter=)I/O1231.5234 56Copying Arrays h=a.view()Create a view of the array with the same data np.copy(a)Create a copy of the array h=a.copy()Create a deep copy of the arraySaving&Loading Text FilesSaving&Loading On Disk np.save(my_array,
14、a)np.savez(array.npz,a,b)np.load(my_array.npy)a.shape Array dimensions len(a)Length of array b.ndim Number of array dimensions e.size Number of array elements b.dtype Data type of array elements b.dtype.name Name of data type b.astype(int)Convert an array to a different type Inspecting Your Array np
15、.info(np.ndarray.dtype)Asking For HelpSorting Arrays a.sort()Sort an array c.sort(axis=0)Sort the elements of an arrays axisData Types np.int64 Signed 64-bit integer types np.float32 Standard double-precision floating point plex Complex numbers represented by 128 floats np.bool Boolean type storing TRUE and FALSE values np.object Python object type np.string_ Fixed-length string type np.unicode_ Fixed-length unicode type1231.5234 561.5234 56123