<jupyter_start><jupyter_text>切片、提取、赋值、索引 1. 用筛法产生素数判别组：<jupyter_code>is_prime = np.ones((100,), dtype=bool)
N_max = int(np.sqrt(len(is_prime)))
for j in range(2, N_max):
    is_prime[2*j::j]=False
(is_prime[17],
is_prime[18])<jupyter_output><empty_output><jupyter_text>2. Using boolean masks：<jupyter_code>np.random.seed(3)
a = np.random.randint(0, 20, 15)
mask = (a % 3 == 0)
extract_from_a = a[mask]
extract_from_a
a[a % 3 == 0] = -1
a<jupyter_output><empty_output><jupyter_text>组合 1 2:<jupyter_code>a = np.arange(100)
is_prime = np.ones((100,), dtype=bool)
N_max = int(np.sqrt(len(is_prime)))
for j in range(2, N_max):
    is_prime[2*j::j]=False
primes = a[is_prime]
primes<jupyter_output><empty_output><jupyter_text>3. flat:<jupyter_code>x = np.reshape(np.arange(25.0), (5,5))
x
x[0]
x.flat[0]
x.flat[12:15]<jupyter_output><empty_output><jupyter_text>运算 1. 按行、列求和<jupyter_code>x = np.array([[1,1],[2,2]])
x
x.sum(axis=0)  # 按列求和，即一列列地求和
x.sum(axis=1)  # 按行求和，即一行行地求和<jupyter_output><empty_output><jupyter_text>2. Broadcasting  求城市两两之间的距离<jupyter_code>mileposts = np.array([0, 198, 303, 736, 871, 1175, 1475, 1544, 1913, 2448])
distance_array = np.abs(mileposts - mileposts[:, np.newaxis])  # 广播
distance_array<jupyter_output><empty_output><jupyter_text>求 $10\times 10$ 网格上某格点到原点的距离<jupyter_code>x, y = np.arange(5), np.arange(5)[:, np.newaxis]
distance = np.sqrt(x ** 2 + y ** 2)  # 广播
distance
plt.pcolor(distance)
plt.colorbar()<jupyter_output><empty_output><jupyter_text>使用numpy.ogrid简化<jupyter_code>x, y = np.ogrid[0:5, 0:5]
x, y
distance = np.sqrt(x ** 2 + y ** 2)
distance<jupyter_output><empty_output><jupyter_text>使用numpy.mgrid提供完整的（广播后的）矩阵<jupyter_code>x, y = np.mgrid[0:4, 0:4]
x
y<jupyter_output><empty_output><jupyter_text>3. 矩阵乘法(Python >= 3.5)<jupyter_code>x = np.array([[1.0, 2], [3, 2], [3, 4]])
y = np.array([[9.0, 8], [7, 6]])
x @ y
z = np.asmatrix(x)
w = np.asmatrix(y)
z * w
x * y  # This will not work<jupyter_output><empty_output><jupyter_text>4. 矩阵指数<jupyter_code>x = np.array([[2, 2, 2], [4, 5, 6], [7, 8, 9]])
x ** 3  # element-by-element
y = np.asmatrix(x)
y ** 3  # y*y*y<jupyter_output><empty_output><jupyter_text>5. 矩阵转置<jupyter_code>x = np.asmatrix(randn(2, 2))
x
x.T<jupyter_output><empty_output><jupyter_text>Maskedarray 1. 处理缺失数据<jupyter_code>x = np.ma.array([1, 2, 3, 4], mask=[0, 1, 0, 1])
x
y = np.ma.array([1, 2, 3, 4], mask=[0, 1, 1, 1])
x + y
np.ma.sqrt([1, -1, 2, -2])<jupyter_output><empty_output><jupyter_text>多项式 1. Polynomials<jupyter_code>p = np.poly1d([3, 2, -1])
p(0)
p.roots
p.order<jupyter_output><empty_output><jupyter_text>多项式拟合<jupyter_code>x = np.linspace(0, 1, 20)
y = np.cos(x) + 0.3*np.random.rand(20)
p = np.poly1d(np.polyfit(x, y, 3))
t = np.linspace(0, 1, 200)
plt.plot(x, y, 'o', t, p(t), '-')<jupyter_output><empty_output><jupyter_text>Baisc Functions and Numerical Indexing  1. 行向量、列向量<jupyter_code>r_[0:10:1]
r_[0:10:5j]
c_[0:5:2]
c_[0:5:4j]<jupyter_output><empty_output><jupyter_text>2. 子阵<jupyter_code>ix_([2, 3], [0, 1, 2])  # (列向量, 行向量) from array
x = reshape(arange(25.0), (5, 5))
x
x[ix_([2, 3], [0, 1, 2])]
ogrid[0:3, 0:2:.5]  # from range/linspace<jupyter_output><empty_output><jupyter_text>3. meshgrid<jupyter_code>x = arange(5)
y = arange(5)
X, Y = meshgrid(x, y)  # from array, axis=0 first
X
Y
mgrid[0:3, 0:2:.5]  # from range/linspace, but axis=1 first<jupyter_output><empty_output><jupyter_text>4. Rouding 舍入<jupyter_code>x = randn(3)
x
around(x)  # 最接近整数/四舍五入
around(x, 2)
x.round(2)  # The same
floor(x)  # 下取整
ceil(x)  # 上取整<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2006783493499152, 'fraction_numerical': 0.06359525155455059, 'mean_word_length': 6.450526315789474, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '499777', 'license': 'MIT', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1901, 'n_words': 288, 'path': 'notebooks/learn_numpy.ipynb', 'repo_name': 'AshWorkshop/Modash', 'size': '3538'}