简体   繁体   English

如何解决 np.reshape 异常:数据必须是一维的

[英]how to solve np.reshape Exception: Data must be 1-dimensional

Im trying to reshape data for a polynomial regression.我正在尝试为多项式回归重塑数据。 Here is my code.这是我的代码。

data = pd.read_csv('GBPJPY.csv')

data.columns = np.array(['Date', 'open', 'high', 'low', 'close', 'vol'])

data.Date = pd.to_datetime(data.Date)

data = data.set_index(data.Date)

data = data[['open', 'high', 'low', 'close', 'vol']]

data = data.drop_duplicates(keep=False)

price = data.close.copy()

price = np.reshape(price,(-1,1))

# Polynomial Regression

polyReg = PolynomialFeatures(degree=3)
price_poly = polyReg.fit_transform(price)

I get an error at我在

price = np.reshape(price,(-1,1))

here is my stacktrace这是我的堆栈跟踪

(main) C:\Users\Owner\Desktop\TradingAlgorithm>python main.py
Traceback (most recent call last):
  File "main.py", line 28, in <module>
    price = np.reshape(price,(-1,1))
  File "<__array_function__ internals>", line 6, in reshape
  File "C:\Users\Owner\Anaconda3\envs\main\lib\site-packages\numpy\core\fromnumeric.py", line 301, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)
  File "C:\Users\Owner\Anaconda3\envs\main\lib\site-packages\numpy\core\fromnumeric.py", line 58, in _wrapfunc
    return _wrapit(obj, method, *args, **kwds)
  File "C:\Users\Owner\Anaconda3\envs\main\lib\site-packages\numpy\core\fromnumeric.py", line 51, in _wrapit
    result = wrap(result)
  File "C:\Users\Owner\Anaconda3\envs\main\lib\site-packages\pandas\core\generic.py", line 1918, in __array_wrap__
    return self._constructor(result, **d).__finalize__(self)
  File "C:\Users\Owner\Anaconda3\envs\main\lib\site-packages\pandas\core\series.py", line 305, in __init__
    data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
  File "C:\Users\Owner\Anaconda3\envs\main\lib\site-packages\pandas\core\construction.py", line 482, in sanitize_array
    raise Exception("Data must be 1-dimensional")
Exception: Data must be 1-dimensional

Here is some code from my csv file这是我的 csv 文件中的一些代码

Date,Open,High,Low,Close,Volume
08.03.2018 00:00:00.000 GMT-0500,147.464,147.466,147.405,147.418,169.75
08.03.2018 00:01:00.000 GMT-0500,147.418,147.430,147.404,147.412,234.21
08.03.2018 00:02:00.000 GMT-0500,147.412,147.419,147.398,147.411,179.43
08.03.2018 00:03:00.000 GMT-0500,147.411,147.424,147.403,147.410,149.62
08.03.2018 00:04:00.000 GMT-0500,147.410,147.433,147.407,147.411,188.22
08.03.2018 00:05:00.000 GMT-0500,147.412,147.412,147.392,147.407,209.49
08.03.2018 00:06:00.000 GMT-0500,147.407,147.413,147.397,147.410,216.6
08.03.2018 00:07:00.000 GMT-0500,147.410,147.425,147.404,147.425,236.67
08.03.2018 00:08:00.000 GMT-0500,147.425,147.433,147.423,147.427,204.06
08.03.2018 00:09:00.000 GMT-0500,147.427,147.432,147.400,147.421,184.34
08.03.2018 00:10:00.000 GMT-0500,147.421,147.422,147.411,147.413,164.26
08.03.2018 00:11:00.000 GMT-0500,147.413,147.414,147.397,147.413,107.21
08.03.2018 00:12:00.000 GMT-0500,147.413,147.442,147.412,147.440,159.66
08.03.2018 00:13:00.000 GMT-0500,147.441,147.445,147.423,147.425,154.93
08.03.2018 00:14:00.000 GMT-0500,147.424,147.433,147.424,147.433,120.57
08.03.2018 00:15:00.000 GMT-0500,147.434,147.448,147.434,147.435,188.68
08.03.2018 00:16:00.000 GMT-0500,147.434,147.454,147.434,147.453,229.66
08.03.2018 00:17:00.000 GMT-0500,147.454,147.460,147.435,147.446,260.38
08.03.2018 00:18:00.000 GMT-0500,147.446,147.449,147.441,147.442,158.1
08.03.2018 00:19:00.000 GMT-0500,147.441,147.468,147.437,147.467,139.76
08.03.2018 00:20:00.000 GMT-0500,147.467,147.467,147.454,147.454,188.6
08.03.2018 00:21:00.000 GMT-0500,147.455,147.455,147.436,147.436,66.31
08.03.2018 00:22:00.000 GMT-0500,147.436,147.436,147.429,147.430,68
08.03.2018 00:23:00.000 GMT-0500,147.431,147.431,147.387,147.401,193.7
08.03.2018 00:24:00.000 GMT-0500,147.400,147.402,147.384,147.389,179.24
08.03.2018 00:25:00.000 GMT-0500,147.390,147.390,147.366,147.370,186.1
08.03.2018 00:26:00.000 GMT-0500,147.371,147.390,147.366,147.378,170.69
08.03.2018 00:27:00.000 GMT-0500,147.376,147.393,147.371,147.393,97.02
08.03.2018 00:28:00.000 GMT-0500,147.391,147.391,147.321,147.340,221.15
08.03.2018 00:29:00.000 GMT-0500,147.341,147.341,147.298,147.321,233.49
08.03.2018 00:30:00.000 GMT-0500,147.318,147.335,147.304,147.326,163.06
08.03.2018 00:31:00.000 GMT-0500,147.326,147.329,147.289,147.295,176.94
08.03.2018 00:32:00.000 GMT-0500,147.295,147.316,147.279,147.311,208.58

Something along the lines should do:应该做的事情:

price = data.close.copy()
price = price.values.reshape(-1,1)

or if you wish:或者如果您愿意:

price = data.close.copy()
price = np.reshape(price.values, (-1,1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM