欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

week15

程序员文章站 2024-03-17 21:34:52
...

Assignment
week15

from sklearn import datasets
from sklearn.naive_bayes import GaussianNB
from sklearn import cross_validation
from sklearn.svm import SVC
from sklearn import metrics

# 生成数据集
data = datasets.make_classification(n_samples=1000, n_features=10)
test = 0;
X, Y = data[0], data[1];
kf = cross_validation.KFold(len(Y), n_folds=10, shuffle=True)
for train_index, test_index in kf:
    X_train, y_train = X[train_index], Y[train_index]
    X_test, y_test = X[test_index], Y[test_index]
    test += 1;
    print("\nTEST ", test);
#朴素贝叶斯Naive Bayes
    clf = GaussianNB()
    clf.fit(X_train, y_train)
    pred = clf.predict(X_test)
    print("Naive Bayes: ");
    acc = metrics.accuracy_score(y_test, pred)
    print ("acc: ", acc)
    f1 = metrics.f1_score(y_test, pred)
    print ("f1: ", f1)
    auc = metrics.roc_auc_score(y_test, pred)
    print ("auc: ", auc)


#SVM
    clf = SVC(C=1e-01, kernel='rbf', gamma=0.1)
    clf.fit(X_train, y_train)
    pred = clf.predict(X_test)
    print("\nSVM: ");
    acc = metrics.accuracy_score(y_test, pred)
    print ("acc: ", acc)
    f1 = metrics.f1_score(y_test, pred)
    print ("f1: ", f1)
    auc = metrics.roc_auc_score(y_test, pred)
    print ("auc: ", auc)

#Random Forest
    from sklearn.ensemble import RandomForestClassifier
    clf = RandomForestClassifier(n_estimators=6)
    clf.fit(X_train, y_train)
    pred = clf.predict(X_test)
    print("\nRandom Forest: ");
    acc = metrics.accuracy_score(y_test, pred)
    print ("acc: ", acc)
    f1 = metrics.f1_score(y_test, pred)
    print ("f1: ", f1)
    auc = metrics.roc_auc_score(y_test, pred)
    print ("auc: ", auc)

TEST  1
Naive Bayes: 
acc:  0.84
f1:  0.8431372549019607
auc:  0.8399359743897561

SVM: 
acc:  0.85
f1:  0.8484848484848485
auc:  0.8505402160864346

Random Forest: 
acc:  0.9
f1:  0.9019607843137255
auc:  0.8999599839935973

TEST  2
Naive Bayes: 
acc:  0.91
f1:  0.9072164948453608
auc:  0.9134460547504026

SVM: 
acc:  0.93
f1:  0.924731182795699
auc:  0.9303542673107892

Random Forest: 
acc:  0.91
f1:  0.9052631578947369
auc:  0.9118357487922706

TEST  3
Naive Bayes: 
acc:  0.92
f1:  0.923076923076923
auc:  0.920915295062224

SVM: 
acc:  0.92
f1:  0.9199999999999999
auc:  0.9233239662786029

Random Forest: 
acc:  0.95
f1:  0.9523809523809524
auc:  0.9504215174628663

TEST  4
Naive Bayes: 
acc:  0.87
f1:  0.8571428571428571
auc:  0.8677238057005219

SVM: 
acc:  0.87
f1:  0.853932584269663
auc:  0.8665194700923324

Random Forest: 
acc:  0.93
f1:  0.924731182795699
auc:  0.9291449217181854

TEST  5
Naive Bayes: 
acc:  0.87
f1:  0.8631578947368421
auc:  0.8693910256410255

SVM: 
acc:  0.92
f1:  0.9130434782608695
auc:  0.9182692307692308

Random Forest: 
acc:  0.95
f1:  0.946236559139785
auc:  0.9487179487179486

TEST  6
Naive Bayes: 
acc:  0.94
f1:  0.9473684210526316
auc:  0.95

SVM: 
acc:  0.89
f1:  0.8990825688073394
auc:  0.9083333333333333

Random Forest: 
acc:  0.88
f1:  0.890909090909091
auc:  0.8958333333333334

TEST  7
Naive Bayes: 
acc:  0.91
f1:  0.8915662650602411
auc:  0.9088466308391897

SVM: 
acc:  0.91
f1:  0.8860759493670887
auc:  0.9014055394791236

Random Forest: 
acc:  0.95
f1:  0.9382716049382716
auc:  0.9464654816039686

TEST  8
Naive Bayes: 
acc:  0.89
f1:  0.897196261682243
auc:  0.8944805194805197

SVM: 
acc:  0.91
f1:  0.9158878504672897
auc:  0.9147727272727273

Random Forest: 
acc:  0.91
f1:  0.9174311926605505
auc:  0.9123376623376623

TEST  9
Naive Bayes: 
acc:  0.87
f1:  0.8686868686868686
auc:  0.8701480592236894

SVM: 
acc:  0.88
f1:  0.8775510204081631
auc:  0.8799519807923168

Random Forest: 
acc:  0.91
f1:  0.9010989010989012
auc:  0.908563425370148

TEST  10
Naive Bayes: 
acc:  0.9
f1:  0.8979591836734694
auc:  0.8999599839935974

SVM: 
acc:  0.9
f1:  0.8936170212765957
auc:  0.8991596638655462

Random Forest: 
acc:  0.91
f1:  0.9072164948453607
auc:  0.909763905562225

对比数据可以看出,Random Forest算法的结果要好。