def evaluate(): 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) try: 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(dataset_name='iris', time_limit=150, output_dir=save_dir, ensemble_method=ensemble_method, evaluation=eval_type, metric='acc') clf.fit(train_data) clf.refit() pred = clf.predict(test_data) print('final score', clf.score(test_data)) except Exception as e: return False return True
def test_cls_without_ensemble(): 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 = None eval_type = 'cv' 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, include_algorithms=['random_forest'], evaluation=eval_type, metric='acc') clf.fit(train_data) print(clf.summary()) clf.refit() pred = clf.predict(test_data) print(accuracy_score(test_data.data[1], pred)) shutil.rmtree(save_dir)
def main(): time_limit = 60 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, 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) save_dir = './data/eval_exps/soln-ml' if not os.path.exists(save_dir): os.makedirs(save_dir) add_classifier(UserDefinedDecisionTree) clf = Classifier(time_limit=time_limit, output_dir=save_dir, enable_meta_algorithm_selection=False, include_algorithms=['UserDefinedDecisionTree'], ensemble_method=None, metric='acc') _start_time = time.time() clf.fit(train_data) print(clf.summary()) pred = clf.predict(test_data) print(accuracy_score(test_data.data[1], pred)) shutil.rmtree(save_dir)
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 main(): tmp_dir = './data/eval_exps/soln-ml' if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) time_limit = 60 print('==> Start new AutoML task 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=tmp_dir, ensemble_method=ensemble_method, enable_meta_algorithm_selection=False, ensemble_size=10, optimizer='random_search', evaluation=eval_type, metric='acc', n_jobs=1) 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(tmp_dir)
def evaluate_package(): train_data, test_data = load_train_test_data('pc4', data_dir='./') Classifier().fit(train_data)
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))
return cs 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) save_dir = './data/eval_exps/soln-ml' if not os.path.exists(save_dir): os.makedirs(save_dir) add_classifier(UserDefinedDecisionTree) clf = Classifier(time_limit=time_limit, output_dir=save_dir, include_algorithms=['UserDefinedDecisionTree'], random_state=1, metric='acc', n_jobs=1) _start_time = time.time() _iter_id = 0 clf.fit(train_data) pred = clf.predict(test_data) print(balanced_accuracy_score(test_data.data[1], pred))
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', enable_meta_algorithm_selection=False, n_jobs=n_jobs) clf.initialize(train_data, tree_id=0) if role == 'master': # bind the IP, port, etc. master = Master(clf, ip=args.master_ip, port=args.port) master.run() print(master.predict()) else: # Set up evaluation workers. worker = EvaluationWorker(clf, args.master_ip, args.port, worker_port=args.worker_port) worker.run()
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))