Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (e.g. vectors). 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?

like image 805
Mila Avatar asked Oct 16 '22 14:10

Mila


1 Answers

Just flatten your x1, x2 and y lists and you are good to go. 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:

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:

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)
like image 186
Sheldore Avatar answered Oct 21 '22 07:10

Sheldore