Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to pass 2D array to linear regression:

x = [[1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 3, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1],
     [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], 
     [0, 0, 0, 0, 0, 1, 1, 0, 0, 3]]
y = [3.9857,  3.6877, 3.6877]

x = numpy.array(x)
y = numpy.array(y)

model = LinearRegression(z,y).fit(z,y)

I am not using reshape (-1,1) as it makes the 2D array to 3D

But I am getting error:

ValueError: setting an array element with a sequence
TypeError: float() argument must be a string or a number, not 'list'

How can I correctly pass a two dimensional array to linear regression?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
276 views
Welcome To Ask or Share your Answers For Others

1 Answer

A bit long to type as a comment, so if you look at your x, before converting to a numpy array:

print([len(i) for i in x])
[36, 10, 10]

And y has length 3. It is ok to do a linear regression, but your independent variable needs to have the same number of variable, per observation.

In your case, the first element of list x should have 10 entries, like the others.

So for example:

import numpy as np
from sklearn import linear_model
clf = linear_model.LinearRegression()

# take the first ten
exo = np.array([i[:10] for i in x])

# fit
clf.fit(exo,np.array(y))
clf.coef_

array([ 8.12727273e-02, -6.93889390e-18,  8.12727273e-02,  0.00000000e+00,
        0.00000000e+00, -2.70909091e-02,  5.41818182e-02,  8.12727273e-02,
        2.70909091e-02,  0.00000000e+00])

You get 10 coefficients, one for each column of x.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...