Py学习  »  Python

如何在Python中获得与在Excel中使用SLOPE相同的结果?

Jonas-2020 • 2 年前 • 392 次点击  
a= [-0.10266667,0.02666667,0.016 ,0.06666667,0.08266667]
b= [5.12,26.81,58.82,100.04,148.08]

excel斜率(a,b)中的结果为0.001062 如何在Python中获得与在Excel中使用SLOPE相同的结果?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132771
 
392 次点击  
文章 [ 1 ]  |  最新文章 2 年前
Steve
Reply   •   1 楼
Steve    2 年前

干得好。

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))

在excel中, SLOPE 参数的顺序是y,x。我在这里使用了这些名称,所以它会更明显。

这个 reshape 只是制造 x a所需列表的列表。 y 只是需要一张清单。 model 有许多其他可用的方法和属性。看见 dir(model) .