def train_predict(clf, X_train, y_train, X_test): preds_br = np.zeros((X_test.shape[0], 9)) for i in range(0, 9): clf.fit(X_train, y_train[:, i]) preds_br[:, i] = clf.predict_proba(X_test)[:, 1] nn_preds = np.array([]) n_iter = 20 for i in range(n_iter): nn_clf.fit(X_train, y_train) s_preds = nn_clf.predict_proba(X_test) nn_preds = nn_preds + s_preds if nn_preds.size else s_preds nn_preds = (nn_preds / n_iter) n_chains = 20 preds_cc = np.zeros((X_test.shape[0], 9)) for i in range(n_chains): cc = ClassifierChain(clf) cc.fit(X_train, y_train) preds_cc = preds_cc + cc.predict(X_test) preds_cc = preds_cc / n_chains preds_br = (preds_br + 3*nn_preds + 2*preds_cc) / 6 return preds_br
def train_predict(clf, X_train, y_train, X_test): preds_br = np.zeros((X_test.shape[0], 9)) for i in range(0, 9): clf.fit(X_train, y_train[:, i]) preds_br[:, i] = clf.predict_proba(X_test)[:, 1] nn_preds = np.array([]) n_iter = 20 for i in range(n_iter): nn_clf.fit(X_train, y_train) s_preds = nn_clf.predict_proba(X_test) nn_preds = nn_preds + s_preds if nn_preds.size else s_preds nn_preds = (nn_preds / n_iter) n_chains = 20 preds_cc = np.zeros((X_test.shape[0], 9)) for i in range(n_chains): cc = ClassifierChain(clf) cc.fit(X_train, y_train) preds_cc = preds_cc + cc.predict(X_test) preds_cc = preds_cc / n_chains preds_br = (preds_br + 3 * nn_preds + 2 * preds_cc) / 6 return preds_br
def train_predict(feature, clf, X_train, y_train, X_test): preds_br = np.zeros((X_test.shape[0], 9)) for i in range(0, 9): clf.fit(X_train, y_train[:, i]) preds_br[:, i] = clf.predict_proba(X_test)[:, 1] np.save('test/' + feature + '_br', preds_br) nn_preds = np.array([]) n_iter = 40 for i in range(n_iter): nn_clf.fit(X_train, y_train) s_preds = nn_clf.predict_proba(X_test) nn_preds = nn_preds + s_preds if nn_preds.size else s_preds nn_preds = (nn_preds / n_iter) np.save('test/' + feature + '_nn', nn_preds) n_chains = 40 preds_cc = np.zeros((X_test.shape[0], 9)) for i in range(n_chains): cc = ClassifierChain(clf) cc.fit(X_train, y_train) preds_cc = preds_cc + cc.predict(X_test) preds_cc = preds_cc / n_chains np.save('test/' + feature + '_cc', preds_cc) preds_br = (preds_br + 3 * nn_preds + 2 * preds_cc) / 6 return preds_br
def train_predict(fold, feature, clf, X_train, y_train, X_test, y_test): preds_br = np.zeros((X_test.shape[0], 9)) for i in range(0, 9): clf.fit(X_train, y_train[:, i]) preds_br[:, i] = clf.predict_proba(X_test)[:, 1] np.save('val3/' + str(fold) + '_' + feature + '_br', preds_br) nn_preds = np.array([]) n_iter = 10 for i in range(n_iter): nn_clf.fit(X_train, y_train) s_preds = nn_clf.predict_proba(X_test) nn_preds = nn_preds + s_preds if nn_preds.size else s_preds nn_preds = (nn_preds / n_iter) np.save('val3/' + str(fold) + '_' + feature + '_nn', nn_preds) n_chains = 10 preds_cc = np.zeros((X_test.shape[0], 9)) for i in range(n_chains): cc = ClassifierChain(clf) cc.fit(X_train, y_train) preds_cc = preds_cc + cc.predict(X_test) preds_cc = preds_cc / n_chains np.save('val3/' + str(fold) + '_' + feature + '_cc', preds_cc) np.save('val3/' + str(fold) + '_' + feature + '_test', y_test) preds_br = (preds_br + 3*nn_preds + 2*preds_cc) / 6 return preds_br
def do_cross_validation(X, Y): X, Y = shuffle(X, Y) folds = 10 kf = KFold(n_splits=folds, shuffle=True) cc = ClassifierChain() zero_one_score = 0 hamming_score = 0 accuracy = 0 for train, test in kf.split(X): X_train, y_train = X[train], Y[train] X_test, y_test = X[test], Y[test] cc.fit(X_train, y_train) zero_one_score += cc.zero_one_loss_score(X_test, y_test) hamming_score += cc.hamming_loss_score(X_test, y_test) accuracy += cc.accuracy_score(X_test, y_test) zero_one_score /= folds hamming_score /= folds accuracy /= folds #print "0/1 Loss:", zero_one_score #print "Hamming Loss:", hamming_score #print "Accuracy:", accuracy #print return [zero_one_score, hamming_score, accuracy]