预期2D阵列,得到1D阵列

时间:2019-03-26 23:33:39

标签: python pandas numpy keras sklearn-pandas

我正在从github运行以下代码,但出现错误。怎么了?

https://github.com/susanli2016/Machine-Learning-with-Python/blob/master/Time%20Series%20ANN%20%26%20LSTM%20VIX.ipynb

单元格:

# scale train and test data to [-1, 1]
scaler = MinMaxScaler(feature_range=(-1, 1))
train_sc = scaler.fit_transform(train)
test_sc = scaler.transform(test)

错误:

ValueError: Expected 2D array, got 1D array instead:
array=[17.24     18.190001 19.219999 ... 10.47     10.18     11.04    ].
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.

2 个答案:

答案 0 :(得分:2)

制造该笔记本的人正在使用旧版本的sklearn。简而言之,当您的功能本来应该是[row_1, row_2...row_n]形式时,它们的形式就是[[row_1], [row_2]...[row_n]]

相应地,使用此:

new_shape = (len(train), 1)

train_sc = scaler.fit_transform(np.reshape(train, new_shape))
test_sc = scaler.transform(np.reshape(test, new_shape))

答案 1 :(得分:0)

解决了添加以下方法的问题,该方法显然将训练对象和测试对象转换为numpy数组。正确吗?

scaler = MinMaxScaler(feature_range=(-1, 1))
train_sc = scaler.fit_transform(train.values.reshape(-1, 1))
test_sc = scaler.transform(test.values.reshape(-1,1))