def BinaryRelevance(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, base_classif, title): #print(base_classif) classifier = skpt.BinaryRelevance(base_classif) classifier.fit(dataset_train_x, dataset_train_y) predictions = classifier.predict(dataset_test_x) Metrics_Accuracy(title, predictions, dataset_test_y)
def BinaryRelevance(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, base_classif, title): #print(base_classif) classifier = skpt.BinaryRelevance(base_classif) 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(title, predictions, dataset_test_y) print("Execution time: {}s".format(time_lapsed))
def rdbr(x_tr, y_tr, x_te, x_va=None): x_tr = np.array(x_tr) y_tr = np.int32(y_tr) pred_base = pt.BinaryRelevance(svm.LinearSVC()) pred_meta = [svm.LinearSVC() for _ in range(np.size(y_tr, axis=1))] pred_base.fit(x_tr, y_tr) for i, pred in enumerate(pred_meta): x_add = np.copy(y_tr) x = np.hstack( (x_tr, x_add[:, 0:i], x_add[:, i + 1:pred_meta.__len__() + 1])) y = y_tr[:, i] pred.fit(x, y) print('training meta %d' % i) n_te, n_va = 0, 0 if x_va is not None: n_te = np.size(x_te, axis=0) n_va = np.size(x_va, axis=0) x_te = np.vstack((x_te, x_va)) else: x_te = np.array(x_te) y_te_base = sparse.dok_matrix.toarray(pred_base.predict(x_te)) diff_num = 0 while True: y_te_adv = np.zeros([np.size(x_te, axis=0), pred_meta.__len__()]) for i, pred in enumerate(pred_meta): x_add = np.copy(y_te_base) x = np.hstack( (x_te, x_add[:, 0:i], x_add[:, i + 1:pred_meta.__len__() + 1])) y_te_adv[:, i] = pred.predict(x) if diff_num == sum(sum(y_te_base != y_te_adv)): break diff_num = sum(sum(y_te_base != y_te_adv)) y_te_base = y_te_adv y_te_ = y_te_adv if x_va is not None: y_va_ = y_te_[n_te:, :] y_te_ = y_te_[0:n_te, :] return y_te_, y_va_ else: return y_te_
def br(x_tr, y_tr, x_te, x_va=None): """ BR算法,基于SVC,使用默认参数 :param x_tr: :param y_tr: :param x_te: :param x_va: :return: """ pred = pt.BinaryRelevance(svm.LinearSVC()) x_tr = np.array(x_tr) y_tr = np.int32(y_tr) x_te = np.array(x_te) x_va = np.array(x_va) pred.fit(x_tr, y_tr) if x_va is None: y_te_ = sparse.dok_matrix.toarray(pred.predict(x_te)) return y_te_ else: y_te_ = sparse.dok_matrix.toarray(pred.predict(x_te)) y_va_ = sparse.dok_matrix.toarray(pred.predict(x_va)) return y_te_, y_va_
def ctrl(x_tr, y_tr, x_te, x_va=None): p = np.size(y_tr, 1) thr = 0.3 m = 5 if p <= 20 else 7 if p <= 100 else 9 n_tr = np.size(x_tr, 0) n_tr_va = int(n_tr / 5) x_tr = np.array(x_tr) y_tr = np.int32(y_tr) x_tr_va = x_tr[0:n_tr_va, :] y_tr_va = y_tr[0:n_tr_va, :] x_tr = x_tr[n_tr_va:, :] y_tr = y_tr[n_tr_va:, :] pred_base = pt.BinaryRelevance(svm.LinearSVC()) pred_base.fit(x_tr, y_tr) y_tr_va_ = sparse.dok_matrix.toarray(pred_base.predict(x_tr_va)) # filter 1 ev = evaluate.Evaluate() f1 = np.zeros([p], dtype=np.float) for j in range(p): f1[j] = ev.eval_macro_f1(y_tr_va[:, j], y_tr_va_[:, j]) yc = np.where(f1 >= thr, True, False) yc_index = np.array(list(range(p)))[yc] y_tr_c = y_tr[:, yc] # filter 2 r = [] for j in range(p): yc_j = np.where(yc_index == j, False, True) yc_index_j = yc_index[yc_j].tolist() y_tr_c_j = y_tr_c[:, yc_j] y_tr_j = y_tr[:, j] chi2, _ = fs.chi2(y_tr_c_j, y_tr_j) chi2 = chi2.tolist() r_j = [] for k in range(m): if not chi2: break index = np.argmax(chi2) r_j.append(yc_index_j[index]) yc_index_j.remove(yc_index_j[index]) chi2.remove(chi2[index]) r.append(r_j) # predict test and validation sets together n_te, n_va = 0, 0 if x_va is not None: n_te = np.size(x_te, axis=0) n_va = np.size(x_va, axis=0) x_te = np.vstack((x_te, x_va)) else: x_te = np.array(x_te) # get original prediction y_te_ori = sparse.dok_matrix.toarray(pred_base.predict(x_te)) y_te_adv = np.zeros([np.size(x_te, 0), p]) # train meta-classifiers and predict (to reduce storage usage) for j in range(p): pred_meta = [svm.LinearSVC() for _ in r[j]] # train meta-classifiers for label l_j for k, index in enumerate(r[j]): x = np.hstack([x_tr, y_tr[:, index][:, np.newaxis]]) y = y_tr[:, j] pred_meta[k].fit(x, y) # predict phase votes = np.zeros([np.size(x_te, 0)]) for k, index in enumerate(r[j]): x = np.hstack([x_te, y_te_ori[:, index][:, np.newaxis]]) y = pred_meta[k].predict(x) votes += np.where(y == 1, 1, -1) y_te_adv[:, j] = np.where(votes > 0, 1, 0) y_te_ = y_te_adv # predict test and validation sets together if x_va is not None: y_va_ = y_te_[n_te:, :] y_te_ = y_te_[0:n_te, :] return y_te_, y_va_ else: return y_te_
def Util_ClassifierMethods(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y): #BR Util_Title("Binary Relevance") base_classif = GaussianNB() BinaryRelevance(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, base_classif, "GaussianNB") dict_res = FindBestSVCParams(skpt.BinaryRelevance(), dataset_train_x, dataset_train_y) base_classif = SVC(kernel=dict_res['classifier__kernel'], degree=dict_res['classifier__degree']) BinaryRelevance(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, base_classif, "SVC tuned") dict_res = FindBestMNBParams(skpt.BinaryRelevance(), dataset_train_x, dataset_train_y) base_classif = MultinomialNB(alpha=dict_res['classifier__alpha']) BinaryRelevance(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, base_classif, "MNB tuned") #CC Util_Title("Classifier Chain") base_classif = GaussianNB() ClassifierChain(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, base_classif, "GaussianNB") dict_res = FindBestSVCParams(skpt.ClassifierChain(), dataset_train_x, dataset_train_y) base_classif = SVC(kernel=dict_res['classifier__kernel'], degree=dict_res['classifier__degree']) ClassifierChain(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, base_classif, "SVC tuned") dict_res = FindBestMNBParams(skpt.ClassifierChain(), dataset_train_x, dataset_train_y) base_classif = MultinomialNB(alpha=dict_res['classifier__alpha']) ClassifierChain(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, base_classif, "MNB tuned") #LP Util_Title("Label Powerset") base_classif = GaussianNB() LabelPowerset(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, base_classif, "GaussianNB") dict_res = FindBestSVCParams(skpt.LabelPowerset(), dataset_train_x, dataset_train_y) base_classif = SVC(kernel=dict_res['classifier__kernel'], degree=dict_res['classifier__degree']) LabelPowerset(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, base_classif, "SVC tuned") dict_res = FindBestMNBParams(skpt.LabelPowerset(), dataset_train_x, dataset_train_y) base_classif = MultinomialNB(alpha=dict_res['classifier__alpha']) LabelPowerset(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, base_classif, "MNB tuned") #MLkNN Util_Title("MLkNN") dict_res = FindBestK(skadapt.MLkNN(), dataset_train_x, dataset_train_y) MLkNN(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, dict_res['k'], dict_res['s']) #MLARAM Util_Title("MLARAM") dict_res = FindBestVT(dataset_train_x, dataset_train_y) MLARAM(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, dict_res['vigilance'], dict_res['threshold']) #BRkNNa Util_Title("BRkNNa") dict_res = FindBestK(skadapt.BRkNNaClassifier(), dataset_train_x, dataset_train_y) BRkNNa(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, dict_res['k']) #BRkNNb Util_Title("BRkNNb") dict_res = FindBestK(skadapt.BRkNNbClassifier(), dataset_train_x, dataset_train_y) BRkNNb(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, dict_res['k']) #RAkELD Util_Title("RAkELd") dict_res = GridSearchCV_baseRakel(RakelD(), dataset_train_x, dataset_train_y) RAkELd(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, dict_res['base_classifier'], dict_res['labelset_size']) #RAkELo Util_Title("RAkELo") dict_res = GridSearchCV_baseRakel(RakelO(), dataset_train_x, dataset_train_y) RAkELO(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, dict_res['base_classifier'], dict_res['labelset_size'], dict_res['model_count']) #MLTSVM Util_Title("MLTSVM") dict_res = FindCKParam(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y) TwinMLSVM(dataset_train_x, dataset_train_y, dataset_test_x, dataset_test_y, dict_res['c_k'], dict_res['sor_omega'])
def Util_ClassifierMethodsBookmarks(train_x, y_train, test_x, y_test): #Scale negatives for BR/ CC and LP for MultinomialNB x_scaler = preprocessing.MinMaxScaler(feature_range=(0, 1)) train_x_scaledb = x_scaler.fit_transform(train_x) test_x_scaledb = x_scaler.fit_transform(test_x) #BR Util_Title("Binary Relevance") base_classif = GaussianNB() BinaryRelevance(train_x_scaledb, y_train, test_x_scaledb, y_test, base_classif, "GaussianNB") dict_res = FindBestSVCParams(skpt.BinaryRelevance(), train_x_scaledb, y_train) base_classif = SVC(kernel=dict_res['classifier__kernel'], degree=dict_res['classifier__degree']) BinaryRelevance(train_x_scaledb, y_train, test_x_scaledb, y_test, base_classif, "SVC tuned") dict_res = FindBestMNBParams(skpt.BinaryRelevance(), train_x_scaledb, y_train) base_classif = MultinomialNB(alpha=dict_res['classifier__alpha']) BinaryRelevance(train_x_scaledb, y_train, test_x_scaledb, y_test, base_classif, "MNB tuned") #CC Util_Title("Classifier Chain") base_classif = GaussianNB() ClassifierChain(train_x_scaledb, y_train, test_x_scaledb, y_test, base_classif, "GaussianNB") dict_res = FindBestSVCParams(skpt.ClassifierChain(), train_x_scaledb, y_train) base_classif = SVC(kernel=dict_res['classifier__kernel'], degree=dict_res['classifier__degree']) ClassifierChain(train_x_scaledb, y_train, test_x_scaledb, y_test, base_classif, "SVC tuned") dict_res = FindBestMNBParams(skpt.ClassifierChain(), train_x_scaledb, y_train) base_classif = MultinomialNB(alpha=dict_res['classifier__alpha']) ClassifierChain(train_x_scaledb, y_train, test_x_scaledb, y_test, base_classif, "MNB tuned") #LP Util_Title("Label Powerset") base_classif = GaussianNB() LabelPowerset(train_x_scaledb, y_train, test_x_scaledb, y_test, base_classif, "GaussianNB") dict_res = FindBestSVCParams(skpt.LabelPowerset(), train_x_scaledb, y_train) base_classif = SVC(kernel=dict_res['classifier__kernel'], degree=dict_res['classifier__degree']) LabelPowerset(train_x_scaledb, y_train, test_x_scaledb, y_test, base_classif, "SVC tuned") dict_res = FindBestMNBParams(skpt.LabelPowerset(), train_x_scaledb, y_train) base_classif = MultinomialNB(alpha=dict_res['classifier__alpha']) LabelPowerset(train_x_scaledb, y_train, test_x_scaledb, y_test, base_classif, "MNB tuned") #RAkELo Util_Title("RAkELo") lbs_size = 3 mod_count = 2 * y_train.shape[1] #2 * of labels print(mod_count) RAkELO(train_x, y_train, test_x, y_test, LinearSVC(max_iter=500, verbose=1), lbs_size, mod_count) #RAkELd lbs_size = 3 RAkELd(train_x, y_train, test_x, y_test, LinearSVC(verbose=2), lbs_size) #MLkNN base_classif = skadapt.MLkNN() k = 10 s = 1 MLkNN(train_x, y_train, test_x, y_test, k, s) #MLARAM v = 0.95 t = 0.05 dict_res = FindBestVT(train_x, y_train) MLARAM(train_x, y_train, test_x, y_test, dict_res['vigilance'], dict_res['threshold']) #BRkNNa dict_res = FindBestK(skadapt.BRkNNaClassifier(), train_x, y_train) BRkNNa(train_x, y_train, test_x, y_test, dict_res['k']) #BRkNNb dict_res = FindBestK(skadapt.BRkNNbClassifier(), train_x, y_train) BRkNNb(train_x, y_train, test_x, y_test, dict_res['k']) #MLTSVM #Test for 0 #TwinMLSVM(train_x,y_train,test_x,y_test,0,1) #Test for 0.125 TwinMLSVM(train_x, y_train, test_x, y_test, 0.125, 1)
def fit(self, x_tr, y_tr): y_total_dim = np.size(y_tr, axis=1) non_zero_dim = np.sum(y_tr, axis=0) != 0 y_tr = np.array(y_tr)[:, non_zero_dim] y_dim = np.size(y_tr, axis=1) # y空间的维度 m_tr = np.size(y_tr, axis=0) # 训练集的数量 # params max_iter = self.max_iter map_size = self.map_size meta_mode = self.meta_mode imb_rate = self.imb_rate neg_weight_balanced = self.neg_weight_balanced neg_weight_imbalanced = self.neg_weight_imbalanced mask_rate = self.mask_rate decay_rate = self.decay_rate slope = self.slope # slope along x and y direction slope_x = slope / (slope**2 + 1)**(1 / 2) slope_y = 1 / (slope**2 + 1)**(1 / 2) # ------------------- training ------------------ # train base classifiers br_clf = pt.BinaryRelevance(svm.LinearSVC(max_iter=max_iter, C=self.C)) x_tr = np.array(x_tr) y_tr = np.int32(y_tr) br_clf.fit(x_tr, y_tr) base_clfs = br_clf.classifiers_ # train meta classifiers meta_clfs = [None for _ in range(y_dim)] for dim in range(y_dim): # expand dimensions x = self.generate_meta_x(x_tr, y_tr, dim, mode=meta_mode) y = y_tr[:, dim] clf = svm.LinearSVC(max_iter=max_iter, C=self.C) clf.fit(x, y) meta_clfs[dim] = clf # --------------- get predictions ---------------- # get base predictions base_proba = np.vstack( [self.sigmoid(clf.decision_function(x_tr)) for clf in base_clfs]).T base_pred = np.where(base_proba > 0.5, 1, 0) # positions for meta predictions meta_proba = np.zeros(base_proba.shape) meta_pred = np.zeros(base_pred.shape) # get meta predictions for dim in range(y_dim): clf = meta_clfs[dim] x = self.generate_meta_x(x_tr, base_pred, dim, mode=meta_mode) proba = self.sigmoid(clf.decision_function(x)) meta_proba[:, dim] = proba # 真值 true_inds = y_tr > 0.5 # ------------- inner validation ---------------- true_count = np.sum(true_inds, axis=1) lr_min = min(true_count) map_background_list = [] map_adapt_list = [] map_list = [] scale_list = [] position_list = [] gradient_list = [] for dim in range(y_dim): # 坐标变换 draw_x = meta_proba[:, dim] - 0.5 draw_y = base_proba[:, dim] - meta_proba[:, dim] x_max = max(draw_x) x_min = min(draw_x) x_width = x_max - x_min y_max = max(draw_y) y_min = min(draw_y) y_width = y_max - y_min # calculate a value to balance the scale between x axis and y axis scale = max( (np.mean(draw_x**2) / (np.mean(draw_y**2) + eps))**(1 / 2), 1) scale_list.append(scale) # get the size of the of the map x_size = map_size y_size = int(x_size / x_width * y_width * scale) # get the radius of the Gauss mask gauss_mask_r = np.mean(np.abs(draw_x)) * mask_rate gauss_mask_R = int(gauss_mask_r / x_width * map_size) # create Gauss mask mask_x = mask_y = np.arange(-gauss_mask_R, gauss_mask_R + 1) mask_X, mask_Y = np.meshgrid(mask_x, mask_y) dist = np.sqrt(mask_X**2 + mask_Y**2) gauss_mask = np.where(dist > gauss_mask_R, 0, np.exp(-decay_rate * dist / gauss_mask_R)) # gradient gradient = gauss_mask[int(4 * gauss_mask_R / 5) + 1, gauss_mask_R] - gauss_mask[ int(4 * gauss_mask_R / 5), gauss_mask_R] gradient_x = gradient * slope_x gradient_y = gradient * slope_y / scale x_center = int((-x_min / x_width) * x_size) + gauss_mask_R y_center = int((-y_min / y_width) * y_size) + gauss_mask_R gradient_list.append([gradient_x, gradient_y]) # draw background confidence map gauss_X, gauss_Y = np.meshgrid(range(x_size + gauss_mask_R * 2), range(y_size + gauss_mask_R * 2)) map_background = ((gauss_X - x_center) * gradient_x + (y_center - gauss_Y) * gradient_y).T map_background_list.append(map_background.copy()) # automatically select a weight for the current dimension based on label imbalance condition neg_weight = neg_weight_imbalanced if np.sum( true_inds[:, dim]) / m_tr < imb_rate else neg_weight_balanced # draw self-adapted confidence map map_adapt = np.zeros(map_background.shape) for i, _ in enumerate(draw_x): # change from probalistic value into a coordinate on the map x_position = int( math.floor((draw_x[i] - x_min) / (x_width + eps) * x_size)) + gauss_mask_R y_position = int( math.floor((draw_y[i] - y_min) / (y_width + eps) * y_size)) + gauss_mask_R # add a positive confidence to the surrounding area of a positive sample if true_inds[i, dim]: map_adapt[x_position - gauss_mask_R:x_position + gauss_mask_R + 1, y_position - gauss_mask_R:y_position + gauss_mask_R + 1] += gauss_mask * meta_proba[i, dim] # add a negative confidence to the surrounding area of a negative sample else: map_adapt[x_position - gauss_mask_R:x_position + gauss_mask_R + 1, y_position - gauss_mask_R:y_position + gauss_mask_R + 1] -= gauss_mask * neg_weight * ( 1 - meta_proba[i, dim]) map_adapt_list.append(map_adapt.copy()) # the final decision pattern map_list.append(map_adapt + map_background) # store the key values for the affine transformation position_list.append( np.array([ x_min, x_width, y_min, y_width, gauss_mask_r, gauss_mask_R, x_size, y_size ])) self.non_zero_dim = non_zero_dim self.y_dim = y_dim self.y_total_dim = y_total_dim self.br_clf = br_clf self.base_clfs = base_clfs self.meta_clfs = meta_clfs self.map_list = map_list self.map_background_list = map_background_list self.map_adapt_list = map_adapt_list self.scale_list = scale_list self.position_list = position_list self.lr_min = lr_min self.gradient_list = gradient_list