python - 如何使用 Numpy reshape ?

标签 python numpy reshape

我有这个数据:

Date,Open,High,Low,Close,Adj Close,Volume
2007-01-03,12.160000,12.750000,11.530000,12.040000,12.040000,0
2007-01-04,12.400000,12.420000,11.280000,11.510000,11.510000,0
2007-01-05,11.840000,12.250000,11.680000,12.140000,12.140000,0
2007-01-08,12.480000,12.830000,11.780000,12.000000,12.000000,0
2007-01-09,11.860000,12.470000,11.690000,11.910000,11.910000,0
2007-01-10,12.340000,12.500000,11.430000,11.470000,11.470000,0
2007-01-11,11.420000,11.480000,10.500000,10.870000,10.870000,0
2007-01-12,10.930000,10.930000,10.140000,10.150000,10.150000,0
2007-01-16,10.640000,10.890000,10.400000,10.740000,10.740000,0
2007-01-17,10.900000,10.900000,10.350000,10.590000,10.590000,0
2007-01-18,10.650000,11.040000,10.450000,10.850000,10.850000,0
2007-01-19,10.800000,11.030000,10.240000,10.400000,10.400000,0
2007-01-22,10.770000,11.080000,10.620000,10.770000,10.770000,0
2007-01-23,10.770000,10.940000,10.220000,10.340000,10.340000,0

我有这段代码可以运行一些时间序列预测

import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import r2_score
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam
from keras.layers import LSTM
df = pd.read_csv("^VIX.csv")
df.drop(['Open', 'High', 'Low', 'Close', 'Volume'], axis=1, inplace=True)
df['Date'] = pd.to_datetime(df['Date'])
df = df.set_index(['Date'], drop=True)
split_date = pd.Timestamp('2016-01-01')
df =  df['Adj Close']
train = df.loc[:split_date]
test = df.loc[split_date:]
plt.figure(figsize=(10, 6))
ax = train.plot()
test.plot(ax=ax)
plt.legend(['train', 'test']);

到目前为止一切顺利,但在运行时

# 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=[12.04
11.51 12.14 ... 16.08 17.290001 18.209999]. 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.

虽然这是原始代码,但看起来 reshape 没有正确完成,我在 numpay reshape 中遗漏了一些东西。

我应该在 reshape 中修复什么? 谢谢!

最佳答案

针对您的特定情况的解决方案:

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

关于python - 如何使用 Numpy reshape ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54679026/

相关文章:

python - NumPy 二维数组 : selecting indices in a circle

python - Scipy odeint 非负解

r - Melt 和 dcast with/字符串连接

numpy - 如何消除循环并使用 numpy reshape?

python - 为特定键订购字典

python - 如何使用docker进行部署开发?

python - 在 Python TKinter 中创建弹出窗口时禁用底层窗口

python - 如何调用 Numpy 数组中的元素?

将数据框 reshape 为平均值堆栈

python - 我如何在此代码中使用 map() ?