Command | Description |
---|---|
* |
multiplication operation: 2*3 returns 6 |
** |
power operation: 2**3 returns 8 |
@ |
matrix multiplication: import numpy as np
A = np.array([[1, 2, 3]])
B = np.array([[3], [2], [1]])
A @ B returns array([[10]]) |
Command | Description |
---|---|
l = [a1, a2,..., an] |
Constructs a list containing the objects a1, a2,..., an. You can append to the list using l.append() .
The ith element of l can be accessed using l[i] |
t =(a1, a2,..., an) |
Constructs a tuple containing the objects a1, a2,..., an. The ith element of t can be accessed using t[i] |
Command | Description |
---|---|
len(iterable) |
len(np.zeros((5, 4))) returns |
zip |
Make an iterator that aggregates elements from each of the iterables. x = [1, 2, 3]
y = [4, 5, 6]
zipped = zip(x, y)
list(zipped) returns |
Command | Description |
---|---|
for a in iterable: |
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 l = []
for i in [1, 2, 3]:
l.append(i**2)
print(l) prints |
Command | Description |
---|---|
if condition: |
Performs code if a condition is met (using tabs). For example if x == 5:
x = x**2
else:
x = x**3 squares x if x is 5, otherwise cubes it. |
Command | Description |
---|---|
lambda |
Used for create anonymous one line functions of the form: f = lambda x, y: 5*x+y The code after the lambda but before variables specifies the parameters. The code after the colon tells python what object to return. |
def |
The def command is used to create functions of more than one line: def g(x, y):
"""
Docstring
"""
ret = sin(x)
return ret + y The code immediately following |
Command | Description |
---|---|
np.array(object, dtype = None) |
np.array([1, 2, 3]) #creates 1 dim array of ints
np.array( [1, 2, 3.0] )#creates 1 dim array of floats
np.array( [ [1, 2], [3, 4] ]) #creates a 2 dim array |
A[i1, i2,..., in] |
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. |
np.zeros(shape) |
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 5\times 5 array while
will throw an error. |
np.ones(shape) |
Same as np.zeros but produces an array of ones |
np.linspace(a, b, n) |
Returns a numpy array with n linearly spaced points between a and b. For example
returns array([ 1. , 1.11111111, 1.22222222, 1.33333333,
1.44444444, 1.55555556, 1.66666667, 1.77777778,
1.88888889, 2. ]) |
np.eye(N) |
Constructs the identity matrix of size N. For example
returns the 3\times 3 identity matrix: \left(\begin{matrix}1&0&0\\0&1&0\\ 0&0&1\end{matrix}\right) |
np.diag(a) |
returns If a is a 1 dimensional array then
returns \left(\begin{matrix}1&0\\0&2\end{matrix}\right) |
np.random.rand(d0, d1,..., dn) |
Constructs a numpy array of shape array([[ 0.69060674, 0.38943021, 0.19128955],
[ 0.5419038 , 0.66963507, 0.78687237]]) |
np.random.randn(d0, d1,..., dn) |
Same as np.random.rand(d0, d1,..., dn) except that it draws from the standard normal distribution \mathcal N(0, 1)
rather than the uniform distribution. |
A.T |
Reverses the dimensions of an array (transpose).
For example,
if x = \left(\begin{matrix} 1& 2\\3&4\end{matrix}\right) then x.T returns \left(\begin{matrix} 1& 3\\2&4\end{matrix}\right)
|
np.hstack(tuple) |
Take a sequence of arrays and stack them horizontally to make a single array. For example a = np.array( [1, 2, 3] )
b = np.array( [2, 3, 4] )
np.hstack( (a, b) ) returns a = np.array( [[1], [2], [3]] )
b = np.array( [[2], [3], [4]] )
np.hstack((a, b)) returns \left( \begin{matrix} 1&2\\2&3\\ 3&4 \end{matrix}\right) |
np.vstack(tuple) |
Like a = np.array( [1, 2, 3] )
b = np.array( [2, 3, 4] )
np.hstack( (a, b) ) returns array( [ [1, 2, 3],
[2, 3, 4] ] ) |
np.amax(a, axis = None) |
By default
then
returns
returns |
np.amin(a, axis = None) |
Same as np.amax except returns minimum element. |
np.argmax(a, axis = None) |
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 np.amax(a, axis = 0) #maximization along row (dim 0) returns np.amax(a, axis = 1) #maximization along column (dim 1) returns |
np.argmin(a, axis =None) |
Same as np.argmax except finds minimal index. |
np.dot(a, b) or a.dot(b)
|
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 np.dot(a, b) is valid.
If b is a (1, 2) array then the operation will return an error. |
Command | Description |
---|---|
np.linalg.inv(A) |
For a 2-dimensional array A. np.linalg.inv(A).dot(A) returns np.array( [ [1, 0],
[0, 1] ]) |
np.linalg.eig(A) |
Returns a 1-dimensional array with all the eigenvalues of
returns the eigenvalues in |
np.linalg.solve(A, b) |
Constructs array x such that Ainv = np.linalg.inv(A)
x = Ainv.dot(b) but numerically more stable. |
Command | Description |
---|---|
pd.Series() | Constructs a Pandas Series Object from some specified data and/or index s1 = pd.Series([1, 2, 3])
s2 = pd.Series([1, 2, 3], index=['a', 'b', 'c']) |
pd.DataFrame() | Constructs a Pandas DataFrame object from some specified data and/or index, column names etc. d = {'a' : [1, 2, 3], 'b' : [4, 5, 6]}
df = pd.DataFrame(d) or alternatively, a = [1, 2, 3]
b = [4, 5, 6]
df = pd.DataFrame(list(zip(a, b)), columns=['a', 'b']) |
Command | Description |
---|---|
plt.plot(x, y, s =None) |
The plot command is included in import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0, 10, 100)
N=len(x)
v= np.cos(x)
plt.figure(1)
plt.plot(x, v, '-og')
plt.show()
plt.savefig('tom_test.eps') plots the cosine function on the domain (0, 10) with a green line with circles at the points x, v |