numpy 100题练习 Day2

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

Create a 3x3 identity matrix

创建一个3x3的矩阵,返回一个对角线是1,其余是0的数组。

np.eye(N),N行。

1
2
3
4
5
6
Z = np.eye(3)
print(Z)

>>> [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

Create a 3x3x3 array with random values

创建一个用随机数组成的3x3x3数组。

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

>>> [[[0.11085346 0.63675724 0.56551715]
[0.85845655 0.83132722 0.15546257]
[0.68620584 0.9561824 0.59587874]]

[[0.49882408 0.56935352 0.9964925 ]
[0.81017846 0.83963088 0.77471192]
[0.59353945 0.73767187 0.6880103 ]]

[[0.4130483 0.55252658 0.88652063]
[0.45995876 0.66758083 0.57286753]
[0.34854386 0.09369495 0.90712067]]]

Create a 10x10 array with random values and find the minimum and maximum values

创建一个10x10的随机数组,并且找到最小值和最大值。

1
2
3
4
5
Z = np.random.random((10,10))
Zmin, Zmax = Z.min(), Z.max()
print(Zmin, Zmax)

>>> 0.0009066852233082789 0.9990012244445428

Create a random vector of size 30 and find the mean value

创建一个大小为30的随机矢量,并且找到平均值。

np.mean 计算元素的平均值。

1
2
3
4
5
Z = np.random.random(30)
m = Z.mean()
print(m)

>>> 0.4892178091799963

Create a 2d array with 1 on the border and 0 inside

创建一个2维数组,并且边框都为数值1,内部数组都为0。

运用切片,[start:stop:step]。

1
2
3
Z = np.ones((10,10))
Z[1:-1,1:-1] = 0
print(Z)
1
2
3
4
5
6
7
8
9
10
11
# output
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]

How to add a border (filled with 0’s) around an existing array?

如何在当前数组添加一个用数值0填充的边框?

1
2
3
Z = np.ones((5,5))
Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
print(Z)
1
2
3
4
5
6
7
8
# output
[[0. 0. 0. 0. 0. 0. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 0. 0. 0. 0. 0. 0.]]

Numpy.pad()

https://docs.scipy.org/doc/numpy/reference/generated/numpy.pad.html

填充数组。

参数:

  • rank N 填充的数组
  • pad_width 填充的数值
  • mode 有11种填充模式,constant表示连续填充相同的值。constant_values = (x,y)
  • stat_length
  • constant_values
  • end_values
  • reflect_type

What is the result of the following expression?

1
2
3
4
5
6
7
8
9
10
11
12
13
print(0 * np.nan)
print(np.nan == np.nan)
print(np.inf > np.nan)
print(np.nan - np.nan)
print(np.nan in set([np.nan]))
print(0.3 == 3 * 0.1)

nan
False
False
nan
True
False

IEEE 754 floating point representation of Not a Number (NaN).

Create a 5x5 matrix with values 1,2,3,4 just below the diagonal

创建一个在对角线下方的值为1,2,3,4 的 5x5矩阵。

1
2
3
4
5
6
7
8
Z = np.diag(1+np.arange(4),k=-1)
print(Z)

[[0 0 0 0 0]
[1 0 0 0 0]
[0 2 0 0 0]
[0 0 3 0 0]
[0 0 0 4 0]]

http://www.numpy.org/devdocs/reference/generated/numpy.diag.html

Create a 8x8 matrix and fill it with a checkerboard pattern

创建一个内部填充棋盘图案的8x8矩阵。

1
2
3
4
5
6
7
8
9
10
11
12
13
Z = np.zeros((8,8),dtype=int)
Z[1::2,::2] = 1
Z[::2,1::2] = 1
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]]

Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?

在一个形状为(6,7,8)的数组里,第100个元素的索引(x,y,z)的数值是?

1
2
3
print(np.unravel_index(99,(6,7,8)))

>>> (1, 5, 4)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.unravel_index.html