题目
![](https://i-blog.csdnimg.cn/blog_migrate/c1d4032a19ec4e37cad3d7d40048f166.png)
代码
from sklearn import datasets,cross_validation
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
def ans(y_test, pred):
print("Accuracy:", metrics.accuracy_score(y_test, pred))
print("F1-score:", metrics.f1_score(y_test, pred))
print("AUC ROC:",metrics.roc_auc_score(y_test, pred))
# 生成数据集
dataset = datasets.make_classification(n_samples=2000, n_features=20, n_repeated=0, n_classes=2)
kf = cross_validation.KFold(len(dataset[1]), n_folds=10, shuffle=True) # 交叉检验划分
num = 0
for train_index, test_index in kf:
x_train, x_test = dataset[0][train_index], dataset[0][test_index]
y_train, y_test = dataset[1][train_index], dataset[1][test_index]
num += 1
print("================================")
print("Test " + str(num))
# 朴素贝叶斯
print("Naive Bayes:")
clf = GaussianNB()
clf.fit(x_train, y_train)
pred = clf.predict(x_test)
ans(y_test, pred)
print("--------------------------------")
# 支持向量机
print("SVM:")
clf = SVC(C=1e-01, kernel='rbf', gamma=0.1)
clf.fit(x_train, y_train)
pred = clf.predict(x_test)
ans(y_test, pred)
print("--------------------------------")
# 随机森林
print("Random Forest:")
clf = RandomForestClassifier(n_estimators=100)
clf.fit(x_train, y_train)
pred = clf.predict(x_test)
ans(y_test, pred)
print("================================")
结果截图 (选择其中的某几次测试结果)
Test 1:
![](https://i-blog.csdnimg.cn/blog_migrate/3c9a67d59b5b52d2f53b056783ad0383.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0bae10fba17c3f8cd6a8c3dc0bbb8909.png)