簡體   English   中英

LinearRegression Predict- ValueError:矩陣未對齊

[英]LinearRegression Predict- ValueError: matrices are not aligned

我一直在搜索谷歌,無法弄清楚我做錯了什么。 我對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數組。 我在谷歌上找到的任何東西,但我不知道我在這里做什么。

這個錯誤意味着什么,為什么會發生?

試圖通過簡單的線性回歸預測股票價格? :^ |。 無論如何,這是你需要改變的:

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]])

請記住,在構建模型時, .fit方法的Xy應該具有[n_samples,n_features]的形狀。 這同樣適用於.predict方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM