我正在尝试使用sklearn MinMaxScaler重新缩放如下所示的python列:
scaler = MinMaxScaler()
y = scaler.fit(df['total_amount'])
但是出现以下错误:
Traceback (most recent call last):
File "/Users/edamame/workspace/git/my-analysis/experiments/my_seq.py", line 54, in <module>
y = scaler.fit(df['total_amount'])
File "/Users/edamame/workspace/git/my-analysis/venv/lib/python3.4/site-packages/sklearn/preprocessing/data.py", line 308, in fit
return self.partial_fit(X, y)
File "/Users/edamame/workspace/git/my-analysis/venv/lib/python3.4/site-packages/sklearn/preprocessing/data.py", line 334, in partial_fit
estimator=self, dtype=FLOAT_DTYPES)
File "/Users/edamame/workspace/git/my-analysis/venv/lib/python3.4/site-packages/sklearn/utils/validation.py", line 441, in check_array
"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[3.180000e+00 2.937450e+03 6.023850e+03 2.216292e+04 1.074589e+04
:
0.000000e+00 0.000000e+00 9.000000e+01 1.260000e+03].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
有什么想法吗?
答案 0 :(得分:3)
MinMaxScaler的输入必须是类似shape [n_samples, n_features]
的数组。因此,您可以将其作为 dataframe 而不是 series (使用双方括号而不是单个方括号)应用于列:
y = scaler.fit(df[['total_amount']])
通过您的描述,听起来您想fit_transform
,而不只是fit
(但我可能是错的):
y = scaler.fit_transform(df[['total_amount']])
更多说明:
如果您的数据框有100行,请在将列转换为数组时考虑形状的差异:
>>> np.array(df[['total_amount']]).shape
(100, 1)
>>> np.array(df['total_amount']).shape
(100,)
第一个返回与[n_samples, n_features]
匹配的形状(MinMaxScaler要求),而第二个不匹配。
答案 1 :(得分:0)
尝试用这种方式:
import pandas as pd
from sklearn import preprocessing
x = df.values #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df = pd.DataFrame(x_scaled)