Machine Learning 简明教程

Machine Learning - Standard Deviation

标准差是衡量一组数据值围绕其均值的变化或离散程度。在机器学习中,它是一个重要的统计概念,用于描述数据集的范围或分布。

Standard deviation is a measure of the amount of variation or dispersion of a set of data values around their mean. In machine learning, it is an important statistical concept that is used to describe the spread or distribution of a dataset.

标准差计算为方差的平方根,方差是每个数据点到均值的平方差的平均值。计算标准差的公式如下——

Standard deviation is calculated as the square root of the variance, which is the average of the squared differences from the mean. The formula for calculating standard deviation is as follows −

\sigma =\sqrt{\left [\Sigma \left ( x-\mu \right )^{2}/N \right ]}

其中——

Where −

  1. $\sigma$is the standard deviation

  2. $\Sigma$ is the sum of

  3. $x$ is the data point

  4. $\mu$ is the mean of the dataset

  5. $N$ is the total number of data points

在机器学习中,标准差用于理解数据集的可变性和检测异常点。例如,在金融领域,标准差用于衡量股票价格的波动性。在图像处理中,标准差用于检测图像噪声。

In machine learning, standard deviation is used to understand the variability of a dataset and to detect outliers. For example, in finance, standard deviation is used to measure the volatility of stock prices. In image processing, standard deviation can be used to detect image noise.

Types of Examples

Example 1

在这个例子中,我们将使用 NumPy 库来计算标准差——

In this example, we will be using the NumPy library to calculate the standard deviation −

import numpy as np

data = np.array([1, 2, 3, 4, 5, 6])
std_dev = np.std(data)

print('Standard deviation:', std_dev)

它将生成如下输出:

It will produce the following output −

Standard deviation: 1.707825127659933

Example 2

让我们看另一个例子,我们将使用 Python 和 Pandas 库计算鸢尾花数据集的每列的标准差——

Let’s see another example in which we will calculate the standard deviation of each column in Iris flower dataset using Python and Pandas library −

import pandas as pd

# load the iris dataset

iris_df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learningdatabases/iris/iris.data',
   names=['sepal length', 'sepal width', 'petal length', 'petal width', 'class'])

# calculate the standard deviation of each column
std_devs = iris_df.std()

# print the standard deviations
print('Standard deviations:')
print(std_devs)

在这个例子中,我们使用熊猫的 read_csv() 方法从 UCI 机器学习存储库加载鸢尾花数据集。然后我们使用 Pandas 数据框架的 std() 方法计算每列的标准差。最后,我们打印每列的标准差。

In this example, we load the Iris dataset from the UCI Machine Learning Repository using Pandas' read_csv() method. We then calculate the standard deviation of each column using the std() method of the Pandas dataframe. Finally, we print the standard deviations for each column.

执行代码后,您将获得以下输出——

On executing the code, you will get the following output −

Standard deviations:
sepal length    0.828066
sepal width     0.433594
petal length    1.764420
petal width     0.763161
dtype: float64

这个例子展示了如何使用标准差来理解数据集的可变性。在这个例子中,我们可以看到“花瓣长度”列的标准差远高于其他列,这表明这个特性可能更可变,并且可能更适合分类任务。

This example demonstrates how standard deviation can be used to understand the variability of a dataset. In this case, we can see that the standard deviation of the 'petal length' column is much higher than that of the other columns, which suggests that this feature may be more variable and potentially more informative for classification tasks.