MinMaxScaler


MinMaxScaler

一、總結

一句話總結:

MinMaxScaler是min、max歸一化,使用的話先fit,然后再transform歸一化操作,也可以合並為fit_transform
>>> from sklearn.preprocessing import MinMaxScaler
>>> data = [[-1, 2], [-0.5, 6], [0, 10], [1, 18]]
>>> scaler = MinMaxScaler()
>>> print(scaler.fit(data))
MinMaxScaler()
>>> print(scaler.data_max_)
[ 1. 18.]
>>> print(scaler.transform(data))
[[0.   0.  ]
 [0.25 0.25]
 [0.5  0.5 ]
 [1.   1.  ]]
>>> print(scaler.transform([[2, 2]]))
[[1.5 0. ]]

 

1、訓練集的歸一化方法為 scaler.fit_transform,驗證集和測試集的歸一化方法為scaler.transform?

壹、training_set_scaled = sc.fit_transform(training_set)  # 求得訓練集的最大值,最小值這些訓練集固有的屬性,並在訓練集上進行歸一化
貳、test_set = sc.transform(test_set)  # 利用訓練集的屬性對測試集進行歸一化
# 歸一化
sc = MinMaxScaler(feature_range=(0, 1))  # 定義歸一化:歸一化到(0,1)之間
print(sc)

MinMaxScaler(copy=True, feature_range=(0, 1))

-------------

training_set_scaled = sc.fit_transform(training_set)  # 求得訓練集的最大值,最小值這些訓練集固有的屬性,並在訓練集上進行歸一化
test_set = sc.transform(test_set)  # 利用訓練集的屬性對測試集進行歸一化
print(training_set_scaled[:5,])
print(test_set[:5,])

[[0.011711  ]
 [0.00980951]
 [0.00540518]
 [0.00590914]
 [0.00489135]]
[[0.84288404]
 [0.85345726]
 [0.84641315]
 [0.87046756]
 [0.86758781]]

 

 

二、MinMaxScaler

博客對應課程的視頻位置:

 

>>> from sklearn.preprocessing import MinMaxScaler
>>> data = [[-1, 2], [-0.5, 6], [0, 10], [1, 18]]
>>> scaler = MinMaxScaler()
>>> print(scaler.fit(data))
MinMaxScaler()
>>> print(scaler.data_max_)
[ 1. 18.]
>>> print(scaler.transform(data))
[[0.   0.  ]
 [0.25 0.25]
 [0.5  0.5 ]
 [1.   1.  ]]
>>> print(scaler.transform([[2, 2]]))
[[1.5 0. ]]

 

 

training_set_scaled = sc.fit_transform(training_set)  # 求得訓練集的最大值,最小值這些訓練集固有的屬性,並在訓練集上進行歸一化

Signature: sc.fit_transform(X, y=None, **fit_params)
Docstring:
Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params
and returns a transformed version of X.

Parameters
----------
X : numpy array of shape [n_samples, n_features]
    Training set.

y : numpy array of shape [n_samples]
    Target values.

**fit_params : dict
    Additional fit parameters.

Returns
-------
X_new : numpy array of shape [n_samples, n_features_new]
    Transformed array.

 

 

=================================================================================

training_set = maotai.iloc[0:2426 - 300, 2:3].values # 前(2426-300=2126)天的開盤價作為訓練集,表格從0開始計數,2:3 是提取[2:3)列,前閉后開,故提取出C列開盤價 test_set = maotai.iloc[2426 - 300:, 2:3].values # 后300天的開盤價作為測試集 print(training_set.shape) print(test_set.shape) 
(2126, 1)
(300, 1)
In [5]:
# 歸一化
sc = MinMaxScaler(feature_range=(0, 1)) # 定義歸一化:歸一化到(0,1)之間 print(sc) 
MinMaxScaler(copy=True, feature_range=(0, 1))
In [5]:
training_set_scaled = sc.fit_transform(training_set) # 求得訓練集的最大值,最小值這些訓練集固有的屬性,並在訓練集上進行歸一化 test_set = sc.transform(test_set) # 利用訓練集的屬性對測試集進行歸一化 print(training_set_scaled[:5,]) print(test_set[:5,]) 
[[0.011711  ]
 [0.00980951]
 [0.00540518]
 [0.00590914]
 [0.00489135]]
[[0.84288404]
 [0.85345726]
 [0.84641315]
 [0.87046756]
 [0.86758781]]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM