2014-02-21 3 views
0

저는 Google을 검색해 왔으며 잘못하고있는 것을 파악할 수 없습니다. 저는 파이썬에 매우 익숙하며 주식에 scikit을 사용하려하지만 예측하려고 할 때 "ValueError : 행렬이 정렬되지 않았습니다."라는 오류가 나타납니다.LinearRegression Predict- 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와 배열 만들기. 내가 Google에서 찾을 수있는 모든 것은 있지만 여기에 내가 무엇을하고 있는지 전혀 알지 못합니다.

이 오류는 무엇을 의미하며 왜 그런 일이 발생합니까?

+0

X.add_constant (len (X)) –

답변

2

간단한 선형 회귀 분석을 통해 주가를 예측하려고합니까? :^|.

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

는 기억, 당신은 [n_samples,n_features]의 모양을 가져야한다 .fit 방법에 대한 모델, Xy를 빌드 할 때 : 어쨌든, 이것은 당신이 변경해야 할 것입니다. .predict 방법에도 동일하게 적용됩니다.

+0

다음과 같은 것을 사용하여 데이터에 상수 기능 매개 변수를 추가하기 만하면됩니다. 사용법을 잘 모르겠습니다. 무엇을 추천하나요? LinearRegression이 내 데이터로 예제를 따라하기 만하면된다는 것을 알지 못합니다. –