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

sklearn

程序员文章站 2022-07-14 12:52:50
...

sklearn


sklearn
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)