python sklearn 資料預處理 範圍縮放

2021-10-08 20:12:49 字數 1099 閱讀 7278

"""

資料預處理 最大最小值縮放

"""import numpy as np

import sklearn.preprocessing as sp

# 準備資料

raw_samples = np.array([[

17,90,

4000],

[20,80

,5000],

[23,75

,5500]]

)# 使用函式 範圍縮放

mms = sp.minmaxscaler(feature_range=(0

,1))

result = mms.fit_transform(raw_samples)

print

("------------函式實現 範圍縮放\n"

, result)

# 手動實現 範圍縮放

new_samples =

for row in raw_samples.t:

# 轉置 對列處理

min_val = np.

min(row)

max_val = np.

max(row)

# 整理解方程需要的 a, b 線性回歸解方程

a = np.array(

[[min_val,1]

,[max_val,1]

])b = np.array([0

,1])

x = np.linalg.solve(a, b)

# 求解結果 x[0]= k x[1]=b

new_row = row * x[0]

+ x[1]

# y = kx + b

print

("---------手動實現 範圍縮放\n"

, np.array(new_samples)

.t)# 再次轉置 顯示結果

Python sklearn庫 資料預處理

python sklearn庫 資料預處理 資料集轉換之預處理資料 將輸入的資料轉化成機器學習演算法可以使用的資料。包含特徵提取和標準化。原因 資料集的標準化 服從均值為0方差為1的標準正態分佈 高斯分布 是大多數機器學習演算法的常見要求。如果原始資料不服從高斯分布,在 時表現可能不好。在實踐中,我...

Python sklearn 交叉驗證

from sklearn.datasets import load boston from sklearn.model selection import cross val score from sklearn.tree import decisiontreeregressor boston loa...

python sklearn庫實現簡單邏輯回歸

import xlrd import matplotlib.pyplot as plt import numpy as np from sklearn import model selection from sklearn.linear model import logisticregression...