numpy 100题练习 Day3

https://github.com/rougier/numpy-100

Create a checkerboard 8x8 matrix using the tile function

1
2
3
4
5
6
7
8
9
10
11
Z = np.tile( np.array([[0,1],[1,0]]), (4,4))
print(Z)

[[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]]

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
2
3
4
5
6
7
8
9
10
11
>>> a = np.array([0, 1, 2])
>>> np.tile(a, 2)
array([0, 1, 2, 0, 1, 2])

>>> np.tile(a, (2, 2))
array([[0, 1, 2, 0, 1, 2],
[0, 1, 2, 0, 1, 2]])

>>> np.tile(a, (2, 1, 2))
array([[[0, 1, 2, 0, 1, 2]],
[[0, 1, 2, 0, 1, 2]]])

Normalize a 5x5 random matrix

1
2
3
4
5
6
7
8
9
Z = np.random.random((5,5))
Z = (Z - np.mean (Z)) / (np.std (Z))
print(Z)

[[-1.500019 1.11585252 -0.47634185 -0.82878742 0.83100145]
[-0.60877327 0.80508094 1.01055956 0.8768261 -1.14530307]
[-1.42879691 0.42025043 1.33401344 -0.16557947 -0.25317782]
[-1.15284007 -0.71064254 -0.33070097 -1.340814 1.22239397]
[ 0.29678951 1.15295262 1.34864726 -1.48277079 1.01017939]]

Create a custom dtype that describes a color as four unsigned bytes (RGBA)

1
2
3
4
color = np.dtype([("r", np.ubyte, 1),
("g", np.ubyte, 1),
("b", np.ubyte, 1),
("a", np.ubyte, 1)])

Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)

1
2
3
4
5
6
7
8
9
10
11
12
Z = np.dot(np.ones((5,3)), np.ones((3,2)))
print(Z)

# Alternative solution, in Python 3.5 and above
Z = np.ones((5,3)) @ np.ones((3,2))
print(Z)

[[3. 3.]
[3. 3.]
[3. 3.]
[3. 3.]
[3. 3.]]

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 or a @ b is preferred.

  • If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * 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
2
3
4
5
6
7
8
>>> np.dot(3, 4)
12

>>> a = [[1, 0], [0, 1]]
>>> b = [[4, 1], [2, 2]]
>>> np.dot(a, b)
array([[4, 1],
[2, 2]])

Given a 1D array, negate all elements which are between 3 and 8, in place.

1
2
3
4
5
6
7
# Author: Evgeni Burovski

Z = np.arange(11)
Z[(3 < Z) & (Z <= 8)] *= -1
print(Z)

>>> [ 0 1 2 3 -4 -5 -6 -7 -8 9 10]

What is the output of the following script?

1
2
3
4
5
6
7
8
# Author: Jake VanderPlas

print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))

>>> 9
>>> 10
1
2
3
4
5
6
Z**Z
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z

What are the result of the following expressions?

1
2
3
4
5
6
7
print(np.array(0) / np.array(0))
print(np.array(0) // np.array(0))
print(np.array([np.nan]).astype(int).astype(float))

>>> nan
>>> 0
>>> [-9.22337204e+18]

How to round away from zero a float array ?

1
2
3
4
5
6
# Author: Charles R Harris

Z = np.random.uniform(-10,+10,10)
print (np.copysign(np.ceil(np.abs(Z)), Z))

>>> [ -3. -5. -9. -6. 2. 2. -1. 10. -6. -10.]

How to find common values between two arrays?

1
2
3
4
5
Z1 = np.random.randint(0,10,10)
Z2 = np.random.randint(0,10,10)
print(np.intersect1d(Z1,Z2))

>>> [1 2 4 6 8]

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
2
3
>>> np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])

>>> array([1, 3])