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

时间:2016-03-26 00:47:20

标签: python numpy p-value

我想计算从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值。

提前致谢。

1 个答案:

答案 0 :(得分:2)

你可以使用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)