简体   繁体   English

使用Sklearn进行多元多元线性回归

[英]Multivariate multiple linear regression using Sklearn

I want to train a linear model Y = M_1*X_1 + M_2*X_2 using sklearn with multidimensional input and output samples (eg vectors). 我想使用具有多维输入和输出样本(例如矢量)的sklearn来训练线性模型Y = M_1*X_1 + M_2*X_2 I tried the following code: 我尝试了以下代码:

from sklearn import linear_model
from pandas import DataFrame 

x1 = [[1,2],[2,3],[3,4]]
x2 = [[1,1],[3,2],[3,5]]
y = [[1,0],[1,2],[2,3]]
model = {
    'vec1': x1,
    'vec2': x2,
    'compound_vec': y}

df = DataFrame(model, columns=['vec1','vec2','compound_vec'])
x = df[['vec1','vec2']].astype(object)
y = df['compound_vec'].astype(object)
regr = linear_model.LinearRegression()
regr.fit(x,y)

But I get the following error: 但是我收到以下错误:

regr.fit(x,y)
 ...
array = array.astype(np.float64)
ValueError: setting an array element with a sequence.

Does anyone know what is wrong with the code? 有谁知道代码有什么问题? and if this is a right way to train Y = M_1*X_1 + M_2*X_2 ? 如果这是训练Y = M_1*X_1 + M_2*X_2的正确方法?

Just flatten your x1 , x2 and y lists and you are good to go. 只需展平你的x1x2y列表就可以了。 One way to do that is using arrays as follows: 一种方法是使用如下数组:

import numpy as np
x1 =np.array(x1).flatten()
x2 =np.array(x2).flatten()
y =np.array(y).flatten()

Second way to do it is using ravel as: 第二种方法是使用ravel作为:

x1 =np.array(x1).ravel()
x2 =np.array(x2).ravel()
y =np.array(y).ravel()

Third way without using NumPy is by using list comprehension as: 不使用NumPy的第三种方法是使用列表理解为:

x1 =[j for i in x1 for j in i]
x2 =[j for i in x2 for j in i]
y =[j for i in y for j in i]

There might be more ways but you got what the problem was. 可能有更多的方法,但你得到了问题所在。 For more ways, you can have a look here 有关更多方法,您可以在这里查看

Output 产量

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

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

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