【问题标题】:cross-validation with Kfold使用 Kfold 进行交叉验证
【发布时间】:2019-09-06 15:28:09
【问题描述】:

我正在尝试使用三个与银行历史相关的二元解释变量:违约、住房和贷款,以使用逻辑回归分类器预测二元响应变量。

我有以下数据集:

将文本 no/yes 转换为整数 0/1 的映射函数

convert_to_binary = {'no' : 0, 'yes' : 1}
default = bank['default'].map(convert_to_binary)
housing = bank['housing'].map(convert_to_binary)
loan = bank['loan'].map(convert_to_binary)
response = bank['response'].map(convert_to_binary)

我添加了我的三个解释变量和对数组的响应

data = np.array([np.array(default), np.array(housing), np.array(loan),np.array(response)]).T

kfold = KFold(n_splits=3)

scores = []
for train_index, test_index in kfold.split(data):
    X_train, X_test = data[train_index], data[test_index]
    y_train, y_test = response[train_index], response[test_index]
    model = LogisticRegression().fit(X_train, y_train)
    pred = model.predict(data[test_index])
    results = model.score(X_test, y_test)
    scores.append(results)
print(np.mean(scores))

我的准确率始终是 100%,我知道这是不正确的。准确率应该在 50-65% 左右?

我做错了什么吗?

【问题讨论】:

  • 你的数据集有多大?
  • 4521 行 × 17 列
  • 我想到了几件事,但主要是,model_data 是什么?如果 model_datadata 相同,则您将响应变量包含在预测变量中,这将解释您的完美模型。

标签: python numpy machine-learning scikit-learn


【解决方案1】:

拆分不正确

这是正确的分割

X_train, X_labels = data[train_index], response[train_index]
y_test, y_labels = data[test_index], response[test_index]
model = LogisticRegression().fit(X_train, X_labels)
pred = model.predict(y_test)
acc = sklearn.metrics.accuracy_score(y_labels,pred,normalize=True)

【讨论】:

  • model.score 不是这样工作的(参见doc),而且你的拆分完全一样,只是写法不同。
猜你喜欢
  • 1970-01-01
  • 2019-04-28
  • 2018-08-27
  • 1970-01-01
  • 2020-04-06
  • 2020-08-31
  • 2015-06-11
  • 2020-07-13
  • 2017-04-21
相关资源
最近更新 更多