def run(output_dir, feature_num, mode): feature_num = int(feature_num) taxo_res = TaxonomyResources(freq_fpaths, isa_fpaths) taxo_features = TaxonomyFeatures(taxo_res, relations_fpath=train_relations_fpath) ensure_dir(output_dir) features = ["hyper_in_hypo_i","hypo2hyper_substract", "freq_substract", "in_weight_substract", "length_substract", "hypo2hyper_s_substract","hypo2hyper_max2_substract"] features = features[:feature_num] if mode == "gridsearch": # grid search is only supported for SVC method = "SVC" hc = SuperTaxi(join(output_dir, "SVC-grid-search"), method="SVC", features=features, overwrite=True) clf = hc.grid_search_svc(taxo_features.relations, test=TEST_GRIDSEARCH) return for method in METHODS: try: classifier_dir = join(output_dir, method) print("\n", method.upper(), "\n", "="*50) hc = SuperTaxi(classifier_dir, method=method, features=features, overwrite=True) if mode == "train": clf = hc.train(taxo_features.relations) hc._print_clf_info() elif mode == "cv": hc.crossval(taxo_features.relations) else: print("Error: unrecognised mode %s" % mode) except: print(format_exc())
def predict_by_classifier(self, classifier_dir): st = SuperTaxi(classifier_dir) self._relations = st.predict(self._relations) return st.meta["method"]