np.array(object, dtype = None) | np.array constructs a numpy array from an object, such as a list or a list of lists. dtype allows you to specify the type of object the array is holding. You will generally note need to specify the dtype . Examples: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 : to access a range of indices, where imin:imax represents all ii such that imin≤iA[:, 2] returns the 2nd column (counting from 0) of A as a 1 dimensional array andA[0:2, :] 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
np.zeros((5, 5)) Constructs an 5×55×5 array whilenp.zeros(5, 5) 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 nn linearly spaced points between aa and bb. For examplenp.linspace(1, 2, 10) returnsarray([ 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 NN. For examplenp.eye(3) returns the 3×33×3 identity matrix:⎛⎝⎜100010001⎞⎠⎟(100010001) |
np.diag(a) | np.diag has 2 uses. First if a is a 2 dimensional array then np.diag returns the principle diagonal of the matrix. Thusnp.diag( [ [1, 3], [5, 6] ]) returns [1, 6] .If aa is a 1 dimensional array then np.diag constructs an array with $a$ as the principle diagonal. Thus,
np.diag([1, 2]) returns(1002)(1002) |
np.random.rand(d0, d1,…, dn) | Constructs a numpy array of shape (d0, d1,…, dn) filled with random numbers drawn from a uniform distribution between :math(0, 1) . For example, np.random.rand(2, 3) returnsarray([[ 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 N(0,1)N(0,1) rather than the uniform distribution. |
A.T | Reverses the dimensions of an array (transpose). For example, if x=(1324)x=(1234) then x.T returns (1234)(1324) |
np.hstack(tuple) | Take a sequence of arrays and stack them horizontally to make a single array. For examplea = np.array( [1, 2, 3] ) b = np.array( [2, 3, 4] ) np.hstack( (a, b) ) returns [1, 2, 3, 2, 3, 4] whilea = np.array( [[1], [2], [3]] ) b = np.array( [[2], [3], [4]] ) np.hstack((a, b)) returns ⎛⎝⎜123234⎞⎠⎟(122334) |
np.vstack(tuple) | Like np.hstack . Takes a sequence of arrays and stack them vertically to make a single array. For examplea = np.array( [1, 2, 3] ) b = np.array( [2, 3, 4] ) np.hstack( (a, b) ) returnsarray( [ [1, 2, 3], [2, 3, 4] ] ) |
np.amax(a, axis = None) | By default np.amax(a) finds the maximum of all elements in the array aa. Can specify maximization along a particular dimension with axis. Ifa = np.array( [ [2, 1], [3, 4] ]) #creates a 2 dim array thennp.amax(a, axis = 0) #maximization along row (dim 0) returns array([3, 4]) andnp.amax(a, axis = 1) #maximization along column (dim 1) returns array([2, 4]) |
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.amaxnp.amax(a, axis = 0) #maximization along row (dim 0) returns array([1, 1]) and
np.amax(a, axis = 1) #maximization along column (dim 1) returns array([0, 1]) |
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 aa and bb. For this operation to work the innermost dimension of aa must be equal to the outermost dimension of bb. If aa is a (3,2)(3,2)array and bb is a (2)(2) array then np.dot(a, b) is valid. If bb is a (1,2)(1,2) array then the operation will return an error. |