如何从 numpy.linalg.lstsq 中获取 p 值?

How to get p-value from numpy.linalg.lstsq?

我想计算从 numpy.linalg.lstsq 得到的拟合的 p 值。这是一个玩具示例:

import numpy as np
x = np.array([[ 58295.62187335],[ 45420.95483714],[  3398.64920064],[   977.22166306],[  5515.32801851],[ 14184.57621022],[ 16027.2803392 ],[ 15313.01865824],[  6443.2448182 ]])
y = np.array([ 143547.79123381,   22996.69597427,    2591.56411049, 661.93115277,    8826.96549102,   17735.13549851, 11629.13003263,   14438.33177173,    6997.89334741])

a, res, rank, s = np.linalg.lstsq(x, y)

从上一个问题 (get the R^2 value from scipy.linalg.lstsq) 我知道必须得到 R²,但是我还想计算 p 值。

提前致谢。

你可以使用 scipy.stats

import numpy as np
from scipy.stats import pearsonr
x = np.array([ 58295.62187335,  45420.95483714,   3398.64920064,    977.22166306, 5515.32801851,  14184.57621022,  16027.2803392 ,  15313.01865824, 6443.2448182 ])
y = np.array([ 143547.79123381,   22996.69597427,    2591.56411049, 661.93115277,    8826.96549102,   17735.13549851, 11629.13003263,   14438.33177173,    6997.89334741])

r, p = pearsonr(x,y)