Numpy Merged
Numpy Merged
axis 0
Arrays of evenly spaced values 1 -2 0 >>> np.greater_equal(a,l) a >= l
1 3 10 0 >>> np.add(a,l) is same as a + l
np.arange(10,25,5) step value axis 1 3 4 2 >>> np.less(a,l) a < l >>> np.linalg.det(f) Determinant
Vectorized Operations with NumPy
axis 0
axis 1 >>> np.less_equal(a,l) a >= l
l=np.linspace(0,3,4) # samples B D F >>> np.linalg.inv(f) Inverse
>>> np.equal(a,l) a >= l
A C E L Arithmetic Operations
axis 0
>>> np.add(a,l) a + l
f=np.random.random((2,2)) random array c(2,3,2) array([1,4,12,3]) Bitwise Operations b b.T
>>> np.subtract(a,l) a - l
>>> np.bitwise_and(a,l) a & l
Other Array Construction Methods nD Array Properties >>> np.multiply(a,l) a * l
>>> np.bitwise_or(a,l) a | l
1 3
1 -2 0
>>> np.divide(a,l) a / l -2 4
>>> np.eye(2) 2 x 2 identity matrix >>> a.size Number of array elements >>> np.bitwise_xor(a,l) a ^ l
>>> np.floor_divide(a,l) a >/ l
3 4 2
>>> a.ndim Number of array dimensions >>> np.invert(a) ~a 0 2
>>> np.diag((1,3,5)) diagonal matrix
>>> a.shape Array dimensions >>> np.mod(a,l) a % l >>> np.left_shift(a,l) a >< l
>>> d = a.copy() Copy of array >>> a.dtype Data type of array elements >>> np.power(a,l) a >* l >>> np.right_shift(a,l) a >> l
Broadcasting & Masking Array Manipulations
Masking Changing shape of an array Splitting an array Tiling Arrays
Two arrays are said to be compatible in a dimension if
>>> np.reshape(c,(2,2,3)) >>> np.split(b, 2, Splits b into 2 >>> np.repeat(a,2) Repeats elements
1. they have the same size in that dimension, or C F
>>> mask = a > 3 B D F axis=0) equal arrays
2. one of the arrays has size 1 in that dimension B EI L
A C E L >>> np.hsplit(b,[2]) Splits at 2nd column
>>> a[mask] A H
D K Adding and Removing elements
>>> g = np.array([[1, -2, 0, 1], G I K G J
[3, 4, 2, 0]]) >>> np.vsplit(b,[1]) Splits at 1st row >>> np.delete(b,1,0) Deletes elements
>>> np.ravel(b)
>>> a + g 1 3 10 0 > 3 Contiguous flattened array >>> np.insert(b,1,
[4,4],axis=1)
Inserts elements
>>> b.flatten()
1 3 10 0
1 3 10 0 + 1 -2 0
3 4 2
1
0
False False True False
Flatten array to one dimension
>>> a[np.newaxis,:] Joining arrays
>>> np.append(a,l,
axis=0)
Appends elements
2
>>> np.nan 1. 3. 10. 0.
2
NaN constant
is
is
ax
>>> a.sort() Sorts an array in-place
ax
axis 1 axis 1 3 3 4 2 2
>>> np.inf Positive Infinity 0. 1. 2. 3.
>>> np.argsort(a) Indices that sort array.