首页 > 解决方案 > Pymc3 问题总结

问题描述

我目前正在努力获取我通过贝叶斯回归运行的模型的统计数据摘要。我首先使用 Lasso 和模型选择来过滤最佳变量,然后用于pm.Model获得适当的回归。

当然,在“过滤”了不相关的解释变量后,X 矩阵的形状发生了变化。我处理的数据是来自 sklearn.dataset 的 load_boston 数据集。我将数据编码为自变量,将目标编码为因变量。

使用进行模型选择后SelectFromModel,我使用该get.support方法获取保留变量的索引。然后,我对所有变量的索引和支持中包含的数字使用了一个循环,目的是将保留变量的名称存储在我在 hoc 创建的空列表中。代码看起来像这样

import pandas as pd
import numpy as np
import pymc3 as pm
import matplotlib.pyplot as plt

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(9)

# Load the boston dataset.
from sklearn.datasets import load_boston
boston = load_boston()
X, y = boston['data'], boston['target']

# Here is the code for the estimator LassoCV 

# Here is the code for Model Selection
support(indices=True) #to obtain the list of indices of retained variables
X_transform = sfm.transform(X) #to remove the unnecessary variables

#Here is the line for linear modeling

#I initialize some useful variables

m = y.shape[0]
n = X.shape[1]
c = supp.shape[0]

L = boston['feature_names']
varnames=[]


for i in range (0, n):
    for j in range (0, c):
        if i == supp[j]:
            varnames.append(L[i])          


 pm.summary(trace, varnames=varnames)

然后控制台会显示“KeyError: RM”,这是所用变量的名称之一。我注意到一个问题,varnames 的每个对象都被归类为str_ object of numpy module,这意味着我无法读取列表中保留变量的名称,除非我双击它们。

我该如何解决这个问题?我不知道我做错了什么。

标签: pythonbayesianpymc3

解决方案


推荐阅读