Home > front end >  How I can get the same result in Python what I get by using SLOPE in Excel?
How I can get the same result in Python what I get by using SLOPE in Excel?

Time:04-24

a= [-0.10266667,0.02666667,0.016 ,0.06666667,0.08266667]
b= [5.12,26.81,58.82,100.04,148.08]

the result in excel SLOPE(a,b) is 0.001062 How I can get the same result in Python what I get by using SLOPE in Excel?

CodePudding user response:

Here you go.

import numpy as np
from sklearn.linear_model import LinearRegression

x = np.array([5.12,26.81,58.82,100.04,148.08]).reshape((-1, 1))
y = np.array([-0.10266667,0.02666667,0.016 ,0.06666667,0.08266667])

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

print(model.coef_)

# methods and attributes available
print(dir(model))

In excel, SLOPE arguments are in the order y, x. I used those names here so it would be more obvious.

The reshape just makes x a lists of lists which is what is required. y is just needs to be a list. model has many other methods and attributes available. See dir(model).

  • Related