我将在数据集上应用负二项回归模型,并使用交叉验证(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)
你定义了X和Y吗?似乎您正在将
data
数据帧传递给kfold.split
方法,但稍后您将X和Y作为数据对象引用。尝试先设置X = data[['w1', 'w2', 'w3', 'w4']]
,然后像在示例中那样引用它们另外,我注意到您在
scores = model.get_prediction(X.iloc[test,:])
中覆盖了原始的scores
列表 例如:相关问题 更多 >
编程相关推荐