Create a checkerboard 8x8 matrix using the tile function
1 | Z = np.tile( np.array([[0,1],[1,0]]), (4,4)) |
numpy.tile
https://docs.scipy.org/doc/numpy/reference/generated/numpy.tile.html
Construct an array by repeating A the number of times given by reps.
If reps has length d
, the result will have dimension of max(d, A.ndim)
.
If A.ndim < d
, A is promoted to be d-dimensional by prepending new axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication, or shape (1, 1, 3) for 3-D replication. If this is not the desired behavior, promote A to d-dimensions manually before calling this function.
If A.ndim > d
, reps is promoted to A.ndim by pre-pending 1’s to it. Thus for an A of shape (2, 3, 4, 5), a reps of (2, 2) is treated as (1, 1, 2, 2).
参数
- A (the input array)
- reps (the number of repetitions of a long each axis)
例子
1 | 0, 1, 2]) a = np.array([ |
Normalize a 5x5 random matrix
1 | Z = np.random.random((5,5)) |
Create a custom dtype that describes a color as four unsigned bytes (RGBA)
1 | color = np.dtype([("r", np.ubyte, 1), |
Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)
1 | Z = np.dot(np.ones((5,3)), np.ones((3,2))) |
numpy.dot
Dot product of two arrays. Specifically,
If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation).
If both a and b are 2-D arrays, it is matrix multiplication, but using
matmul
ora @ b
is preferred.If either a or b is 0-D (scalar), it is equivalent to
multiply
and usingnumpy.multiply(a, b)
ora * b
is preferred.If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.
If a is an N-D array and b is an M-D array (where
M>=2
), it is a sum product over the last axis of a and the second-to-last axis of b:1
dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
参数
- a
- b
- out
例子
1 | 3, 4) np.dot( |
Given a 1D array, negate all elements which are between 3 and 8, in place.
1 | # Author: Evgeni Burovski |
What is the output of the following script?
1 | # Author: Jake VanderPlas |
Consider an integer vector Z, which of these expressions are legal?
1 | Z**Z |
What are the result of the following expressions?
1 | print(np.array(0) / np.array(0)) |
How to round away from zero a float array ?
1 | # Author: Charles R Harris |
How to find common values between two arrays?
1 | Z1 = np.random.randint(0,10,10) |
https://docs.scipy.org/doc/numpy/reference/generated/numpy.intersect1d.html
numpy.intersect1d
Find the intersection of two arrays.
Return the sorted, unique values that are in both of the input arrays.
1 | 1, 3, 4, 3], [3, 1, 2, 1]) np.intersect1d([ |