如何在sklearn中使用Kfold交叉验证进行负二项回归?

2024-09-24 06:30:15 发布

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

我将在数据集上应用负二项回归模型,并使用交叉验证(K-Fold)检查模型分数和特征的权重和显著性。下面是应用MinMax scaler后的数据帧。w4是一个分类变量

data.head()


     w1      w2      w3      w4     Y
0   0.17    0.44    0.00    2004    1   
1   0.17    0.83    0.22    2004    0   
2   0.00    1.00    0.34    2005    0
3   1.00    0.00    1.00    2005    1
4   1.00    0.22    0.12    2006    3

我使用以下代码在测试数据集上获得训练模型的分数,但似乎在为模型寻址训练和测试数据集时存在问题。如果有人能帮忙,我将不胜感激

scores = []
kfold = KFold(n_splits=10, shuffle=True, random_state=1)
for train, test in kfold.split(data):
    model = smf.glm(formula = "Y ~ w1 + w2 + w3 + C(w4)", data=X.iloc[train,:], family=sm.families.NegativeBinomial()).fit()
    scores = scores.append(model.get_prediction(X.iloc[test,:])
    
print(scores)

Tags: 数据模型testdatamodeltrain分数w1
1条回答
网友
1楼 · 发布于 2024-09-24 06:30:15

你定义了X和Y吗?似乎您正在将data数据帧传递给kfold.split方法,但稍后您将X和Y作为数据对象引用。尝试先设置X = data[['w1', 'w2', 'w3', 'w4']],然后像在示例中那样引用它们

另外,我注意到您在scores = model.get_prediction(X.iloc[test,:])中覆盖了原始的scores列表 例如:

X = data[['w1', 'w2', 'w3', 'w4']].values
Y = data['Y'].values
preds, scores = [], []
kfold = KFold(n_splits=10, shuffle=True, random_state=1)
for train_idx, test_idx in kfold.split(data):
    X_train, X_test = X[train_idx], X[test_idx]
    y_test = Y[test_idx]
    model = smf.glm(formula = "Y ~ w1 + w2 + w3 + C(w4)", 
                    data=X_train, 
                    family=sm.families.NegativeBinomial()).fit()
    preds.append(model.get_prediction(X_test))
    scores.append(model.score(X_test, y_test))
print(scores)

相关问题 更多 >