常用方法:(1)简单分离训练集和测试集,(2)K折交叉验证分离
1.最简单分离测试集和测试集:train_test_split
train_test_split是交叉验证中常用的函数,功能是从样本中随机的按比例选取训练集和测试集。
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
#导入数据
filename = "d:/my_project/input/pima-indians-diabetes.data.csv"
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = pd.read_csv(filename, names=names)
#将数据分为输入数据和输出数据
X = data.iloc[:, 0:8].as_matrix()
y = data.iloc[:, 8].as_matrix()
#划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=4)
#训练模型
model = LogisticRegression()
model.fit(X_train, y_train)
result = model.score(X_test, y_test)
print("算法评估结果:%.3f%%" % (result))
- K折交叉验证
K折交叉验证是用来评估机器学习算法的黄金法则,K=10是一般的建议。
import cross_val_score
import sklearn.model_selection import KFold
kfold = KFold(n_splits=10, shuffle=False)
model = LogisticRegression()
result = cross_val_score(model, X, y, cv=kfold)
print("评估结果:%.3f, %.3f" %(result.mean(), result.std()))
Snipaste_2018-05-16_11-19-57.png
Stratified k-fold与k-fold类似,将数据集划分成k份,不同点在于,划分的k份中,每一份内各个类别数据的比例和原始数据集中各个类别的比例相同。
网友评论