Note
Go to the end to download the full example code
Default DL85Classifier¶
######################################################################
# DL8.5 default classifier #
######################################################################
Model building...
Model built. Duration of building = 0.0041
Confusion Matrix below
[[ 13 21]
[ 0 129]]
Accuracy DL8.5 on training set = 0.8313
Accuracy DL8.5 on test set = 0.8712
##############################################################
# DL8.5 classifier : Manual cross-validation (5-fold) #
##############################################################
Model building...
Model built. Duration of building = 0.0246
Average accuracy on training set = 0.8319
Average accuracy on test set = 0.8227
##############################################################
# DL8.5 classifier : Automatic cross-validation (5-fold) #
##############################################################
Model building...
Model built. Duration of building = 0.0272
Average accuracy on test set = 0.8239
import numpy as np
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, make_scorer
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
import time
from dl85 import DL85Classifier
dataset = np.genfromtxt("../datasets/anneal.txt", delimiter=' ')
X, y = dataset[:, 1:], dataset[:, 0]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
print("######################################################################\n"
"# DL8.5 default classifier #\n"
"######################################################################")
clf = DL85Classifier(max_depth=2)
start = time.perf_counter()
print("Model building...")
clf.fit(X, y)
duration = time.perf_counter() - start
print("Model built. Duration of building =", round(duration, 4))
y_pred = clf.predict(X_test)
print("Confusion Matrix below")
print(confusion_matrix(y_test, y_pred))
print("Accuracy DL8.5 on training set =", round(clf.accuracy_, 4))
print("Accuracy DL8.5 on test set =", round(accuracy_score(y_test, y_pred), 4), "\n\n\n")
print("##############################################################\n"
"# DL8.5 classifier : Manual cross-validation (5-fold) #\n"
"##############################################################")
kf = KFold(n_splits=5, random_state=42, shuffle=True)
training_accuracies = []
test_accuracies = []
start = time.perf_counter()
print("Model building...")
for train_index, test_index in kf.split(X):
data_train = X[train_index]
target_train = y[train_index]
data_test = X[test_index]
target_test = y[test_index]
clf = DL85Classifier(max_depth=2)
clf.fit(data_train, target_train)
preds = clf.predict(data_test)
training_accuracies.append(clf.accuracy_)
test_accuracies.append(accuracy_score(target_test, preds))
duration = time.perf_counter() - start
print("Model built. Duration of building =", round(duration, 4))
print("Average accuracy on training set =", round(np.mean(training_accuracies), 4))
print("Average accuracy on test set =", round(np.mean(test_accuracies), 4), "\n\n\n")
print("##############################################################\n"
"# DL8.5 classifier : Automatic cross-validation (5-fold) #\n"
"##############################################################")
clf = DL85Classifier(max_depth=2)
start = time.perf_counter()
print("Model building...")
scores = cross_val_score(clf, X, y, cv=5)
duration = time.perf_counter() - start
print("Model built. Duration of building =", round(duration, 4))
print("Average accuracy on test set =", round(np.mean(scores), 4))
Total running time of the script: ( 0 minutes 0.097 seconds)