<jupyter_start><jupyter_text>Selected Exercises for `numpy` Import numpy<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>查看numpy的版本号<jupyter_code>np.__version__<jupyter_output><empty_output><jupyter_text>创建一个10*10的矩阵，其元素全是1<jupyter_code>a = np.ones((10, 10))
a<jupyter_output><empty_output><jupyter_text>创建一个3*3的矩阵，元素分别为0到8<jupyter_code>b = np.arange(0, 9, 1)
b.reshape((3, 3))<jupyter_output><empty_output><jupyter_text>检查下面这个矩阵x元素的最大值和最小值<jupyter_code>x = np.random.random((10, 10))
x.max(), x.min()<jupyter_output><empty_output><jupyter_text>下面的矩阵x是5 * 5的、元素全是1的矩阵，如何给它加一个宽度为1的边框，使得新矩阵是7*7，而且边缘一圈的值为0？<jupyter_code>x = np.ones((5, 5))
m = np.zeros((1, 5))
y = np.concatenate([m, x, m], axis=0)
n = np.zeros((7, 1))
np.concatenate([n, y, n], axis=1)
### Better Answer
np.pad(x, pad_width=1, constant_values=0)<jupyter_output><empty_output><jupyter_text>创建一个5 * 5 的矩阵，以随机数进行填充。并把这个矩阵进行标准化（即减去平均值再除以标准差）。标准化之后的标准差是多少？<jupyter_code>array = np.random.rand(5, 5)
((array - array.mean()) / array.std()).std()<jupyter_output><empty_output><jupyter_text>把数组x中值大于3小于8的数都添一个负号，再打印出来检查看看对不对<jupyter_code>x = np.arange(11)
x[(x > 3) & (x < 8)] *= -1
print(x)<jupyter_output>[ 0  1  2  3 -4 -5 -6 -7  8  9 10]<jupyter_text>如何把1.34353保留两位小数？如何只保留整数？如何四舍五入保留？<jupyter_code>np.around(1.34353, decimals=2), np.around(1.34353) #负数我没有懂是什么意思
### Answer
round(1.34353, 2), round(1.34353), int(1.34353)<jupyter_output><empty_output><jupyter_text>利用`np.intersect1d`函数，试问下列两个array有哪些共同元素？分别在什么位置？<jupyter_code>np.intersect1d([1, 2, 3, 4, 5], [2, 4, 6])<jupyter_output><empty_output><jupyter_text>今天几号？可以这么问numpy哈哈哈哈<jupyter_code>np.datetime64('today')<jupyter_output><empty_output><jupyter_text>把下面这个array首位颠倒一下<jupyter_code>x = np.random.randint(low=0, high=100, size=(10))
x
#x.reverse() 为啥用不了
### Answer
x[::-1]<jupyter_output><empty_output><jupyter_text>对下面这个array从大到小排序，然后打印看看<jupyter_code>x = np.random.randint(low=0, high=100, size=(10)).reshape(1, -1)
x
print(np.ndarray.sort(x, axis=1))   #aaaaaa怎么回事弄不出来
### Answer
y = np.sort(x)[0]
y[::-1]<jupyter_output><empty_output><jupyter_text>X和Y是平面直角坐标系上点的坐标，请把这些点的坐标转化为极坐标。<jupyter_code>Z = np.random.random((10,2))
X, Y = Z[:,0], Z[:,1]
x = np.arctan( Y / X ) / 180 * np.pi
y = np.sqrt( X ** 2 + Y ** 2)
### Answer
x = np.arctan( Y / X ) * 180 / np.pi ## rad to deg: np.rad2deg()
y = np.sqrt( X ** 2 + Y ** 2)<jupyter_output><empty_output><jupyter_text>搞明白这句话是在干什么<jupyter_code>list(zip(X, Y))
#zip(X, Y)把X, Y变成zip对象，再用list变成列表
#### 咋跟没说一样咧<jupyter_output><empty_output><jupyter_text>把Z最大值的位置处的元素换为-99<jupyter_code>Z = np.random.random(10)
Z[np.argmax(Z)] = -99<jupyter_output><empty_output><jupyter_text>找出下面这个array中哪个元素最接近15？<jupyter_code>Z = np.random.randint(low=0, high=25, size=(30))
np.argmin(Z - 15)<jupyter_output><empty_output><jupyter_text>搞明白下面这段是在干甚<jupyter_code>X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))
D = np.sqrt(X * X + Y * Y)
sigma, mu = 1.0, 0.0
G = np.exp(-((D - mu)**2 / (2.0 * sigma**2)))

import matplotlib.pyplot as plt
plt.imshow(G)
plt.colorbar()
plt.show()
#坐标为（X, Y）的点距离原点的距离是以0为均值，以1为标准差的正态分布（公式前的根号下2pi呢？）
### 看的是color 不是距离 OwO<jupyter_output><empty_output><jupyter_text>请用np.load函数加载储存在`array-with-nan.npy`文件中的numpy矩阵，并用图像观察之。1) 白色的点是啥子？2) 检查一下矩阵里是否存在`np.nan` (Not A Number). 如果存在，这些nan在何处？index分别是什么？3) 将这些nan都换成9，再画出图来看看（记得画出colorbar），白点变成啥子了？<jupyter_code>S = np.load('array-with-nan.npy')
plt.imshow(S)
plt.colorbar()
plt.show()
# 1) nan 'not a number'没有数字，也就没有颜色可以代表
np.where(np.isnan(S))
S[np.where(np.isnan(S))] = 9
plt.imshow(S)
plt.colorbar()
plt.show()
# 3) 变成了✨<jupyter_output><empty_output><jupyter_text>如何把变量x = True变成False？<jupyter_code>x = True
np.logical_not(x)
### Answer
not True<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18641347953998394, 'fraction_numerical': 0.05001337255950789, 'mean_word_length': 8.42065491183879, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '227748', 'license': 'MIT', 'n_tokens_mistral': 2193, 'n_tokens_neox': 2223, 'n_words': 210, 'path': 'numpy/HW/HW-numpy-exercise-revised.ipynb', 'repo_name': 'AstroJacobLi/pynguin', 'size': '3739'}