python sklearn 数据预处理 范围缩放
"""
数据预处理 最大最小值缩放
"""
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
new_samples.append(new_row)
print("---------手动实现 范围缩放\n", np.array(new_samples).T) # 再次转置 显示结果