<jupyter_start><jupyter_text>08 ``Numpy`` 中的聚合操作  sum<jupyter_code>import numpy as np

L = np.random.random(100)
sum(L)
np.sum(L)
np.random.rand(10)
big_array = np.random.rand(1000000)
%timeit sum(big_array)
%timeit np.sum(big_array)<jupyter_output>61.5 ms ± 409 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
309 µs ± 4.48 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)<jupyter_text>min, max<jupyter_code>np.min(big_array)
np.max(big_array)
big_array.min()
big_array.max()
big_array.sum()<jupyter_output><empty_output><jupyter_text>多维度聚合<jupyter_code>X = np.arange(16).reshape(4,-1)
X
np.sum(X)
np.sum(X, axis=0)<jupyter_output><empty_output><jupyter_text>沿着第一个维度，行的方向进行计算，得到的是每一列相加的值。<jupyter_code>np.sum(X, axis=1)<jupyter_output><empty_output><jupyter_text>注意：axis描述的是将要被压缩的维度。  其他聚合操作<jupyter_code>np.prod(X)<jupyter_output><empty_output><jupyter_text>得到所有元素的乘积<jupyter_code>np.prod(X + 1)
np.mean(X)
np.median(X)<jupyter_output><empty_output><jupyter_text>中位数<jupyter_code>v = np.array([1, 1, 2, 2, 10])
np.mean(v)
np.median(v)
np.percentile(big_array, q=50)<jupyter_output><empty_output><jupyter_text>统计学中的50%，百分位<jupyter_code>np.median(big_array)
np.percentile(big_array, q=100)
np.max(big_array)
for percent in [0, 25, 50, 75, 100]:
    print(np.percentile(big_array, q=percent))
np.var(big_array)<jupyter_output><empty_output><jupyter_text>var 求方差<jupyter_code>np.std(big_array)<jupyter_output><empty_output><jupyter_text>std 求标准差<jupyter_code>x = np.random.normal(0, 1, 1000000)<jupyter_output><empty_output><jupyter_text>均值为0，标准差为1<jupyter_code>np.mean(x)
np.std(x)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1585740626920713, 'fraction_numerical': 0.046097111247695145, 'mean_word_length': 9.781456953642385, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '98837', 'license': 'Apache-2.0', 'n_tokens_mistral': 870, 'n_tokens_neox': 823, 'n_words': 95, 'path': '03-Jupyter-Notebook-Numpy-and-Matplotlib/08-Aggregation-Operator-in-Numpy/08-Aggregation-Operator-in-Numpy.ipynb', 'repo_name': 'mtianyan/Mtianyan-Play-with-Machine-Learning-Algorithms', 'size': '1627'}