def rakeld_ensemble(vec, label): problem_transform_classifier = LabelPowerset(classifier=LinearSVC(), require_dense=[False, True]) classifier = RakelD(classifier=problem_transform_classifier, labelset_size=5) classifier.fit(vec, label) return classifier
def RAkELd(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, base_clasif, num_labels): classifier = RakelD(base_classifier=base_clasif, labelset_size=num_labels) classifier.fit(dataset_train_x, dataset_train_y) predictions = classifier.predict(dataset_test_x) Metrics_Accuracy("RAkELd", predictions, dataset_test_y)
def build_Rake(X_train, y_train, X_test, y_test): classifier = RakelD(base_classifier=GaussianNB(), base_classifier_require_dense=[True, True], labelset_size=4) classifier.fit(X_train, y_train) prediction = classifier.predict(X_test) print('Test accuracy is {}'.format(accuracy_score(y_test, prediction)))
def RAkELd(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, base_clasif, num_labels): classifier = RakelD(base_classifier=base_clasif, labelset_size=num_labels) start_time = time.time() classifier.fit(dataset_train_x, dataset_train_y) stop_time = time.time() time_lapsed = stop_time - start_time predictions = classifier.predict(dataset_test_x) Metrics_Accuracy("RAkELd", predictions, dataset_test_y) print("Execution time: {}s".format(time_lapsed))
def run(classifier, train_test_set): X_train, X_test, y_train, y_test = train_test_set # init model and fit to train data rakel = RakelD(base_classifier=classifier) rakel.fit(X_train, y_train) # make predictions y_pred = rakel.predict(X_test) print('\n--------Rakel with {:}'.format(rakel)) return y_test, y_pred
def RAkEL_fit(clfs, steps, X_train, y_train, X_test, y_test): metrics = {} for key, clf in zip(clfs.keys(), clfs.values()): acc = [] prec_micro = [] prec_macro = [] hamm_loss = [] f1_micro = [] f1_macro = [] print('Fitting RAkEL with Base Classifier: %s' % key) for k in steps: classifier = RakelD(base_classifier=clf, labelset_size=k) classifier.fit(X_train, y_train) prediction = classifier.predict(X_test) acc.append(accuracy_score(y_test, prediction)) prec_micro.append(precision_score(y_test, prediction, average='micro')) prec_macro.append(precision_score(y_test, prediction, average='macro')) hamm_loss.append(hamming_loss(y_test, prediction)) f1_micro.append(f1_score(y_test, prediction, average='micro')) f1_macro.append(f1_score(y_test, prediction, average='macro')) metrics[key] = [acc, hamm_loss, f1_micro, f1_macro, prec_micro, prec_macro] return metrics
from sklearn.metrics import f1_score from sklearn.metrics import jaccard_similarity_score from sklearn.metrics import precision_score from sklearn.metrics import recall_score from sklearn.naive_bayes import GaussianNB from skmultilearn.ensemble import RakelD from datetime import timedelta import time start = time.time() classifier = RakelD(base_classifier=GaussianNB(), base_classifier_require_dense=[True, True], labelset_size=4) classifier.fit(x_train, y_train) predictions = classifier.predict(x_test) # accuracy print("Accuracy = ", accuracy_score(y_test, predictions)) print("\n") print("F1 = ", f1_score(y_test, predictions, average='micro')) print("\n") print("Jaccard = ", jaccard_similarity_score(y_test, predictions)) print("\n") print("Precision = ", precision_score(y_test, predictions, average='micro')) print("\n") print("Recall = ", recall_score(y_test, predictions, average='micro'))