Python函数来计算Beta矩阵

2024-05-16 21:50:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一个有效的函数,在python中为每个可能的多元回归模型自动生成beta,该模型给定一个因变量和一组预测值作为数据帧

例如,给定这组数据:

enter image description here

https://i.stack.imgur.com/YuPuv.jpg
因变量为“人均病例”,以下各列为预测变量

在一个简单的示例中:


  Student   Grade    Hours Slept   Hours Studied   ...  
 --------- -------- ------------- --------------- ----- 
  A             90             9               1   ...  
  B             85             7               2   ...  
  C            100             4               5   ...  
  ...          ...           ...             ...   ...  

其中,beta矩阵输出看起来是这样的:


  Regression   Hours Slept   Hours Studied  
 ------------ ------------- --------------- 
           1   #             N/A            
           2   N/A           #              
           3   #             #              

表格大小为[2^n - 1],其中n是变量的数量,因此在有5个预测因子和1个依赖因子的情况下,将有31个回归,每个回归具有不同的beta计算组合

更详细地描述了该过程here,并发布了用R编写的实际解决方案here


Tags: 数据函数https模型comherestack因子
1条回答
网友
1楼 · 发布于 2024-05-16 21:50:23

我不知道有哪个软件包已经这样做了。但您可以创建所有这些组合(2^n-1),其中n是X中的列数(自变量),并为每个组合拟合线性回归模型,然后获得每个模型的系数/β

以下是我将如何做到这一点,希望这能有所帮助

from sklearn import datasets, linear_model
import numpy as np
from itertools import combinations

#test dataset
X, y = datasets.load_boston(return_X_y=True)

X = X[:,:3] # Orginal X has 13 columns, only taking n=3 instead of 13 columns

#create all 2^n-1 (here 7 because n=3) combinations of columns, where n is the number of features/indepdent variables

all_combs = [] 
for i in range(X.shape[1]):
    all_combs.extend(combinations(range(X.shape[1]),i+1))

# print 2^n-1 combinations
print('2^n-1 combinations are:')
print(all_combs) 

 ## Create a betas/coefficients as zero matrix with rows (2^n-1) and columns equal to X
betas = np.zeros([len(all_combs), X.shape[1]])+np.NaN

## Fit a model for each combination of columns and add the coefficients into betas matrix
lr = linear_model.LinearRegression()
for regression_no, comb in enumerate(all_combs):
    lr.fit(X[:,comb], y)
    betas[regression_no, comb] = lr.coef_

## Print Coefficients of each model
print('Regression No'.center(15)+" ".join(['column {}'.format(i).center(10) for i in range(X.shape[1])]))  
print('_'*50)
for index, beta in enumerate(betas):
    print('{}'.format(index + 1).center(15), " ".join(['{:.4f}'.format(beta[i]).center(10) for i in range(X.shape[1])]))

导致

2^n-1 combinations are:
[(0,), (1,), (2,), (0, 1), (0, 2), (1, 2), (0, 1, 2)]


    Regression No  column 0   column 1   column 2 
__________________________________________________
       1         -0.4152      nan        nan    
       2           nan       0.1421      nan    
       3           nan        nan      -0.6485  
       4         -0.3521     0.1161      nan    
       5         -0.2455      nan      -0.5234  
       6           nan       0.0564    -0.5462  
       7         -0.2486     0.0585    -0.4156  

相关问题 更多 >