數據標準化處理
問題:
量綱不一:就是單位、特徵的單位不一致,不能放在一起比較
處理方法:
0-1標準化、Z標準化、normalizer歸一化
案例:
# -*- coding: utf-8 -*-
import pandas
import numpy
data=pandas.read_csv(
'D:\\DATA\\pycase\\number2\\6.1\\data1.csv'
)
## min-max(0-1)標準化
# 導入(0-1)標準化方法
from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler()
# 錯誤解決ValueError: Expected 2D array, got 1D array instead:
# array=[4742.92 3398. 2491.9 2149. 2070. ].
# Reshape your data either using array.reshape(-1, 1) if your data has a single feature # # or array.reshape(1, -1) if it contains a single sample.
### 使用array.reshape(-1, 1)重新調整你的數據)python3 加values
data['0-1標準化票房']=scaler.fit_transform(data['票房'].values.reshape(-1,1))
data['0-1標準化評分']=scaler.fit_transform(data['評分'].values.reshape(-1,1))
# Z-score 標準化
from sklearn.preprocessing import scale
data['Z標準化票房']=scale(data['票房'].values.reshape(-1,1))
data['Z標準化評分']=scale(data['評分'].values.reshape(-1,1))
## normalizer 歸一化
from sklearn.preprocessing import Normalizer
scaler=Normalizer()
data['歸一化票房']=scaler.fit_transform(
data['票房'].values.reshape(1,-1)
)[0]
data['歸一化評分']=scaler.fit_transform(
data['評分'].values.reshape(1,-1)
)[0]