sklearn
程序员文章站
2022-07-14 12:52:50
...
Code:
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
#Create a classification dataset
X, y = datasets.make_classification(n_samples=1000, n_features=10,
n_informative=2, n_redundant=2, n_repeated=0, n_classes=2)
#Split the dataset using 10-fold cross validation
kf = cross_validation.KFold(len(X), 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]
#Train the algorithms GaussianNB
clf1 = GaussianNB()
clf1.fit(X_train, y_train)
pred1 = clf1.predict(X_test)
print('pred1:\n', pred1)
print('y_test:\n', y_test)
#Evaluate the cross-validated performance
acc1 = metrics.accuracy_score(y_test, pred1)
print('acc1:\n', acc1)
f11 = metrics.f1_score(y_test, pred1)
print('f11:\n', f11)
auc1 = metrics.roc_auc_score(y_test, pred1)
print('auc1:\n', auc1)
#Train the algorithms SVC
clf2 = SVC(C=1e-01, kernel='rbf', gamma=0.1)
clf2.fit(X_train, y_train)
pred2 = clf2.predict(X_test)
print('pred2:\n', pred2)
print('y_test:\n', y_test)
# Evaluate the cross-validated performance
acc2 = metrics.accuracy_score(y_test, pred2)
print('acc2:\n', acc2)
f12 = metrics.f1_score(y_test, pred2)
print('f12:\n', f12)
auc2 = metrics.roc_auc_score(y_test, pred2)
print('auc2:\n', auc2)
#Train the algorithms RandomForestClassifier
clf3 = RandomForestClassifier(n_estimators=6)
clf3.fit(X_train, y_train)
pred3 = clf3.predict(X_test)
print('pred3:\n', pred3)
print('y_test:\n', y_test)
#Evaluate the cross-validated performance
acc3 = metrics.accuracy_score(y_test, pred3)
print('acc3:\n', acc3)
f13 = metrics.f1_score(y_test, pred3)
print('f13:\n', f13)
auc3 = metrics.roc_auc_score(y_test, pred3)
print('auc3:\n', auc3)
上一篇: python重载类的特殊方法
下一篇: Nginx安装与学习笔记
推荐阅读
-
基于sklearn数字识别(python开发)
-
逻辑回归原理,推导,sklearn应用
-
使用sklearn进行对数据标准化、归一化以及将数据还原的方法
-
sklearn使用之Pipeline、FeatureUnion、GridSearchCV代码示例
-
对python sklearn one-hot编码详解
-
K-Means 聚类算法 python sklearn
-
对sklearn的使用之数据集的拆分与训练详解(python3.6)
-
Python 之 sklearn——logistic回归
-
使用sklearn对iris数据集进行聚类分析
-
使用sklearn之LabelEncoder将Label标准化的方法