NumPy是Python中用于科学计算的重要库,提供了大量的数学和科学计算函数和工具,包括一系列的统计函数。在数据分析和机器学习等领域,统计函数是非常重要的一部分。
下面是NumPy中最常用9个统计函数:
以下是这些统计函数的使用方法。
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
mean = np.mean(arr)
print("Mean:", mean)
输出结果为:
Mean: 3.0
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
median = np.median(arr)
print("Median:", median)
输出结果为:
Median: 3.0
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
var = np.var(arr)
print("Variance:", var)
输出结果为:
Variance: 2.0
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
std = np.std(arr)
print("Standard deviation:", std)
输出结果为:
Standard deviation: 1.4142135623730951
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
min_val = np.min(arr)
print("Minimum value:", min_val)
输出结果为:
Minimum value: 1
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
max_val = np.max(arr)
print("Maximum value:", max_val)
输出结果为:
Maximum value: 5
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
percentile_25 = np.percentile(arr, 25)
percentile_50 = np.percentile(arr, 50)
percentile_75 = np.percentile(arr, 75)
print("25th percentile:", percentile_25)
print("50th percentile:", percentile_50)
print("75th percentile:", percentile_75)
输出结果为:
25th percentile: 2.0
50th percentile: 3.0
75th percentile: 4.0
import numpy as np
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([6, 7, 8, 9, 10])
corrcoef_matrix = np.corrcoef(arr1, arr2)
print("Correlation coefficient matrix:")
print(corrcoef_matrix)
输出结果为:
Correlation coefficient matrix:
[[1. 1.]
[1. 1.]]
本文链接:http://task.lmcjl.com/news/4377.html