我对sklearn的线性回归模型有疑问。
我想使用多个功能来拟合线性回归。现在我的X是一个np.matrix而我的Y是一个np.array。
我的X看起来像这样(打印)X.shape - > (21,3):
[[ 8.68269590e-03 -2.83226292e-03 1.91826382e-01]
[ 5.85903392e-03 -5.68809929e-03 2.21862758e-01]
[ 2.90920454e-03 -1.24549359e-03 1.71619892e-01]
[ 7.71491867e-04 1.74288704e-03 2.70315213e-03]
[-2.44583484e-03 -4.73496469e-05 -1.25966777e-01]
[-4.16023564e-03 -2.09644321e-03 -4.91722645e-02]
[-3.22298365e-03 -3.55366669e-03 -1.67993225e-02]
[-2.79712919e-03 -1.94070947e-03 -1.70873725e-01]
[-2.76366703e-03 -4.98257755e-04 -2.52336769e-01]
[-3.65153430e-03 -3.89128554e-03 -2.03762730e-01]
[-6.07841812e-03 -8.89479214e-03 -1.54953118e-01]
[-7.55809682e-03 -1.13395249e-02 -2.29260955e-01]
[-7.46617379e-03 -5.70467322e-03 -2.01416145e-01]
[-7.82348527e-03 3.58732358e-04 -1.47799157e-01]
[-8.68110057e-03 -3.98060036e-05 -1.17156978e-01]
[-9.13439934e-03 3.21795372e-03 -4.17922611e-02]
[-6.64659597e-03 5.79326182e-03 -7.08715900e-02]
[-3.28840696e-03 2.57177260e-03 -1.34971930e-01]
[-1.38119572e-04 2.25318751e-03 -6.03902835e-02]
[ 4.53278359e-03 2.40625868e-03 1.38175436e-01]
[ 5.95225669e-03 1.00742943e-03 1.75614285e-01]]
我的Y看起来像这样(打印)Y.shape - > (21,1):
[[ 0.00472189]
[ 0.00134158]
[-0.01183452]
[-0.00712723]
[ 0.01007362]
[ 0.00373918]
[-0.00832614]
[-0.02623798]
[-0.00381873]
[ 0.0068726 ]
[-0.01438412]
[ 0.00898785]
[ 0.0100893 ]
[-0.00321919]
[ 0.00827624]
[ 0.00486361]
[-0.01065365]
[ 0.00741757]
[ 0.01037663]
[ 0.00230243]
[-0.00169308]]
但是,sklearn会抛出以下异常:
ValueError: shapes (1,1) and (3,1) not aligned: 1 (dim 1) != 3 (dim 0)
我用Google搜索并尝试了多项内容,但没有成功。我来自不同的编程背景,所以python对我来说是一种新的东西。
你们能否指出我正确的方向来解决这个错误?
编辑:我的代码:
training_chart = self.prices[-self.TRAINING_DATA_SIZE:]
x = []
y = []
n = 12
for i, price in enumerate(training_chart[n:]):
history = training_chart[i:i+n]
price_change = (price - history[-1]) / history[-1]
sma_12 = indicators.SMA(history, 12)
sma_8 = indicators.SMA(history, 8)
sma_6 = indicators.SMA(history, 6)
sma_4 = indicators.SMA(history, 4)
rsi = indicators.RSI(history, 12)[-1]
long_term_ma_index = (sma_8 - sma_12) / sma_12
short_term_ma_index = (sma_4 - sma_6) / sma_6
y.append(price_change)
x.append(long_term_ma_index)
x.append(short_term_ma_index)
x.append(rsi / 100 - 0.5)
x = np.matrix(x).reshape(-1, 3)
y = np.array(y).reshape(-1, 1)
print x
print x.shape
print y
print y.shape
model = LinearRegression()
model.fit(x, y)
编辑2:完全追溯:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/Users/jessekramer/Code/trading-bot/pytrader/main.py", line 145, in <module>
main(sys.argv[1:])
File "/Users/jessekramer/Code/trading-bot/pytrader/main.py", line 53, in main
bot.run()
File "pytrader/runners/backtest.py", line 16, in run
self.strategy.tick(candlestick)
File "pytrader/bots/strategies/linear_regression.py", line 24, in tick
next_price = model.predict(self.current_price)
File "/Users/jessekramer/VirtualEnv/trading/lib/python2.7/site-packages/sklearn/linear_model/base.py", line 256, in predict
return self._decision_function(X)
File "/Users/jessekramer/VirtualEnv/trading/lib/python2.7/site-packages/sklearn/linear_model/base.py", line 241, in _decision_function
dense_output=True) + self.intercept_
File "/Users/jessekramer/VirtualEnv/trading/lib/python2.7/site-packages/sklearn/utils/extmath.py", line 140, in safe_sparse_dot
return np.dot(a, b)
ValueError: shapes (1,1) and (3,1) not aligned: 1 (dim 1) != 3 (dim 0)
答案 0 :(得分:0)
查看回溯,当您调用predict
方法时,似乎会出现问题:
next_price = model.predict(self.current_price)
您需要确保self.current_price
具有正确的形状--- shape=(whatever, 3)