Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outputting coefficients when running linear regression using sklearn

I'm attempting to run a simple linear regression on a data set and retrieve the coefficients. The data which is from a a .csv file looks like:

"","time","LakeHuron"
"1",1875,580.38
"2",1876,581.86
"3",1877,580.97
"4",1878,580.8
...

import pandas as pd
import numpy as np
from sklearn import datasets, linear_model

def Main():
    location = r"~/Documents/Time Series/LakeHuron.csv"
    ts = pd.read_csv(location, sep=",", parse_dates=[0], header=None)

    ts.drop(ts.columns[[0]], axis=1, inplace=True)

    length = len(ts)
    x = ts[1].values
    y = ts[2].values

    x = x.reshape(length, 1)
    y = y.reshape(length, 1)

    regr = linear_model.LinearRegression()
    regr.fit(x, y)
    print(regr.coef_)

if __name__ == "__main__":
    Main()

Since this is a simple linear model then $Y_t = a_0 + a_1*t$, which in this case should be $Y_t = 580.202 -0.0242t$. and all that prints out when running the above code is [[-0.02420111]]. Is there anyway to get the second coefficient 580.202?

I've had a look at the documentation on http://scikit-learn.org/stable/modules/linear_model.html and it outputs two variables in the array.

like image 644
Lukasz Avatar asked Feb 24 '26 20:02

Lukasz


1 Answers

Look like you only have one X and one Y, So output is correct. Try this:

#coef_ : array, shape (n_features, ) or (n_targets, n_features)

    print(regr.coef_)

#intercept_ : array  Independent term in the linear model.

    print(regr.intercept_)

http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html#sklearn.linear_model.LinearRegression

like image 76
Merlin Avatar answered Feb 26 '26 08:02

Merlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!