简体   繁体   English

LinearRegression Predict- ValueError:矩阵未对齐

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

I've been searching google and can't figure out what I'm doing wrong. 我一直在搜索谷歌,无法弄清楚我做错了什么。 I'm pretty new to python and trying to use scikit on stocks but I'm getting the error "ValueError: matrices are not aligned" when trying to predict. 我对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()

The error line is 错误行是

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

I've tried nesting it in a list. 我已经尝试将其嵌套在列表中。 Making it an array with numpy. 使它成为一个numpy数组。 Anything I could find on google but I have no idea what I'm doing here. 我在谷歌上找到的任何东西,但我不知道我在这里做什么。

What does this error mean and why is it happening? 这个错误意味着什么,为什么会发生?

Trying to predict stock price by simple linear regression? 试图通过简单的线性回归预测股票价格? :^|. :^ |。 Anyway, this is what you need to change: 无论如何,这是你需要改变的:

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

Remember, when you build a model, X and y for .fit method should have the shape of [n_samples,n_features] . 请记住,在构建模型时, .fit方法的Xy应该具有[n_samples,n_features]的形状。 The same applies to the .predict method. 这同样适用于.predict方法。

暂无
暂无

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

相关问题 PolynomialFeatures LinearRegression ValueError:形状未对齐 - PolynomialFeatures LinearRegression ValueError: shapes not aligned ValueError:矩阵未对齐 - ValueError: matrices are not aligned ValueError:矩阵未对齐,且矩阵形状正确 - ValueError: matrices are not aligned, with correct shape of matrices Python sklearn.linear_model:LinearRegression()当.predict()时出现ValueError - Python sklearn.linear_model: LinearRegression() ValueError occured when .predict() ValueError:矩阵未针对复制错误和x [:]对齐 - ValueError: matrices are not aligned for copy error and x[:] sklearn LinearRegression.Predict()问题 - sklearn LinearRegression.Predict() issue 线性回归器无法预测一组值; 错误:ValueError:形状(100,1)和(2,1)未对齐:1(dim 1)!= 2(dim 0) - Linear Regressor unable to predict a set of values; Error: ValueError: shapes (100,1) and (2,1) not aligned: 1 (dim 1) != 2 (dim 0) 将 scikit LinearRegression.predict 用于多特征标签 - Using scikit LinearRegression.predict for multifeature labels 传递exog值时,ARMAX模型预测会导致“ ValueError:矩阵未对齐” - ARMAX model forecasting leads to “ValueError: matrices are not aligned” when passing exog values ValueError:形状(1,1000)和(1,1000)不对齐:1000(dim 1)!= 1(dim 0)当numpy.dot()具有两个矩阵时 - ValueError: shapes (1,1000) and (1,1000) not aligned: 1000 (dim 1) != 1 (dim 0) When numpy.dot() with two matrices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM