📜  用Python描述一个 NumPy 数组

📅  最后修改于: 2022-05-13 01:54:53.669000             🧑  作者: Mango

用Python描述一个 NumPy 数组

NumPy 是一个用于数值计算的Python库。它提供强大的多维数组作为Python对象以及各种数学函数。在本文中,我们将介绍用于数组描述性分析的所有基本 NumPy 函数。让我们首先为我们的分析初始化一个样本数组。

以下代码初始化一个 NumPy 数组:

Python3
import numpy as np
 
 
# sample array
arr = np.array([4, 5, 8, 5, 6, 4,
                9, 2, 4, 3, 6])
print(arr)


Python3
import numpy as np
 
 
arr = np.array([4, 5, 8, 5, 6, 4,
                9, 2, 4, 3, 6])   
 
# measures of central tendency
mean = np.mean(arr)
median = np.median(arr)
 
print("Array =", arr)
print("Mean =", mean)
print("Median =", median)


Python3
import numpy as np
 
 
arr = np.array([4, 5, 8, 5, 6, 4,
                9, 2, 4, 3, 6])
 
# measures of dispersion
min = np.amin(arr)
max = np.amax(arr)
range = np.ptp(arr)
variance = np.var(arr)
sd = np.std(arr)
 
print("Array =", arr)
print("Measures of Dispersion")
print("Minimum =", min)
print("Maximum =", max)
print("Range =", range)
print("Variance =", variance)
print("Standard Deviation =", sd)


Python3
import numpy as np 
 
 
arr = np.array([4, 5, 8, 5, 6, 4,
                9, 2, 4, 3, 6])   
 
# measures of central tendency
mean = np.mean(arr)
median = np.median(arr)
 
# measures of dispersion
min = np.amin(arr)
max = np.amax(arr)
range = np.ptp(arr)
variance = np.var(arr)
sd = np.std(arr)
 
print("Descriptive analysis")
print("Array =", arr)
print("Measures of Central Tendency")
print("Mean =", mean)
print("Median =", median)
print("Measures of Dispersion")
print("Minimum =", min)
print("Maximum =", max)
print("Range =", range)
print("Variance =", variance)
print("Standard Deviation =", sd)


输出:

[4 5 8 5 6 4 9 2 4 3 6]

为了描述我们的 NumPy 数组,我们需要找到两种类型的统计信息:

  • 集中趋势的度量。
  • 分散措施。

集中趋势的度量

以下方法用于在 NumPy 中找到集中趋势的度量:

  • mean()-将 NumPy 数组作为参数并返回数据的算术平均值。
np.mean(arr)
  • median()-将 NumPy 数组作为参数并返回数据的中位数。
np.median(arr)

以下示例说明了 mean() 和 median() 方法的用法。

例子:

Python3

import numpy as np
 
 
arr = np.array([4, 5, 8, 5, 6, 4,
                9, 2, 4, 3, 6])   
 
# measures of central tendency
mean = np.mean(arr)
median = np.median(arr)
 
print("Array =", arr)
print("Mean =", mean)
print("Median =", median)

输出:

Array = [4 5 8 5 6 4 9 2 4 3 6]
Mean = 5.09090909091
Median = 5.0

分散测量

以下方法用于查找 NumPy 中的分散度量:

  • amin() -它将 NumPy 数组作为参数并返回最小值。
np.amin(arr)
  • amax() -它将 NumPy 数组作为参数并返回最大值。
np.amax(arr)
  • ptp() -它将 NumPy 数组作为参数并返回数据的范围。
np.ptp(arr)
  • var() -它将 NumPy 数组作为参数并返回数据的方差。
np.var(arr)
  • std() -它将 NumPy 数组作为参数并返回数据的标准变体。
np.std(arr)

示例:以下代码说明了 amin()、amax()、ptp()、var() 和 std() 方法。

Python3

import numpy as np
 
 
arr = np.array([4, 5, 8, 5, 6, 4,
                9, 2, 4, 3, 6])
 
# measures of dispersion
min = np.amin(arr)
max = np.amax(arr)
range = np.ptp(arr)
variance = np.var(arr)
sd = np.std(arr)
 
print("Array =", arr)
print("Measures of Dispersion")
print("Minimum =", min)
print("Maximum =", max)
print("Range =", range)
print("Variance =", variance)
print("Standard Deviation =", sd)

输出:

Array = [4 5 8 5 6 4 9 2 4 3 6]
Measures of Dispersion
Minimum = 2
Maximum = 9
Range = 7
Variance = 3.90082644628
Standard Deviation = 1.9750509984

示例:现在我们可以结合上述示例来对我们的数组进行完整的描述性分析。

Python3

import numpy as np 
 
 
arr = np.array([4, 5, 8, 5, 6, 4,
                9, 2, 4, 3, 6])   
 
# measures of central tendency
mean = np.mean(arr)
median = np.median(arr)
 
# measures of dispersion
min = np.amin(arr)
max = np.amax(arr)
range = np.ptp(arr)
variance = np.var(arr)
sd = np.std(arr)
 
print("Descriptive analysis")
print("Array =", arr)
print("Measures of Central Tendency")
print("Mean =", mean)
print("Median =", median)
print("Measures of Dispersion")
print("Minimum =", min)
print("Maximum =", max)
print("Range =", range)
print("Variance =", variance)
print("Standard Deviation =", sd)

输出:

Descriptive analysis
Array = [4 5 8 5 6 4 9 2 4 3 6]
Measurements of Central Tendency
Mean = 5.09090909091
Median = 5.0
Minimum = 2
Maximum = 9
Range = 7
Variance = 3.90082644628
Standard Deviation = 1.9750509984