user2593973
user2593973

Reputation:

sklearn LinearRegression reports error

Why doesn't this work in python?

    x = []
    y = []
    for ii in range(0,100):
        x.append(ii)
        y.append(ii)
    clf = LinearRegression()
    clf.fit(x, y)
    clf.predict(101)

I get the error "tuple index out of range"

Upvotes: 1

Views: 237

Answers (1)

elyase
elyase

Reputation: 40993

Make a list for each row so that in the end you have a 2D structure [[0], [1], [2], ...]:

x = []
y = []
for ii in range(0,100):
    x.append([ii])    <-----
    y.append(ii)
clf = LinearRegression()
clf.fit(x, y)
clf.predict(101)

Output:

array([ 101.])

Upvotes: 3

Related Questions