sklearn(6.13作业)

由于端午...所以这次作业现在才提交...


作业步骤

直接上代码

from sklearn import datasets, metrics
from sklearn.model_selection import KFold
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
import numpy as np

def result_output(y_test, pred):
    print("Accuracy: ", metrics.accuracy_score(y_test, pred))
    try:  
        print("F1-score: ", metrics.f1_score(y_test, pred))
    except:
        print("F1-score Error!")
    try:
        print("AUC ROC ", metrics.roc_auc_score(y_test, pred))
    except ValueError:
        print("ROC AUC score is not defined when only one class present in y_true")


# dataset = datasets.load_wine(return_X_y=True)
dataset = datasets.make_classification(n_samples=2000, n_features=10)
kf = KFold(n_splits=10)
k = 0
for train_index, test_index in kf.split(dataset[0]):
    X_train, X_test = dataset[0][train_index], dataset[0][test_index]
    y_train, y_test = dataset[1][train_index], dataset[1][test_index]
    k += 1
    print("***********************************")
    print("Test %d" % k)
    print("Naive Bayes:")
    clf = GaussianNB()
    clf.fit(X_train, y_train)
    pred = clf.predict(X_test)
    result_output(y_test, pred)
    print("---------------------------")
    print("SVM:")
    clf = SVC(C=1e-2, kernel='rbf', gamma=0.1)
    clf.fit(X_train, y_train)
    pred = clf.predict(X_test)
    result_output(y_test, pred)
    print("---------------------------")
    print("Random Forest:")
    clf = RandomForestClassifier(n_estimators=10)
    clf.fit(X_train, y_train)
    pred = clf.predict(X_test)
    result_output(y_test, pred)

以下是其中的若干次结果:


Test 1
Test 5
Test 9

你可能感兴趣的:(sklearn(6.13作业))