十折交叉验证python_机器学习:验证方法及示例

在这篇文章中,我们将讨论以下概念,这些概念都旨在评估机器学习分类模型的性能:

  1. 交叉验证模型。
  2. 混淆矩阵。
  3. ROC曲线。
  4. Cohen's κ score。

导入Python库

import numpy as npimport pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltimport warningswarnings.filterwarnings('ignore')
5cfdd3b5690735a7e58e48a6416917fa.png

我们首先创建具有三个特征和二元标签的简单机器学习数据集。Python代码如下:

from sklearn.model_selection import train_test_split# Creating the datasetN = 1000 # number of samplesdata = {'A': np.random.normal(100, 8, N), 'B': np.random.normal(60, 5, N), 'C': np.random.choice([1, 2, 3], size=N, p=[0.2, 0.3, 0.5])}df = pd.DataFrame(data=data)# Labeling def get_label(A, B, C): if A < 95: return 1 elif C == 1: return 1 elif B > 68 or B < 52: return 1 return 0df['label'] = df.apply(lambda row: get_label(row['A'],row['B'],row['C']),axis=1)# Dividing to train and test setX = np.asarray(df[['A', 'B', 'C']])y = np.asarray(df['label'])X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
61833a79235c97f8cc42706a9852f2fe.png

让我们尝试使用简单的逻辑回归来进行演示。

from sklearn import linear_modelfrom sklearn.model_selection import cross_val_scoreclf = linear_model.LogisticRegression()clf.fit(X_train, y_train)print(">> Score of the classifier on the train set is: 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值