python sklearn库中的归一化方法
时间: 2023-10-29 12:05:50 浏览: 89
sklearn库中提供了两种常用的归一化方法:MinMaxScaler和StandardScaler。
1. MinMaxScaler:将数据缩放到指定的最小值和最大值之间,默认为0-1范围内。公式为:
X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
X_scaled = X_std * (max - min) + min
示例代码:
```
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
```
2. StandardScaler:将数据缩放到均值为0,方差为1的标准正态分布中。公式为:
X_std = (X - X.mean(axis=0)) / X.std(axis=0)
示例代码:
```
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
```
阅读全文