目录
参数
sklearn.preprocessing.MinMaxScaler(feature_range=(0, 1), *, copy=True, clip=False)
将数据缩放在指定范围内
机器学习中,将数据缩放从而标准化,有利于模型的拟合,减小预测误差,增加模型的准确性
feature_range
tuple (min, max), default=(0, 1)
数据标准化的数据范围
copy
bool, default=True
设置为False,直接对原数据进行操作
clip
bool, default=False
属性
min_
ndarray of shape (n_features,)
每个特征相对于最小值的调整
数值上等于
KaTeX parse error: Expected group after '_' at position 39: …0) * self.scale_̲
scale_
ndarray of shape (n_features,)
每个特征的相对缩放值,数值上等于
s
c
a
l
e
_
=
(
m
a
x
−
m
i
n
)
/
(
X
.
m
a
x
(
a
x
i
s
=
0
)
−
X
.
m
i
n
(
a
x
i
s
=
0
)
)
scale\_=(max - min) / (X.max(axis=0) - X.min(axis=0))
scale_=(max−min)/(X.max(axis=0)−X.min(axis=0))
data_min_
ndarray of shape (n_features,)
每个特征的最小值
data_max_
ndarray of shape (n_features,)
每个特征的最大值
data_range_
ndarray of shape (n_features,)
n_features_in_
int
拟合时的特征数量
n_samples_seen_
int
数据缩放器处理的样本数量
feature_names_in_
ndarray of shape (n_features_in_,)
拟合时的特征名称
方法
fit(X, y=None)
计算数据中的最大值和最小值用于后面的数据拟合
fit_transform(X, y=None, **fit_params)
拟合数据,并转化原始数据
get_feature_names_out(input_features=None)
返回特征名称
get_params(deep=True)
返回数据缩放器的参数
set_params(**params)
设置数据缩放器的参数
transform(X)
根据特征范围缩放数据
应用实例
简单的数据
from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler()
data=[[1,2,3],[4,5,6],[7,8,9]]
new_data=scaler.fit_transform(data)
new_data
>>> array([[0. , 0. , 0. ],
[0.5, 0.5, 0.5],
[1. , 1. , 1. ]])
data1=[[-1, 2], [-0.5, 6], [0, 10], [1, 18],[3,5],[7,54]]
new_data1=scaler.fit_transform(data1)
new_data1
>>> array([[0. , 0. ],
[0.0625 , 0.07692308],
[0.125 , 0.15384615],
[0.25 , 0.30769231],
[0.5 , 0.05769231],
[1. , 1. ]])
真实的数据集
from sklearn.datasets import load_digits
x,y=load_digits(return_X_y=True)
x.shape,y.shape
>>> ((1797, 64), (1797,))
x[0]
>>> array([ 0., 0., 5., 13., 9., 1., 0., 0., 0., 0., 13., 15., 10.,
15., 5., 0., 0., 3., 15., 2., 0., 11., 8., 0., 0., 4.,
12., 0., 0., 8., 8., 0., 0., 5., 8., 0., 0., 9., 8.,
0., 0., 4., 11., 0., 1., 12., 7., 0., 0., 2., 14., 5.,
10., 12., 0., 0., 0., 0., 6., 13., 10., 0., 0., 0.])
new_x0=scaler.fit_transform(x[0].reshape(-1,1))
new_x0
>>> array([[0. ],
[0. ],
[0.33333333],
[0.86666667],
[0.6 ],
[0.06666667],
[0. ],
[0. ],
[0. ],
[0. ],
[0.86666667],
[1. ],
[0.66666667],
[1. ],
[0.33333333],
[0. ],
[0. ],
[0.2 ],
[1. ],
[0.13333333],
[0. ],
[0.73333333],
[0.53333333],
[0. ],
[0. ],
[0.26666667],
[0.8 ],
[0. ],
[0. ],
[0.53333333],
[0.53333333],
[0. ],
[0. ],
[0.33333333],
[0.53333333],
[0. ],
[0. ],
[0.6 ],
[0.53333333],
[0. ],
[0. ],
[0.26666667],
[0.73333333],
[0. ],
[0.06666667],
[0.8 ],
[0.46666667],
[0. ],
[0. ],
[0.13333333],
[0.93333333],
[0.33333333],
[0.66666667],
[0.8 ],
[0. ],
[0. ],
[0. ],
[0. ],
[0.4 ],
[0.86666667],
[0.66666667],
[0. ],
[0. ],
[0. ]])