Weak15 Sklearn Homework
程序员文章站
2022-03-11 20:00:22
...
代码:
import sklearn
from sklearn import datasets
from sklearn import cross_validation
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
dataset = datasets.make_classification(n_samples=1000,n_features=10)
data = dataset[0]
target = dataset[1]
kf = cross_validation.KFold(len(data),n_folds=10,shuffle=True)
先import一堆库,以及生成数据集和kf
GaussianNB:
i = 1
for train_index,test_index in kf:
x_train,y_train = data[train_index],target[train_index]
x_test,y_test = data[test_index],target[test_index]
clf = GaussianNB()
clf.fit(x_train,y_train)
pred = clf.predict(x_test)
print("Group:",i)
i += 1
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))
SVC:
for c in [1e-02, 1e-01, 1e00, 1e01, 1e02]:
i = 1
for train_index,test_index in kf:
x_train,y_train = data[train_index],target[train_index]
x_test,y_test = data[test_index],target[test_index]
clf = SVC(C=c,kernel='rbf',gamma=0.1)
clf.fit(x_train,y_train)
pred = clf.predict(x_test)
print("Group:",i)
i += 1
print("C = ",c)
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))
截图只截每个C的第一组。
RandomForestClassifier:
for n in [10, 100, 1000]:
i = 1
for train_index,test_index in kf:
x_train,y_train = data[train_index],target[train_index]
x_test,y_test = data[test_index],target[test_index]
clf = RandomForestClassifier(n_estimators=n)
clf.fit(x_train,y_train)
pred = clf.predict(x_test)
print("Group:",i)
i += 1
print("n_estimators = ",n)
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))
截图只截每个n_estimators的第一组。
分析:
实验使用相同的数据集,从结果可知随机森林算法的精确度比其他两种算法要高。SVC需要选择适合的参数才能得到一个比较高的精确度。朴素贝叶斯算法比适合参数的SVM略差,但是比其余参数的SVC略好。