數(shù)據(jù)的預處理是數(shù)據(jù)分析,或者機器學習訓練前的重要步驟。
通過數(shù)據(jù)預處理,可以
- 提高數(shù)據(jù)質量,處理數(shù)據(jù)的缺失值、異常值和重復值等問題,增加數(shù)據(jù)的準確性和可靠性
- 整合不同數(shù)據(jù),數(shù)據(jù)的來源和結構可能多種多樣,分析和訓練前要整合成一個數(shù)據(jù)集
- 提高數(shù)據(jù)性能,對數(shù)據(jù)的值進行變換,規(guī)約等(比如無量綱化),讓算法更加高效
本篇介紹的標準化處理,可以消除數(shù)據(jù)之間的差異,使不同特征的數(shù)據(jù)具有相同的尺度,
以便于后續(xù)的數(shù)據(jù)分析和建模。
1. 原理
數(shù)據(jù)標準化的過程如下:
- 計算數(shù)據(jù)列的算術平均值(
mean
) - 計算數(shù)據(jù)列的標準差(
sd
) - 標準化處理:\(new\_data = (data - mean) / sd\)
data
是原始數(shù)據(jù),new_data
是標準化之后的數(shù)據(jù)。
根據(jù)原理,實現(xiàn)的對一維數(shù)據(jù)標準化的示例如下:
import numpy as np
# 標準化的實現(xiàn)原理
data = np.array([1, 2, 3, 4, 5])
mean = np.mean(data) # 平均值
sd = np.std(data) # 標準差
# 標準化
data_new = (data-mean)/sd
print("處理前: {}".format(data))
print("處理后: {}".format(data_new))
# 運行結果
處理前: [1 2 3 4 5]
處理后: [-1.41421356 -0.70710678 0. 0.70710678 1.41421356]
使用scikit-learn
庫中的標準化函數(shù)scale
,得到的結果也和上面一樣。
from sklearn import preprocessing as pp
data = np.array([1, 2, 3, 4, 5])
pp.scale(data)
# 運行結果
array([-1.41421356, -0.70710678, 0. , 0.70710678, 1.41421356])
scikit-learn
庫中的標準化函數(shù)scale
不僅可以處理一維的數(shù)據(jù),也可以處理多維的數(shù)據(jù)。
2. 作用
標準化處理的作用主要有:
2.1. 消除數(shù)據(jù)量級的影響
數(shù)據(jù)分析時,不一樣量級的數(shù)據(jù)放在一起分析會增加很多不必要的麻煩,比如下面三組數(shù)據(jù):
data_min = np.array([0.001, 0.002, 0.003, 0.004, 0.005])
data = np.array([1, 2, 3, 4, 5])
data_max = np.array([10000, 20000, 30000, 40000, 50000])
三組數(shù)據(jù)看似差距很大,但是標準化處理之后:
from sklearn import preprocessing as pp
print("data_min 標準化:{}".format(pp.scale(data_min)))
print("data 標準化:{}".format(pp.scale(data)))
print("data_max 標準化:{}".format(pp.scale(data_max)))
# 運行結果
data_min 標準化:[-1.41421356 -0.70710678 0. 0.70710678 1.41421356]
data 標準化:[-1.41421356 -0.70710678 0. 0.70710678 1.41421356]
data_max 標準化:[-1.41421356 -0.70710678 0. 0.70710678 1.41421356]
標準化處理之后,發(fā)現(xiàn)三組數(shù)據(jù)其實是一樣的。
將數(shù)據(jù)轉化為相同的尺度,使得不同變量之間的比較更加方便和有意義,避免對分析結果產(chǎn)生誤導。
2.2. 增強可視化效果
此外,標準化之后的數(shù)據(jù)可視化效果也會更好。
比如下面一個對比學生們數(shù)學和英語成績的折線圖:
math_scores = np.random.randint(0, 150, 10)
english_scores = np.random.randint(0, 100, 10)
fig, ax = plt.subplots(2, 1)
fig.subplots_adjust(hspace=0.4)
ax[0].plot(range(1, 11), math_scores, label="math")
ax[0].plot(range(1, 11), english_scores, label="english")
ax[0].set_ylim(0, 150)
ax[0].set_title("標準化之前")
ax[0].legend()
ax[1].plot(range(1, 11), pp.scale(math_scores), label="math")
ax[1].plot(range(1, 11), pp.scale(english_scores), label="english")
ax[1].set_title("標準化之后")
ax[1].legend()
plt.show()
隨機生成10
個數(shù)學和英語的成績,數(shù)學成績的范圍是0~150
,英語成績的范圍是0~100
。
標準化前后的折線圖對比如下:
標準化之前的對比,似乎數(shù)學成績要比英語成績好。
而從標準化之后的曲線圖來看,其實兩門成績是差不多的。
這就是標準化的作用,使得可視化結果更加準確和有意義。
2.3. 機器學習的需要
許多機器學習算法對輸入數(shù)據(jù)的規(guī)模和量綱非常敏感。
如果輸入數(shù)據(jù)的特征之間存在數(shù)量級差異,可能會影響算法的準確性和性能。
標準化處理可以將所有特征的數(shù)據(jù)轉化為相同的尺度,從而避免這種情況的發(fā)生,提高算法的準確性和性能。文章來源:http://www.zghlxwxcb.cn/news/detail-753124.html
3. 總結
總的來說,數(shù)據(jù)標準化處理是數(shù)據(jù)處理中不可或缺的一步,它可以幫助我們消除數(shù)據(jù)之間的差異,提高分析結果的性能和穩(wěn)定性,增加數(shù)據(jù)的可解釋性,從而提高我們的決策能力。文章來源地址http://www.zghlxwxcb.cn/news/detail-753124.html
到了這里,關于【scikit-learn基礎】--『預處理』之 標準化的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!