def run_one_model_with_specific_evaluation_mod(train_data, test_data, mode: str = None): """ Runs the example with one model svc. :param train_data: train data for pipeline training :param test_data: test data for pipeline training :param mode: pass gpu flag to make gpu evaluation """ problem = 'classification' if mode == 'gpu': baseline_model = Fedot(problem=problem, preset='gpu') else: baseline_model = Fedot(problem=problem) svc_node_with_custom_params = PrimaryNode('svc') # the custom params are needed to make probability evaluation available # otherwise an error is occurred svc_node_with_custom_params.custom_params = dict(kernel='rbf', C=10, gamma=1, cache_size=2000, probability=True) preset_pipeline = Pipeline(svc_node_with_custom_params) start = datetime.now() baseline_model.fit(features=train_data, target='target', predefined_model=preset_pipeline) print(f'Completed with custom params in: {datetime.now() - start}') baseline_model.predict(features=test_data) print(baseline_model.get_metrics())
def run_ts_forecasting_example(with_plot=True, with_pipeline_vis=True, timeout=None): train_data_path = f'{fedot_project_root()}/examples/data/salaries.csv' target = pd.read_csv(train_data_path)['target'] # Define forecast length and define parameters - forecast length forecast_length = 30 task_parameters = TsForecastingParams(forecast_length=forecast_length) # init model for the time series forecasting model = Fedot(problem='ts_forecasting', task_params=task_parameters, timeout=timeout) # run AutoML model design in the same way pipeline = model.fit(features=train_data_path, target='target') if with_pipeline_vis: pipeline.show() # use model to obtain forecast forecast = model.predict(features=train_data_path) print( model.get_metrics(metric_names=['rmse', 'mae', 'mape'], target=target)) # plot forecasting result if with_plot: model.plot_prediction() return forecast
def run_multi_output_case(path, vis=False): """ Function launch case for river levels prediction on Lena river as multi-output regression task :param path: path to the file with table :param vis: is it needed to visualise pipeline and predictions """ target_columns = [ '1_day', '2_day', '3_day', '4_day', '5_day', '6_day', '7_day' ] data = InputData.from_csv(path, target_columns=target_columns, columns_to_drop=['date']) train, test = train_test_data_setup(data) problem = 'regression' automl_model = Fedot(problem=problem) automl_model.fit(features=train) predicted_array = automl_model.predict(features=test) # Convert output into one dimensional array forecast = np.ravel(predicted_array) mae_value = mean_absolute_error(np.ravel(test.target), forecast) print(f'MAE - {mae_value:.2f}') if vis: plot_predictions(predicted_array, test)
def test_pandas_input_for_api(): train_data, test_data, threshold = get_dataset('classification') train_features = pd.DataFrame(train_data.features) train_target = pd.Series(train_data.target) test_features = pd.DataFrame(test_data.features) test_target = pd.Series(test_data.target) # task selection, initialisation of the framework baseline_model = Fedot(problem='classification') # fit model without optimisation - single XGBoost node is used baseline_model.fit(features=train_features, target=train_target, predefined_model='xgboost') # evaluate the prediction with test data prediction = baseline_model.predict(features=test_features) assert len(prediction) == len(test_target) # evaluate quality metric for the test sample baseline_metrics = baseline_model.get_metrics(metric_names='f1', target=test_target) assert baseline_metrics['f1'] > 0
def test_api_predict_correct(task_type: str = 'classification'): train_data, test_data, _ = get_dataset(task_type) model = Fedot(problem=task_type, composer_params=composer_params) fedot_model = model.fit(features=train_data) prediction = model.predict(features=test_data) metric = model.get_metrics() assert isinstance(fedot_model, Pipeline) assert len(prediction) == len(test_data.target) assert metric['f1'] > 0
def run_classification_example(timeout=None): train_data_path = f'{fedot_project_root()}/cases/data/scoring/scoring_train.csv' test_data_path = f'{fedot_project_root()}/cases/data/scoring/scoring_test.csv' problem = 'classification' baseline_model = Fedot(problem=problem, timeout=timeout) baseline_model.fit(features=train_data_path, target='target', predefined_model='xgboost') baseline_model.predict(features=test_data_path) print(baseline_model.get_metrics()) auto_model = Fedot(problem=problem, seed=42, timeout=timeout) auto_model.fit(features=train_data_path, target='target') prediction = auto_model.predict_proba(features=test_data_path) print(auto_model.get_metrics()) return prediction
def run_regression_example(): data_path = f'{fedot_project_root()}/cases/data/cholesterol/cholesterol.csv' data = InputData.from_csv(data_path) train, test = train_test_data_setup(data) problem = 'regression' baseline_model = Fedot(problem=problem) baseline_model.fit(features=train, predefined_model='xgbreg') baseline_model.predict(features=test) print(baseline_model.get_metrics()) auto_model = Fedot(problem=problem, seed=42) auto_model.fit(features=train, target='target') prediction = auto_model.predict(features=test) print(auto_model.get_metrics()) return prediction
def test_multiobj_for_api(): train_data, test_data, _ = get_dataset('classification') composer_params['composer_metric'] = ['f1', 'node_num'] model = Fedot(problem='classification', composer_params=composer_params) model.fit(features=train_data) prediction = model.predict(features=test_data) metric = model.get_metrics() assert len(prediction) == len(test_data.target) assert metric['f1'] > 0 assert model.best_models is not None
def test_multi_target_regression_composing_correct(multi_target_data_setup): # Load simple dataset for multi-target train, test = multi_target_data_setup problem = 'regression' simple_composer_params = get_simple_composer_params() automl_model = Fedot(problem=problem, composer_params=simple_composer_params) automl_model.fit(features=train) predicted_array = automl_model.predict(features=test) assert predicted_array is not None
def test_api_forecast_correct(task_type: str = 'ts_forecasting'): # The forecast length must be equal to 12 forecast_length = 12 train_data, test_data, _ = get_dataset(task_type) model = Fedot(problem='ts_forecasting', composer_params=composer_params, task_params=TsForecastingParams(forecast_length=forecast_length)) model.fit(features=train_data) ts_forecast = model.predict(features=train_data) metric = model.get_metrics(target=test_data.target, metric_names='rmse') assert len(ts_forecast) == forecast_length assert metric['rmse'] >= 0
def run_pipeline_with_specific_evaluation_mode(train_data: InputData, test_data: InputData, mode: str = None): """ Runs the example with 3-node pipeline. :param train_data: train data for pipeline training :param test_data: test data for pipeline training :param mode: pass gpu flag to make gpu evaluation """ problem = 'classification' if mode == 'gpu': baseline_model = Fedot(problem=problem, preset='gpu') else: baseline_model = Fedot(problem=problem) svc_node_with_custom_params = PrimaryNode('svc') svc_node_with_custom_params.custom_params = dict(kernel='rbf', C=10, gamma=1, cache_size=2000, probability=True) logit_node = PrimaryNode('logit') rf_node = SecondaryNode( 'rf', nodes_from=[svc_node_with_custom_params, logit_node]) preset_pipeline = Pipeline(rf_node) start = datetime.now() baseline_model.fit(features=train_data, target='target', predefined_model=preset_pipeline) print(f'Completed with custom params in: {datetime.now() - start}') baseline_model.predict(features=test_data) print(baseline_model.get_metrics())
def test_api_forecast_numpy_input_with_static_model_correct(task_type: str = 'ts_forecasting'): forecast_length = 10 train_data, test_data, _ = get_dataset(task_type) model = Fedot(problem='ts_forecasting', task_params=TsForecastingParams(forecast_length=forecast_length)) # Define chain for prediction node_lagged = PrimaryNode('lagged') chain = Chain(SecondaryNode('linear', nodes_from=[node_lagged])) model.fit(features=train_data.features, target=train_data.target, predefined_model=chain) ts_forecast = model.predict(features=train_data) metric = model.get_metrics(target=test_data.target, metric_names='rmse') assert len(ts_forecast) == forecast_length assert metric['rmse'] >= 0
def make_forecast(df, len_forecast: int, time_series_label: str): """ Function for making time series forecasting with Prophet library :param df: dataframe to process :param len_forecast: forecast length :param time_series_label: name of time series to process :return predicted_values: forecast :return model_name: name of the model (always 'AutoTS') """ # Define parameters task = Task(TaskTypesEnum.ts_forecasting, TsForecastingParams(forecast_length=len_forecast)) # Init model for the time series forecasting model = Fedot(problem='ts_forecasting', task_params=task.task_params, composer_params={ 'timeout': 1, 'preset': 'ultra_light_tun' }, preset='ultra_light_tun') input_data = InputData(idx=np.arange(0, len(df)), features=np.array(df[time_series_label]), target=np.array(df[time_series_label]), task=task, data_type=DataTypesEnum.ts) start_forecast = len(df) end_forecast = start_forecast + len_forecast predict_input = InputData(idx=np.arange(start_forecast, end_forecast), features=np.array(df[time_series_label]), target=np.array(df[time_series_label]), task=task, data_type=DataTypesEnum.ts) # Run AutoML model design in the same way pipeline = model.fit(features=input_data) predicted_values = model.predict(predict_input) model_name = 'FEDOT' return predicted_values, model_name
def run_credit_scoring_problem(train_file_path, test_file_path, timeout: float = 5.0, is_visualise=False, with_tuning=False, cache_path=None): preset = 'light_tun' if with_tuning else 'light' automl = Fedot(problem='classification', timeout=timeout, verbose_level=4, preset=preset) automl.fit(train_file_path) predict = automl.predict(test_file_path) metrics = automl.get_metrics() if is_visualise: automl.current_pipeline.show() print(f'Composed ROC AUC is {round(metrics["roc_auc"], 3)}') return metrics["roc_auc"]
def test_cv_api_correct(): composer_params = { 'max_depth': 1, 'max_arity': 2, 'timeout': 0.0001, 'preset': 'ultra_light', 'cv_folds': 10 } task = Task(task_type=TaskTypesEnum.classification) dataset_to_compose, dataset_to_validate = get_data(task) model = Fedot(problem='classification', composer_params=composer_params, verbose_level=2) fedot_model = model.fit(features=dataset_to_compose) prediction = model.predict(features=dataset_to_validate) metric = model.get_metrics() assert isinstance(fedot_model, Pipeline) assert len(prediction) == len(dataset_to_validate.target) assert metric['f1'] > 0