def evaluate_sys(run_id, task_type, mth, dataset, ens_method, enable_meta, eval_type='holdout', time_limit=1200, seed=1, tree_id=0): _task_type = MULTICLASS_CLS if task_type == 'cls' else REGRESSION train_data, test_data = load_train_test_data(dataset, task_type=_task_type) _enable_meta = True if enable_meta == 'true' else False if task_type == 'cls': from mindware.estimators import Classifier estimator = Classifier(time_limit=time_limit, per_run_time_limit=30, output_dir=save_folder, ensemble_method=ens_method, enable_meta_algorithm_selection=_enable_meta, evaluation=eval_type, metric='bal_acc', include_algorithms=['extra_trees', 'random_forest', 'adaboost', 'gradient_boosting', 'k_nearest_neighbors', 'liblinear_svc', 'libsvm_svc', 'lightgbm', 'logistic_regression', 'random_forest'], n_jobs=1) else: from mindware.estimators import Regressor estimator = Regressor(time_limit=time_limit, per_run_time_limit=90, output_dir=save_folder, ensemble_method=ens_method, enable_meta_algorithm_selection=_enable_meta, evaluation=eval_type, metric='mse', # include_preprocessors=['percentile_selector_regression'], # include_algorithms=['random_forest'], n_jobs=1) start_time = time.time() estimator.fit(train_data, opt_strategy=mth, dataset_id=dataset, tree_id=tree_id) pred = estimator.predict(test_data) if task_type == 'cls': test_score = balanced_accuracy_score(test_data.data[1], pred) else: test_score = mean_squared_error(test_data.data[1], pred) validation_score = estimator._ml_engine.solver.incumbent_perf # eval_dict = estimator._ml_engine.solver.get_eval_dict() print('Run ID : %d' % run_id) print('Dataset : %s' % dataset) print('Val/Test score : %f - %f' % (validation_score, test_score)) save_path = save_folder + '%s_%s_%s_%s_%d_%d_%d_%d.pkl' % ( task_type, mth, dataset, enable_meta, time_limit, (ens_method is None), tree_id, run_id) with open(save_path, 'wb') as f: pickle.dump([dataset, validation_score, test_score, start_time], f) # Delete output dir shutil.rmtree(os.path.join(estimator.get_output_dir()))
def evaluate_hmab(algorithms, run_id, time_limit=600, dataset='credit', eval_type='holdout', enable_ens=True, seed=1): print('%s\nDataset: %s, Run_id: %d, Budget: %d.\n%s' % ('=' * 50, dataset, run_id, time_limit, '=' * 50)) task_id = '[%s][%s-%d-%d]' % (hmab_id, dataset, len(algorithms), time_limit) _start_time = time.time() train_data, test_data = load_train_test_data(dataset, task_type=MULTICLASS_CLS) if enable_ens is True: ensemble_method = 'ensemble_selection' else: ensemble_method = None clf = Classifier(time_limit=time_limit, per_run_time_limit=per_run_time_limit, include_algorithms=algorithms, amount_of_resource=None, output_dir=save_dir, ensemble_method=ensemble_method, evaluation=eval_type, metric='bal_acc', n_jobs=1) # clf.fit(train_data, meta_datasets=holdout_datasets) # clf.fit(train_data, opt_strategy='combined') clf.fit(train_data) clf.refit() pred = clf.predict(test_data) test_score = balanced_accuracy_score(test_data.data[1], pred) timestamps, perfs = clf.get_val_stats() validation_score = np.max(perfs) print('Dataset : %s' % dataset) print('Validation/Test score : %f - %f' % (validation_score, test_score)) save_path = save_dir + '%s-%d.pkl' % (task_id, run_id) with open(save_path, 'wb') as f: stats = [timestamps, perfs] pickle.dump([validation_score, test_score, stats], f)
def test_cls(): save_dir = './data/eval_exps/soln-ml' if not os.path.exists(save_dir): os.makedirs(save_dir) time_limit = 60 print('==> Start to evaluate with Budget %d' % time_limit) ensemble_method = 'ensemble_selection' eval_type = 'holdout' iris = load_iris() X, y = iris.data, iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1, stratify=y) dm = DataManager(X_train, y_train) train_data = dm.get_data_node(X_train, y_train) test_data = dm.get_data_node(X_test, y_test) clf = Classifier(time_limit=time_limit, output_dir=save_dir, ensemble_method=ensemble_method, enable_meta_algorithm_selection=False, ensemble_size=10, evaluation=eval_type, metric='acc', n_jobs=2) clf.fit(train_data, tree_id=2) print(clf.summary()) pred = clf.predict(test_data) print(accuracy_score(test_data.data[1], pred)) shutil.rmtree(save_dir)
n_jobs = args.n_jobs ensemble_method = args.ens_method if ensemble_method == 'none': ensemble_method = None save_dir = './data/eval_exps/soln-ml' if not os.path.exists(save_dir): os.makedirs(save_dir) print('==> Start to evaluate with Budget %d' % time_limit) iris = load_iris() X, y = iris.data, iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1) dm = DataManager(X_train, y_train) train_data = dm.get_data_node(X_train, y_train) test_data = dm.get_data_node(X_test, y_test) clf = Classifier(time_limit=time_limit, output_dir=save_dir, ensemble_method=ensemble_method, evaluation=eval_type, metric='acc', n_jobs=n_jobs) clf.fit(train_data) pred = clf.predict(test_data) print(balanced_accuracy_score(test_data.data[1], pred))
import numpy as np import os import sys sys.path.append(os.getcwd()) from mindware.components.feature_engineering.transformations.preprocessor.text2vector import \ Text2VectorTransformation from mindware.components.feature_engineering.transformation_graph import DataNode from mindware.components.utils.constants import * from mindware.estimators import Classifier x = np.array([[1, 'I am good', 'I am right', 3], [2, 'He is good', 'He is ok', 4], [2.5, 'Everyone is good', 'Everyone is ok', 7], [1.3333, 'well', 'what', 5]]) y = np.array([0, 1, 0, 1]) t2v = Text2VectorTransformation() data = (x, y) feature_type = [NUMERICAL, TEXT, TEXT, DISCRETE] datanode = DataNode(data, feature_type) clf = Classifier(time_limit=20, enable_meta_algorithm_selection=False, include_algorithms=['random_forest']) clf.fit(datanode, opt_strategy='combined') print(clf.predict(datanode))