python - 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
,这意味着我无法读取列表中保留变量的名称,除非我双击它们。
我该如何解决这个问题?我不知道我做错了什么。
解决方案
推荐阅读
- grep - 使用 grep -v '^$' 时出现非法变量名错误
- android - 谷歌地图 - TileOverlay - 拉伸瓷砖以获得更高的缩放级别
- python - 如何键入提示 Python 函数返回从超类派生的任何类的实例?
- excel - 从 Excel 保存多个 PDF 文件
- kotlin - 在泛型中使用 Nothing 是一个好习惯吗?
- javascript - 如何针对不同的数组转换或排序数组?
- r - 仅在单击菜单项时加载闪亮模块
- sap - SAPJco 调用 BAPI_MATERIAL_DISPLAY
- python - 大熊猫整数到日期时间索引的多索引?
- scala - 在 Typesafe Config 中处理环境变量