我想使用sklearn
中的MinMaxScaler
来对测试和训练数据进行缩放,以便进行分析。
我一直在按照一个教程(https://mc.ai/an-introduction-on-time-series-forecasting-with-simple-neura-networks-lstm/)进行操作,但得到了错误信息ValueError: Expected 2D array, got 1D array instead
。
我尝试查看了Print predict ValueError: Expected 2D array, got 1D array instead,但如果我尝试train = train.reshape(-1, 1)
或test = test.reshape(-1, 1)
,因为它们是序列,会得到错误信息AttributeError: 'Series' object has no attribute 'reshape'
。
我该如何最好地解决这个问题?
# 导入库
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
# 创建最小工作示例数据集
data = [['1981-11-03', 510], ['1982-11-03', 540], ['1983-11-03', 480],
['1984-11-03', 490], ['1985-11-03', 492], ['1986-11-03', 380],
['1987-11-03', 440], ['1988-11-03', 640], ['1989-11-03', 560],
['1990-11-03', 660], ['1991-11-03', 610], ['1992-11-03', 480]]
df = pd.DataFrame(data, columns = ['Date', 'Tickets'])
# 将'Date'设置为日期时间类型
df['Date'] = pd.to_datetime(df['Date'])
# 将'Date'设置为索引
df = df.set_index(['Date'], drop=True)
# 将数据集拆分为训练和测试
split_date = pd.Timestamp('1989-11-03')
df = df['Tickets']
train = df.loc[:split_date]
test = df.loc[split_date:]
# 缩放训练和测试数据
scaler = MinMaxScaler(feature_range=(-1, 1))
train_sc = scaler.fit_transform(train)
test_sc = scaler.transform(test)
X_train = train_sc[:-1]
y_train = train_sc[1:]
X_test = test_sc[:-1]
y_test = test_sc[1:]
# 错误信息
ValueError: Expected 2D array, got 1D array instead: array=[510. 540. 480. 490. 492. 380. 440. 640. 560.]. 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.
回答:
这一行代码
df = df['Tickets']
将你的数据转换成了pd.Series。
如果你想得到一个数据框,可以使用
df = df[['Tickets']]
这应该能解决你的问题;数据框可以直接输入到缩放器的拟合函数中,而不需要重塑。