2016-05-03 13 views
0

タスク:3つの入力ベクトルと4つの出力ベクトルによる線形回帰?

例として、我々は3つの入力ベクトルいる:使用する方法

foofoo = [1, 1, 2, 2, 3, 3] 
barbar = [4, 4, 5, 5, 6, 6] 
spamspam = [7, 7, 8, 8, 9, 9] 
hamham = [10, 10, 11, 11, 12, 12] 

:また、我々は入力ベクトルから線形依存性を持つ4つの出力ベクトルを

foo = [1, 2, 3, 4, 5, 6] 
bar = [50, 60, 70, 80, 90, 100] 
spam = [-10, -20, -30, -40, -50, -60] 

を持っていますこのデータのPythonでの線形回帰?

+0

3入力ベクトルと4つの出力ベクトルは、いかなる意味がありません。 –

+1

これをチェックしてください:http://connor-johnson.com/2014/02/18/linear-regression-with-python/ – vcp

答えて

1

あなたは行わhereとしてOLS (Ordinary Least Squares model)を使用することができます。

#imports 
import numpy as np 
import statsmodels.api as sm 

#generate the input matrix 
X=[foo,bar,spam] 
#turn it into a numpy array 
X = np.array(X).T 
#add a constant column 
X=sm.add_constant(X) 

これは、入力行列X与える:

array([[ 1., 1., 50., -10.], 
     [ 1., 2., 60., -20.], 
     [ 1., 3., 70., -30.], 
     [ 1., 4., 80., -40.], 
     [ 1., 5., 90., -50.], 
     [ 1., 6., 100., -60.]]) 

そして今、あなたはそれぞれの所望の出力ベクトルをフィットすることができます

resFoo = sm.OLS(endog=foofoo, exog=X).fit() 
resBar = sm.OLS(endog=barbar, exog=X).fit() 
resSpam = sm.OLS(endog=spamspam, exog=X).fit() 
resham = sm.OLS(endog=hamham, exog=X).fit() 

resultは、あなたにcoef (定数のため、3列のfoo、バー、およびスパム)ficients:

>>> resFoo.params 
array([-0.00063323, 0.0035345 , 0.01001583, -0.035345 ]) 

は、あなたが今入力して、それを確認することができます。foofooの所望の出力に近い

>>> np.matrix(X)*np.matrix(resFoo.params).T 
matrix([[ 0.85714286], 
     [ 1.31428571], 
     [ 1.77142857], 
     [ 2.22857143], 
     [ 2.68571429], 
     [ 3.14285714]]) 


回帰を行うためのさまざまな方法のため、この質問を参照してください:Multiple linear regression in Python

関連する問題