作业(二十)——sklearn

包含需要的函数

from sklearn.datasets import make_classification
from sklearn.cross_validation import KFold
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score,f1_score,roc_auc_score

建立一个分类数据集

dataset = make_classification(n_samples=1000, n_features=10, n_informative=2, n_redundant=2, n_repeated=0, n_classes=2)
print("data")
print(dataset[0])
print("target")
print(dataset[1])



十折交叉验证划分训练集和测试集

kf = KFold(len(dataset[0]), n_folds=10, shuffle=True)
for train_index, test_index in kf:
    X_train, y_train = dataset[0][train_index], dataset[1][train_index]
    X_test, y_test = dataset[0][test_index], dataset[1][test_index]

朴素贝叶斯模型训练及其评估结果

clf = GaussianNB()
clf.fit(X_train, y_train)
pred = clf.predict(X_test)
acc = accuracy_score(y_test, pred)
print(acc)
f1 = f1_score(y_test, pred)
print(f1)
auc = roc_auc_score(y_test, pred)
print(auc)

SVC模型训练及其评估结果

clf = SVC(C=100, kernel='rbf', gamma=0.1)
clf.fit(X_train, y_train)
pred = clf.predict(X_test)
acc = accuracy_score(y_test, pred)
print(acc)
f1 = f1_score(y_test, pred)
print(f1)
auc = roc_auc_score(y_test, pred)
print(auc)

随机深林分类模型训练及其评估结果

clf = RandomForestClassifier(n_estimators=100)
clf.fit(X_train, y_train)
pred = clf.predict(X_test)
acc = accuracy_score(y_test, pred)
print(acc)
f1 = f1_score(y_test, pred)
print(f1)
auc = roc_auc_score(y_test, pred)
print(auc)

平均来看,对于这组数据是随机深林分类模型最好

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭