简体   繁体   中英

Multivariate linear regression in python

I have a data set as follows arranged in X and Y matrices as follows: 在此处输入图片说明

I want to find a 2*2 matrix A such that y_i=A x_i for all i=1,...,n . So I am using the following code for linear regression in python:

import numpy as np
import matplotlib.pyplot as plt
import sklearn
from sklearn import datasets, linear_model

#n=5
X=np.random.uniform(0,1,(2,5))
A=np.random.uniform(0,1,(2,2))
y=np.dot(A,X)
print(y)

# Create linear regression object
regr = linear_model.LinearRegression()
# Train the model using the training sets
model=regr.fit(X, y)
#model.predict(X)
model.coef_

However my model.coef_ command is printing a 5*5 matrix instead of a 2*2 matrix that I want for A . How do I achieve this?

Fit the model on a transpose of the samples and outcome - the second dimensions will be used to create the model - to get a 2x2 array:

model=regr.fit(X.T, y.T)
# test
np.testing.assert_allclose(y.T, model.predict(X.T))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM