<jupyter_start><jupyter_text>NumPy 基本使用 ndarray  array & matrix 创建 array & matrix<jupyter_code>import numpy as np

array_1d = np.array([1, 2, 3])
matrix = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])

print(array_1d.dtype)
print(matrix.dtype)

print(array_1d.shape)
print(matrix.shape)

# modify

matrix[1][0] = 6

print(matrix)<jupyter_output>int64
int64
(3,)
(3, 3)
[[1 2 3]
 [6 5 6]
 [7 8 9]]<jupyter_text>构建 array<jupyter_code>import numpy as np


person_type = np.dtype({
    'names':['name', 'chinese', 'english', 'mathematics'],
    'formats':['S32', 'i', 'i', 'i']
})
peoples = np.array(
    [("zhangfei", 66, 65, 30),
    ("guanyu", 95, 85, 98),
    ("zhaoyun", 93, 92, 96),
    ("huangzhong", 90, 88, 77),
    ("dianwei", 80, 90, 90)
    ],
    dtype = person_type
)

chinese = peoples[:]['chinese']
english = peoples[:]['english']
math = peoples[:]['mathematics']

print(np.mean(chinese))
print(np.mean(english))
print(np.mean(math))<jupyter_output>84.8
84.0
78.2<jupyter_text>ufunc (universal function object) 连续数组创建<jupyter_code>import numpy as np


array_range = np.arange(1, 11, 2)
array_linear = np.linspace(1, 9, 5)

print(array_range)
print(array_linear)<jupyter_output>[1 3 5 7 9]
[1. 3. 5. 7. 9.]<jupyter_text>数组运算<jupyter_code>import numpy as np


array_range = np.arange(1, 11, 2)
array_linear = np.linspace(1, 9, 5)

print(np.add(array_range, array_linear))
print(np.subtract(array_range, array_linear))
print(np.multiply(array_range, array_linear))
print(np.divide(array_range, array_linear))
print(np.power(array_range, array_linear))
# array_range % array_linear
print(np.remainder(array_range, array_linear))
print(np.mod(array_range, array_linear))<jupyter_output>[ 2.  6. 10. 14. 18.]
[0. 0. 0. 0. 0.]
[ 1.  9. 25. 49. 81.]
[1. 1. 1. 1. 1.]
[1.00000000e+00 2.70000000e+01 3.12500000e+03 8.23543000e+05
 3.87420489e+08]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]<jupyter_text>统计函数 最大值 amax 最小值amin<jupyter_code>import numpy as np


matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print(np.amin(matrix))
# columns based
print(np.amin(matrix, 0))
# row based
print(np.amin(matrix, 1))

print(np.amax(matrix))
print(np.amax(matrix, 0))
print(np.amax(matrix, 1))<jupyter_output>1
[1 2 3]
[1 4 7]
9
[7 8 9]
[3 6 9]<jupyter_text>最大值与最小值的差ptp<jupyter_code>import numpy as np

matrix = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
print(np.ptp(matrix))
print(np.ptp(matrix, axis=0))
print(np.ptp(matrix, axis=1))<jupyter_output>8
[6 6 6]
[2 2 2]<jupyter_text>百分位数<jupyter_code>import numpy as np

matrix = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])

print(np.percentile(matrix, 50))
print(np.percentile(matrix, 50, axis=0))
print(np.percentile(matrix, 50, axis=1))<jupyter_output>5.0
[4. 5. 6.]
[2. 5. 8.]<jupyter_text>中位数median和平均数mean<jupyter_code>import numpy as np

matrix = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])

print(np.median(matrix))
print(np.median(matrix, axis=0))
print(np.median(matrix, axis=1))
print(np.mean(matrix))
print(np.mean(matrix, axis=0))
print(np.mean(matrix, axis=1))<jupyter_output>5.0
[4. 5. 6.]
[2. 5. 8.]
5.0
[4. 5. 6.]
[2. 5. 8.]<jupyter_text>加权平均 average<jupyter_code>import numpy as np


array = np.array([1, 2, 3, 4])
weight_array = np.array([1, 2, 3, 4])
print(np.average(array))
print(np.average(array, weights=weight_array))<jupyter_output>2.5
3.0<jupyter_text>统计标准差std、方差var<jupyter_code>import numpy as np


array = np.array([1, 2, 3, 4])
print(np.std(array))
print(np.var(array))<jupyter_output>1.118033988749895
1.25<jupyter_text>排序<jupyter_code>import numpy as np


array_2d = np.array([[4, 3, 2],[2, 4, 1]])
print(np.sort(array_2d))
print("*" * 20)
print(np.sort(array_2d, axis=None))
print("*" * 20)
print(np.sort(array_2d, axis=0))
print("*" * 20)
print(np.sort(array_2d, axis=1))<jupyter_output>[[2 3 4]
 [1 2 4]]
********************
[1 2 2 3 4 4]
********************
[[2 3 1]
 [4 4 2]]
********************
[[2 3 4]
 [1 2 4]]
{'fraction_non_alphanumeric': 0.21347736625514405, 'fraction_numerical': 0.09439300411522633, 'mean_word_length': 5.303079416531604, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '44574', 'license': 'MIT', 'n_tokens_mistral': 2059, 'n_tokens_neox': 1820, 'n_words': 379, 'path': 'notebooks/basic_numpy_usage.ipynb', 'repo_name': 'nutshellfool/dataAnalysis', 'size': '3888'}