简体   繁体   English

python中2个列表的线性回归

[英]Linear regression of 2 lists in python

Hi I have 2 lists of numbers and I want to get the R^2 from a regular linear regression.您好,我有 2 个数字列表,我想从常规线性回归中获取 R^2。 I think the question has been posted a lot, but I just cannot find this somewhere.我认为这个问题已经发布了很多,但我无法在某处找到它。

My lists:我的清单:

my_y = [2,5,6,10]
my_x = [19,23,22,30]

I have tried to change it to numpy arrays and then use sklearn to regress and get what I need, but I did not succeed.我曾尝试将其更改为 numpy 数组,然后使用 sklearn 回归并获得我需要的东西,但我没有成功。 I used the following code:我使用了以下代码:

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)

Does anyone have a fast way to get the R^2 from doing a linear regression between those 2 variables?有没有人有快速的方法通过在这两个变量之间进行线性回归来获得 R^2?

My expected output from this regression is: R^2=0.930241此回归的预期输出是:R^2=0.930241

Try:尝试:

import scipy

my_y = [2,5,6,10]
my_x = [19,23,22,30]

slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(my_x, my_y)
    
print(r_value**2)

and you get:你得到:

0.9302407516147975

From scipy version '1.4.1' (thanks to @FlamePrinz for having noted the issue for new versions of scipy):来自 scipy 版本'1.4.1' (感谢@FlamePrinz 注意到新版本 scipy 的问题):

from scipy import stats

my_y = [2,5,6,10]
my_x = [19,23,22,30]

slope, intercept, r_value, p_value, std_err = stats.linregress(my_x, my_y)

print(r_value**2)

From a quick look of the documentation, I see that linear_model needs you to provide a linear model as the name suggests.通过快速浏览文档,我发现linear_model需要您提供顾名思义的线性模型。 To get a simple R:要获得一个简单的 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 0.9644898919194527

and R**2 yields the desired result of 0.930. R**2产生所需的结果 0.930。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM