嗨,我有2个数字列表,我想从常规线性回归得到R ^ 2。我认为问题已经发布了很多,但我在某个地方找不到。
我的清单:
my_y = [2,5,6,10]
my_x = [19,23,22,30]
我试图将它改为numpy数组,然后使用sklearn回归并得到我需要的东西,但我没有成功。我使用了以下代码:
from sklearn.linear_model import LinearRegression
import numpy as np
my_y = np.array([2,5,6,10]).reshape(1, -1)
my_x = np.array([19,23,22,30]).reshape(1,-1)
lm = LinearRegression()
result = lm.score(my_x, my_y)
print(result)
有没有人有快速的方法让R ^ 2在这两个变量之间进行线性回归?
此回归的预期输出为:R ^ 2 = 0.930241
尝试:
import scipy
slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(my_x, my_y)
print(r_value**2)
你得到:
0.9302407516147975
从文档的快速浏览中,我看到linear_model
需要您提供一个线性模型,顾名思义。得到一个简单的R:
import scipy
my_y = np.array([2,5,6,10])
my_x = np.array([19,23,22,30])
R=scipy.stats.linregress(my_x, my_y)[2]
print(R)
0.9644898919194527
和R**2
产生0.930的理想结果。