-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Cour
committed
Apr 13, 2018
1 parent
1312d9d
commit e3e6681
Showing
4 changed files
with
83 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,38 @@ | ||
|
||
from sklearn.metrics import mean_squared_error, average_precision_score, confusion_matrix, accuracy_score, f1_score | ||
from collections import defaultdict | ||
from sklearn.model_selection import KFold | ||
import audiphil as au | ||
import numpy as np | ||
|
||
def kfold(X, y, num_k, clfs_names): | ||
|
||
def kfold(X, y, clfs_names, num_splits, cm=False, cm_size=15): | ||
classifiers = [] | ||
d = defaultdict(list) | ||
labels = sorted(list(set(y))) | ||
for sm_c, name in clfs_names: | ||
|
||
scores = [] | ||
|
||
kf = KFold(n_splits=num_splits, shuffle=True) | ||
|
||
for train_index, test_index in kf.split(X): | ||
X_train, X_test = a(X[train_index]), a(X[test_index]) | ||
y_train, y_test = a(y[train_index]), a(y[test_index]) | ||
X_train, X_test = np.array(X[train_index]), np.array(X[test_index]) | ||
y_train, y_test = np.array(y[train_index]), np.array(y[test_index]) | ||
|
||
cur = sm_c() | ||
cur = cur.fit(X_train, y_train) | ||
y_res = cur.predict(X_test) | ||
|
||
cur_acc = f1_score(y_test, y_res, average="macro") | ||
#cur_acc = f1_score(y_test, y_res, average="macro") | ||
cur_acc = accuracy_score(y_test, y_res) | ||
|
||
if cm: | ||
mycm = confusion_matrix(y_test, y_res, labels=labels) | ||
au.plot_confusion_matrix(mycm, classes=labels, normalize=False, title="{} {}".format(name, "%0.2f" % accuracy_score(y_test, y_res)), cm_size=cm_size) | ||
|
||
scores.append(cur_acc) | ||
classifiers.append(cur) | ||
|
||
scores = np.array(scores) | ||
d[name].append("KFOLD: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2)) | ||
|
||
return d | ||
|
||
return (d, classifiers) | ||
|