
Lists Also see NumPy Arrays Libraries Python For Data Science Cheat Sheet >>> a = 'is' Import libraries Python Basics >>> b = 'nice' >>> import numpy Data analysis Machine learning Learn More Python for Data Science Interactively at www.datacamp.com >>> my_list = ['my', 'list', a, b] >>> import numpy as np >>> my_list2 = [[4,5,6,7], [3,4,5,6]] Selective import >>> from math import pi Scientific computing 2D plotting Variables and Data Types Selecting List Elements Index starts at 0 Install Python Variable Assignment Subset >>> my_list[1] Select item at index 1 >>> x=5 >>> my_list[-3] Select 3rd last item >>> x Slice 5 >>> my_list[1:3] Select items at index 1 and 2 Calculations With Variables >>> my_list[1:] Select items after index 0 Select items before index 3 Leading open data science platform Free IDE that is included Create and share Sum of two variables >>> my_list[:3] >>> x+2 Copy my_list powered by Python with Anaconda documents with live code, >>> my_list[:] visualizations, text, ... 7 Subset Lists of Lists >>> x-2 Subtraction of two variables my_list[list][itemOfList] >>> my_list2[1][0] 3 Numpy Arrays Also see Lists >>> my_list2[1][:2] >>> x*2 Multiplication of two variables >>> my_list = [1, 2, 3, 4] 10 List Operations >>> x**2 Exponentiation of a variable >>> my_array = np.array(my_list) 25 >>> my_list + my_list >>> my_2darray = np.array([[1,2,3],[4,5,6]]) >>> x%2 Remainder of a variable ['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice'] Selecting Numpy Array Elements Index starts at 0 1 >>> my_list * 2 >>> x/float(2) Division of a variable ['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice'] Subset Select item at index 1 2.5 >>> my_list2 > 4 >>> my_array[1] True 2 Types and Type Conversion Slice List Methods Select items at index 0 and 1 str() '5', '3.45', 'True' Variables to strings >>> my_array[0:2] Get the index of an item array([1, 2]) >>> my_list.index(a) Count an item Subset 2D Numpy arrays int() 5, 3, 1 Variables to integers >>> my_list.count(a) Append an item at a time my_2darray[rows, columns] >>> my_list.append('!') >>> my_2darray[:,0] Remove an item array([1, 4]) float() 5.0, 1.0 Variables to floats >>> my_list.remove('!') >>> del(my_list[0:1]) Remove an item Numpy Array Operations Reverse the list bool() True, True, True Variables to booleans >>> my_list.reverse() Append an item >>> my_array > 3 >>> my_list.extend('!') array([False, False, False, True], dtype=bool) Remove an item >>> my_list.pop(-1) >>> my_array * 2 Asking For Help Insert an item >>> my_list.insert(0,'!') array([2, 4, 6, 8]) Sort the list >>> help(str) >>> my_list.sort() >>> my_array + np.array([5, 6, 7, 8]) Strings array([6, 8, 10, 12]) Numpy Array Functions >>> my_string = 'thisStringIsAwesome' String Operations Index starts at 0 >>> my_string >>> my_array.shape Get the dimensions of the array 'thisStringIsAwesome' >>> my_string[3] >>> np.append(other_array) Append items to an array Insert items in an array String Operations >>> my_string[4:9] >>> np.insert(my_array, 1, 5) >>> np.delete(my_array,[1]) Delete items in an array String Methods Mean of the array >>> my_string * 2 >>> np.mean(my_array) String to uppercase Median of the array 'thisStringIsAwesomethisStringIsAwesome' >>> my_string.upper() >>> np.median(my_array) String to lowercase Correlation coefficient >>> my_string + 'Innit' >>> my_string.lower() >>> my_array.corrcoef() Count String elements Standard deviation 'thisStringIsAwesomeInnit' >>> my_string.count('w') >>> np.std(my_array) Replace String elements >>> 'm' in my_string >>> my_string.replace('e', 'i') True >>> my_string.strip() Strip whitespaces DataCamp Learn Python for Data Science Interactively Python For Data Science Cheat Sheet Working with Different Programming Languages Widgets Kernels provide computation and communication with front-end interfaces Notebook widgets provide the ability to visualize and control changes Jupyter Notebook like the notebooks. There are three main kernels: in your data, often as a control like a slider, textbox, etc. Learn More Python for Data Science Interactively at www.DataCamp.com You can use them to build interactive GUIs for your notebooks or to IRkernel IJulia synchronize stateful and stateless information between Python and Installing Jupyter Notebook will automatically install the IPython kernel. JavaScript. Saving/Loading Notebooks Restart kernel Interrupt kernel Create new notebook Restart kernel & run Interrupt kernel & Download serialized Save notebook clear all output state of all widget Open an existing all cells with interactive models in use Make a copy of the notebook Connect back to a widgets Restart kernel & run remote notebook current notebook all cells Embed current Rename notebook Run other installed widgets kernels Revert notebook to a Save current notebook previous checkpoint and record checkpoint Command Mode: Download notebook as Preview of the printed - IPython notebook 15 notebook - Python - HTML Close notebook & stop - Markdown 13 14 - reST running any scripts - LaTeX - PDF 1 2 3 4 5 6 7 8 9 10 11 12 Writing Code And Text Code and text are encapsulated by 3 basic cell types: markdown cells, code cells, and raw NBConvert cells. Edit Cells Edit Mode: 1. Save and checkpoint 9. Interrupt kernel 2. Insert cell below 10. Restart kernel Cut currently selected cells 3. Cut cell 11. Display characteristics Copy cells from 4. Copy cell(s) 12. Open command palette to clipboard clipboard to current 5. Paste cell(s) below 13. Current kernel cursor position 14. Kernel status Paste cells from 6. Move cell up Executing Cells 7. Move cell down 15. Log out from notebook server clipboard above Paste cells from Run current cells down 8. Run current cell current cell clipboard below Run selected cell(s) and create a new one current cell Paste cells from below Asking For Help clipboard on top Run current cells down Delete current cells of current cel and create a new one Walk through a UI tour Split up a cell from above Run all cells Revert “Delete Cells” List of built-in keyboard current cursor Run all cells above the Run all cells below invocation shortcuts position current cell the current cell Edit the built-in Merge current cell Merge current cell keyboard shortcuts Change the cell type of toggle, toggle Notebook help topics with the one above with the one below current cell scrolling and clear Description of Move current cell up Move current cell toggle, toggle current outputs markdown available Information on down Adjust metadata scrolling and clear in notebook unofficial Jupyter all output Notebook extensions underlying the Find and replace Python help topics current notebook in selected cells View Cells IPython help topics Remove cell Copy attachments of NumPy help topics attachments Toggle display of Jupyter SciPy help topics current cell Toggle display of toolbar logo and filename Matplotlib help topics Paste attachments of Insert image in SymPy help topics current cell selected cells Toggle display of cell Pandas help topics action icons: Insert Cells - None About Jupyter Notebook - Edit metadata Toggle line numbers - Raw cell format Add new cell above the Add new cell below the - Slideshow in cells current one current one - Attachments - Tags DataCamp Learn Python for Data Science Interactively Python For Data Science Cheat Sheet Inspecting Your Array Subsetting, Slicing, Indexing Also see Lists >>> a.shape Array dimensions Length of array Subsetting >>> len(a) 1 2 3 Select the element at the 2nd index NumPy Basics Number of array dimensions >>> a[2] Learn Python for Data Science Interactively at www.DataCamp.com >>> b.ndim 3 >>> e.size Number of array elements 1.5 2 3 >>> b[1,2] Select the element at row 0 column 2 >>> b.dtype Data type of array elements 6.0 4 5 6 (equivalent to b[1][2]) >>> b.dtype.name Name of data type >>> b.astype(int) Convert an array to a different type Slicing NumPy >>> a[0:2] 1 2 3 Select items at index 0 and 1 2 array([1, 2]) Asking For Help 1.5 2 3 The NumPy library is the core library for scientific computing in >>> b[0:2,1] Select items at rows 0 and 1 in column 1 >>> np.info(np.ndarray.dtype) array([ 2., 5.]) 4 5 6 Python. It provides a high-performance multidimensional array 1.5 2 3 object, and tools for working with these arrays. Array Mathematics >>> b[:1] Select all items at row 0 array([[1.5, 2., 3.]]) 4 5 6 (equivalent to b[0:1, :]) Same as Use the following import convention: Arithmetic Operations >>> c[1,...] [1,:,:] array([[[ 3., 2., 1.], Subtraction [ 4., 5., 6.]]]) >>> import numpy as np >>> g = a - b Reversed array array([[-0.5, 0. , 0. ], >>> a[ : :-1] a NumPy Arrays array([3, 2, 1]) [-3. , -3. , -3. ]]) Subtraction Boolean Indexing 1D array 2D array 3D array >>> np.subtract(a,b) Select elements from less than 2 >>> b + a Addition >>> a[a<2] 1 2 3 a axis 1 axis 2 array([[ 2.5, 4. , 6. ], array([1]) 1 2 3 axis 1 [ 5. , 7. , 9. ]]) Fancy Indexing 1.5 2 3 Addition Select elements (1,0),(0,1),(1,2) and (0,0) axis 0 axis 0 >>> np.add(b,a) >>> b[[1, 0, 1, 0],[0, 1, 2, 0]] 4 5 6 >>> a / b Division array([ 4. , 2. , 6. , 1.5]) array([[ 0.66666667, 1. , 1. ], >>> b[[1, 0, 1, 0]][:,[0,1,2,0]] Select a subset of the matrix’s rows [ 0.25 , 0.4 , 0.5 ]]) array([[ 4. ,5. , 6. , 4. ], and columns >>> np.divide(a,b) Division [ 1.5, 2. , 3. , 1.5], Multiplication [ 4. , 5. , 6. , 4. ], Creating Arrays >>> a * b [ 1.5, 2. , 3. , 1.5]]) array([[ 1.5, 4. , 9. ], >>> a = np.array([1,2,3]) [ 4. , 10. , 18. ]]) >>> b = np.array([(1.5,2,3), (4,5,6)], dtype = float) >>> np.multiply(a,b) Multiplication Array Manipulation >>>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages9 Page
-
File Size-