- Python cheatsheet
Python Cheat sheet (from your instructor) Note: All of these commands are based on Python 2.x versions not 3.x This by no means is comprehensive, but it will help you with a few tasks in lab 1 and HW 1. 1) Reading a file into the python environment file='myfile.txt' fl=open(file,'r')! 2) Writing and calling executable python files.
Don’t miss our FREE NumPy cheat sheet at the bottom of this post. NumPy is a commonly used Python data analysis package. By using NumPy, you can speed up your workflow, and interface with other packages in the Python ecosystem, like scikit-learn, that use NumPy under the hood. NumPy was originally developed in the mid 2000s, and arose from an. NumPy is the library that gives Python its ability to work with data at speed. Originally, launched in 1995 as ‘Numeric,’ NumPy is the foundation on which many important Python data science libraries are built, including Pandas, SciPy and scikit-learn. Python For Data Science Cheat Sheet NumPy Basics Learn Python for Data Science Interactively at www.DataCamp.com NumPy DataCamp Learn Python for Data Science Interactively 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. PYTHON FOR DATA SCIENCE CHEAT SHEET Python NumPy A library consisting of multidimensional array objects and a collection of routines for processing those arrays. W h a t i s N u m P y? Import numpy as np –Import numpy I m p o r t C o n v e n t i o n FURTHERMORE: Python for Data Science Certification Training Course.
Operators¶
Command | Description |
---|---|
| multiplication operation: |
| power operation: |
| matrix multiplication: returns |
Data Types¶
Command | Description |
---|---|
| Constructs a list containing the objects (a1, a2,..., an). You can append to the list using |
| Constructs a tuple containing the objects (a1, a2,..., an). The (ith) element of (t) can be accessed using |
Built-In Functions¶
Command | Description |
---|---|
|
returns |
| Make an iterator that aggregates elements from each of the iterables. returns |
Iterating¶
Command | Description |
---|---|
| For loop used to perform a sequence of commands (denoted using tabs) for each element in an iterable object such as a list, tuple, or numpy array.An example code is prints |
Comparisons and Logical Operators¶
Command | Description |
---|---|
| Performs code if a condition is met (using tabs). For example squares (x) if (x) is (5), otherwise cubes it. |
User-Defined Functions¶
Command | Description |
---|---|
| Used for create anonymous one line functions of the form: The code after the lambda but before variables specifies the parameters. The code after the colon tells python what object to return. |
| The def command is used to create functions of more than one line: The code immediately following |
Numpy¶
Command | Description |
---|---|
|
|
| Access a the element in numpy array A in with index i1 in dimension 1, i2 in dimension 2, etc.Can use
returns the 2nd column (counting from 0) of A as a 1 dimensional array and
returns the 0th and 1st rows in a 2 dimensional array. |
| Constructs numpy array of shape shape. Here shape is an integer of sequence of integers. Such as 3, (1, 2), (2, 1), or (5, 5). Thus
Constructs an (5times 5) array while
will throw an error. |
| Same as |
| Returns a numpy array with (n) linearly spaced points between (a) and (b). For example
returns |
| Constructs the identity matrix of size (N). For example
returns the (3times 3) identity matrix: [begin{split}left(begin{matrix}1&0&00&1&0 0&0&1end{matrix}right)end{split}] |
|
returns If (a) is a 1 dimensional array then
returns [begin{split}left(begin{matrix}1&00&2end{matrix}right)end{split}] |
| Constructs a numpy array of shape |
| Same as |
| Reverses the dimensions of an array (transpose).For example,if (x = left(begin{matrix} 1& 23&4end{matrix}right)) then |
| Take a sequence of arrays and stack them horizontally to make a single array. For example returns returns (left( begin{matrix} 1&22&3 3&4 end{matrix}right)) |
| Like returns |
| By default
then
returns
returns |
| Same as |
| Performs similar function to np.amax except returns index of maximal element.By default gives index of flattened array, otherwise can use axis to specify dimension.From the example for np.amax returns returns |
| Same as |
| Returns an array equal to the dot product of (a) and (b).For this operation to work the innermost dimension of (a) must be equal to the outermost dimension of (b).If (a) is a ((3, 2)) array and (b) is a ((2)) array then |
numpy.linalg¶
Command | Description |
---|---|
| For a 2-dimensional array (A). returns |
| Returns a 1-dimensional array with all the eigenvalues of $A$ as well as a 2-dimensional array with the eigenvectors as columns.For example,
returns the eigenvalues in |
| Constructs array (x) such that but numerically more stable. |
Pandas¶
Command | Description |
---|---|
pd.Series() | Constructs a Pandas Series Object from some specified data and/or index |
pd.DataFrame() | Constructs a Pandas DataFrame object from some specified data and/or index, column names etc. or alternatively, |
Numpy Cheat Sheet Pdf
Plotting¶
Programming Cheat Sheet Pdf
Command | Description |
---|---|
| The plot command is included in plots the cosine function on the domain (0, 10) with a green line with circles at the points (x, v) |