Python Cheat Sheet
| categories: Info
We'll use this page to keep a list of the functions and types we're using.
Types:
- int: integers, positive and negative
- 1 2 3 -42
- float: like real numbers but not quite.
- 1.1 0.01 -3.14 6.022e23
- str: string, a sequence of characters
- "a string" 'another string' 'Z'
- list: a sequence of values of any type
- [1, 2] ['hello', 17]
- tuple: a sequence of values of any type. Like a list but you can't change the values
- (1, 2) (1.5, 1, 'abc')
- bool: a Boolean value, True or False
- True False 0 1
- array: vector, matrix, or higher dimensioned collection of numbers
- array([1, 2, 3]) array([0.1, 1.5, 2.3])
In this table we'll associate types with variable names. Of course, variable names and type are disconnected but this device will make it easy to see the types. In this table:
- x,y,z are floats
- A,B,C are arrays
- i,j,k are integers
- s,t,u are strings
- np.arange(5)
- returns np.array([0, 1, 2, 3, 4])
- np.arange(4, 7)
- returns np.array([4, 5, 6])
- np.arange(3, 7, 2)
- returns np.array([3, 5])
- np.array([1, 5, 3, -2, 42])
- returns a 1-D array with elements 1, 5, 3, -2, and 42.
- np.loadtxt('foo.csv', delimiter=',')
- returns an array of the data read from the comma separated values file named foo.csv. The array will have one row for each line in the file.
- A[0]
- returns the first (0th) element of array A.
- A[-1]
- returns the last element of array A.
- A[2:5]
- returns the elements at index 2, 3, and 4 from array A
- A[3:8:3]
- returns the elements at index 3 and 6 from array A
- A[:5]
- is the same as A[0:5] which returns the first 5 elements of A.
- A[3:]
- returns all the elements at index 3 through the end.
- np.max(A)
- return the maximum element of A.
- np.argmax(A)
- returns the index of the first maximum element of A.
- np.sum(A)
- Sum the elements of A.
- np.sum(A,i)
- Sum A along the ith dimension.
- np.std(A)
- standard deviation of A
- A[A > 5]
- All the elements of A that are greater than 5
- np.mean(A)
- Mean of all the elements of A
- np.mean(A,i)
- Mean along the ith axis
- len(A[A > 5])
- The number of elements greater than 5.
- np.sum(A > 5)
- Also the number of elements greater than 5. True is 1 and False is 0, so if you sum up the True values you get their count.
- A.shape
- a tuple of integers giving the length of each dimension of the array
- A.shape = (r,c)
- resize the array to have r rows and c columns. r*c must equal A.size
- np.reshape(A, (r,c))
- returns a new array with the same values of A and the given shape.
- np.logical_and(A,B)
- returns a vector of Booleans that are True if and only if corresponding values in A and B are True
- np.logical_or(A,B)
- returns a vector of Booleans that are True if either of the corresponding values in A and B are True.
- np.abs(A)
- returns the absolute value of each element in A
- np.clip(A,low,high)
- returns an array with values limited to the range low to high. That is, any values less than low are replaced with low and any values greater than high are replaced with high.
- np.ones_like(A)
- returns an array the same shape as A but filled with ones.
- np.linalg.lstsq(A,B)
- returns a tuple with several values. The first is the one we usually want. It is an array X that is closest in the least-squares sense to the solution np.dot(A,X) == B. That is, np.sum((np.dot(A,X) - B)**2) is minimized.
- pylab.hist(B)
- display the histogram of element frequencies in vector B
- pylab.plot(A,B)
- display a graph of B versus A.
- i % j
- The percent sign is the mod operator, it returns the remainder after integer division
- np.random.random_integers(low, high, count)
- Returns an array of randomly selected integers over the range low to high inclusive.
- np.random.randn(n)
- Returns a vector of length n containing normally distributed random numbers with mean = 0 and standard deviation = 1
- np.cumsum(a, axis=i)
- Returns the cumlative sum of the elements of an array.
- np.diff(a, axis=i)
- Calculate the difference between successive elements of an array along the given axis.
- np.isnan(A)
- Returns a boolean vector with True for each number that is nan, False otherwise.
- np.interp(x, xp, yp)
- Returns the one-dimensional piecewise linear interpolant in the data defined by xp and yp. For example, np.interp(np.array([2, 4]), np.array([1, 3, 5]), np.array([100, 300, 200])) will return np.array([200, 250]).