【问题标题】:LinearRegression Predict- ValueError: matrices are not alignedLinearRegression Predict- ValueError:矩阵未对齐
【发布时间】:2014-03-22 08:32:50
【问题描述】:

我一直在搜索谷歌,但无法弄清楚我做错了什么。我对 python 很陌生,并试图在股票上使用 scikit,但在尝试预测时出现错误“ValueError:矩阵未对齐”。

import datetime

import numpy as np
import pylab as pl
from matplotlib import finance
from matplotlib.collections import LineCollection

from sklearn import cluster, covariance, manifold, linear_model

from sklearn import datasets, linear_model

###############################################################################
# Retrieve the data from Internet

# Choose a time period reasonnably calm (not too long ago so that we get
# high-tech firms, and before the 2008 crash)
d1 = datetime.datetime(2003, 01, 01)
d2 = datetime.datetime(2008, 01, 01)

# kraft symbol has now changed from KFT to MDLZ in yahoo
symbol_dict = {
    'AMZN': 'Amazon'}

symbols, names = np.array(symbol_dict.items()).T

quotes = [finance.quotes_historical_yahoo(symbol, d1, d2, asobject=True)
          for symbol in symbols]

open = np.array([q.open for q in quotes]).astype(np.float)
close = np.array([q.close for q in quotes]).astype(np.float)

# The daily variations of the quotes are what carry most information
variation = close - open

#########

pl.plot(range(0, len(close[0])-20), close[0][:-20], color='black')

model = linear_model.LinearRegression(normalize=True)
model.fit([close[0][:-1]], [close[0][1:]])

print(close[0][-20:])
model.predict(close[0][-20:])


#pl.plot(range(0, 20), model.predict(close[0][-20:]), color='red')

pl.show()

错误行是

model.predict(close[0][-20:])

我尝试将它嵌套在一个列表中。用 numpy 使其成为一个数组。我可以在谷歌上找到任何东西,但我不知道我在这里做什么。

这个错误是什么意思,为什么会发生?

【问题讨论】:

  • 您也可以使用类似的方式向数据中添加一个常量特征参数:X.add_constant(len(X))

标签: python numpy linear-regression predict


【解决方案1】:

试图通过简单的线性回归来预测股票价格? :^|。无论如何,这是你需要改变的:

In [19]:

M=model.fit(close[0][:-1].reshape(-1,1), close[0][1:].reshape(-1,1))
In [31]:

M.predict(close[0][-20:].reshape(-1,1))
Out[31]:
array([[ 90.92224274],
       [ 94.41875811],
       [ 93.19997275],
       [ 94.21895723],
       [ 94.31885767],
       [ 93.030142  ],
       [ 90.76240203],
       [ 91.29187436],
       [ 92.41075928],
       [ 89.0940647 ],
       [ 85.10803717],
       [ 86.90624508],
       [ 89.39376602],
       [ 90.59257129],
       [ 91.27189427],
       [ 91.02214318],
       [ 92.86031126],
       [ 94.25891741],
       [ 94.45871828],
       [ 92.65052033]])

请记住,当您构建模型时,Xy.fit 方法应该具有 [n_samples,n_features] 的形状。 .predict 方法也是如此。

【讨论】:

  • 我不确定该使用什么。你会推荐什么?我什至不知道 LinearRegression 意味着什么我只是想用我自己的数据来遵循这些示例。
猜你喜欢
  • 2012-11-01
  • 2018-03-06
  • 1970-01-01
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多