本文整理匯總了Python中sklearn.preprocessing.data.MinMaxScaler.fit_transform方法的典型用法代碼示例。如果您正苦於以下問題:Python MinMaxScaler.fit_transform方法的具體用法?Python MinMaxScaler.fit_transform怎麽用?Python MinMaxScaler.fit_transform使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.preprocessing.data.MinMaxScaler
的用法示例。
在下文中一共展示了MinMaxScaler.fit_transform方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_min_max_scaler_iris
# 需要導入模塊: from sklearn.preprocessing.data import MinMaxScaler [as 別名]
# 或者: from sklearn.preprocessing.data.MinMaxScaler import fit_transform [as 別名]
def test_min_max_scaler_iris():
X = iris.data
scaler = MinMaxScaler()
# default params
X_trans = scaler.fit_transform(X)
assert_array_almost_equal(X_trans.min(axis=0), 0)
assert_array_almost_equal(X_trans.min(axis=0), 0)
assert_array_almost_equal(X_trans.max(axis=0), 1)
X_trans_inv = scaler.inverse_transform(X_trans)
assert_array_almost_equal(X, X_trans_inv)
# not default params: min=1, max=2
scaler = MinMaxScaler(feature_range=(1, 2))
X_trans = scaler.fit_transform(X)
assert_array_almost_equal(X_trans.min(axis=0), 1)
assert_array_almost_equal(X_trans.max(axis=0), 2)
X_trans_inv = scaler.inverse_transform(X_trans)
assert_array_almost_equal(X, X_trans_inv)
# min=-.5, max=.6
scaler = MinMaxScaler(feature_range=(-.5, .6))
X_trans = scaler.fit_transform(X)
assert_array_almost_equal(X_trans.min(axis=0), -.5)
assert_array_almost_equal(X_trans.max(axis=0), .6)
X_trans_inv = scaler.inverse_transform(X_trans)
assert_array_almost_equal(X, X_trans_inv)
# raises on invalid range
scaler = MinMaxScaler(feature_range=(2, 1))
assert_raises(ValueError, scaler.fit, X)
示例2: test_min_max_scaler_zero_variance_features
# 需要導入模塊: from sklearn.preprocessing.data import MinMaxScaler [as 別名]
# 或者: from sklearn.preprocessing.data.MinMaxScaler import fit_transform [as 別名]
def test_min_max_scaler_zero_variance_features():
"""Check min max scaler on toy data with zero variance features"""
X = [[0., 1., +0.5],
[0., 1., -0.1],
[0., 1., +1.1]]
X_new = [[+0., 2., 0.5],
[-1., 1., 0.0],
[+0., 1., 1.5]]
# default params
scaler = MinMaxScaler()
X_trans = scaler.fit_transform(X)
X_expected_0_1 = [[0., 0., 0.5],
[0., 0., 0.0],
[0., 0., 1.0]]
assert_array_almost_equal(X_trans, X_expected_0_1)
X_trans_inv = scaler.inverse_transform(X_trans)
assert_array_almost_equal(X, X_trans_inv)
X_trans_new = scaler.transform(X_new)
X_expected_0_1_new = [[+0., 1., 0.500],
[-1., 0., 0.083],
[+0., 0., 1.333]]
assert_array_almost_equal(X_trans_new, X_expected_0_1_new, decimal=2)
# not default params
scaler = MinMaxScaler(feature_range=(1, 2))
X_trans = scaler.fit_transform(X)
X_expected_1_2 = [[1., 1., 1.5],
[1., 1., 1.0],
[1., 1., 2.0]]
assert_array_almost_equal(X_trans, X_expected_1_2)
示例3: pearson
# 需要導入模塊: from sklearn.preprocessing.data import MinMaxScaler [as 別名]
# 或者: from sklearn.preprocessing.data.MinMaxScaler import fit_transform [as 別名]
def pearson(A, B, scale=True):
correlation = 0
if scale:
scaler = MinMaxScaler()
A = scaler.fit_transform(A)
B = scaler.fit_transform(B)
for i in range(A.shape[1]):
correlation = correlation + pearsonr(A[:, i], B[:, i])[0]
return correlation / A.shape[1]