Unverified Commit 0ca74853 authored by Julia Borisova's avatar Julia Borisova Committed by GitHub
Browse files

Multi_ts API example adding, available_operations from API bug fixed (#728)

single_drop mutation fixed (for empty nodes), multi_ts example added, test on available_operations
parent f021908d
master 0.7.3 1113-example-for-sa 1136-upgrade-input-data-class 1157-fix-2 1158-fix 922-preproc-acceleration Add-retries-to-gitlab-mirror-action DRMPN-better-caching DRMPN-fix-get-metrics HEAD ability_chosen_cats add-arg-to-pipe add-iopt add-mase-metric add-mutation-agent add-preprocessing-control add-tuning-history add_direct_examples add_locf_to_assumption add_slices_to_input_data add_topo2_speedup ag_exp_ensembles ai-gen-docs amlb-fixes api_parameters_unify atomized-model-operation auto-forecasting-models backup-branch bench-doc-0402 branch-for-hack cloud-fix composite_bn crop_to_data_range_transf custom-automl-example dim-err-ex docstring-rebased docstrings easy-fitness-eval ensemble-operations exp_comp_or_tuner exps extract-fit-initiall-assumption fedot_for_bn_new fi_exp_prep fi_exp_prep_old fix-gitlab-mirroring fix_issue_1296 fix_ts_wrong_links generated-docs golem-random-opt graph_factory_for_nas has_one_root_fix hotfix-signature-function hotfix-target_encoding huawei huawei_new_paper improve_clstm input_data_unit_tests iopt-experiment lgbm_impl logging-fixes-for-golem-main move-topo-in-extra multimodal_benchmarking multimodal_text_detection multimodal_text_detection_new new-ensemble-operations new-optimizer-for-ts-forecasting new_ts_metrics operation-repo-refactor optimizer-api-stab pd_np_to_inputdata pipeline_import_export_hotfix preprox-researx proto-strategy-opt release release-071 release-073 release-075 remote remote-fixes repr-fix runfix surr-golem surrogate-example temp-DRMPN-caching-class-variable temp-branch-DRMPN-better-cachingytho test_docu_bot ts-opt-analytics ts-opt-more-changes xgb_impl v0.7.5 v0.7.4 v0.7.3.2 v0.7.3.1 v0.7.3 v0.7.2 v0.7.1 v0.7.0 v0.6.2 v0.6.1 v0.6.0
No related merge requests found
Showing with 123 additions and 56 deletions
+123 -56
import os
import numpy as np
from examples.simple.time_series_forecasting.ts_pipelines import ts_complex_ridge_smoothing_pipeline
from fedot.api.main import Fedot
from fedot.core.data.data import InputData
from fedot.core.data.data_split import train_test_data_setup
from fedot.core.pipelines.pipeline_builder import PipelineBuilder
from fedot.core.repository.tasks import TsForecastingParams, Task, TaskTypesEnum
from fedot.core.utils import fedot_project_root
from matplotlib import pyplot as plt
from sklearn.metrics import mean_absolute_error, mean_squared_error, mean_absolute_percentage_error
def prepare_data(forecast_length, is_multi_ts):
"""
Function to form InputData from file with time-series
"""
columns_to_use = ['61_91', '56_86', '61_86', '66_86']
target_column = '61_91'
task = Task(TaskTypesEnum.ts_forecasting,
TsForecastingParams(forecast_length=forecast_length))
file_path = os.path.join(str(fedot_project_root()), 'cases/data/arctic/topaz_multi_ts.csv')
if is_multi_ts:
data = InputData.from_csv_multi_time_series(
file_path=file_path,
task=task,
columns_to_use=columns_to_use)
else:
data = InputData.from_csv_time_series(
file_path=file_path,
task=task,
target_column=target_column)
train_data, test_data = train_test_data_setup(data)
return train_data, test_data, task
def visualize_result(train, test, target, forecast, is_multi_ts):
if is_multi_ts:
history = np.ravel(train.target[:, 0])
else:
history = np.ravel(train.target)
plt.plot(np.ravel(test.idx), target, label='test')
plt.plot(np.ravel(train.idx), history, label='history')
plt.plot(np.ravel(test.idx), forecast, label='prediction_after_tuning')
plt.xlabel('Time step')
plt.ylabel('Sea level')
plt.legend()
plt.show()
def run_multi_ts_forecast(forecast_length, is_multi_ts):
"""
Function for run experiment with use multi_ts data type (is_multi_ts=True) for train set extension
Or run experiment on one time-series (is_multi_ts=False)
"""
train_data, test_data, task = prepare_data(forecast_length, is_multi_ts)
# init model for the time series forecasting
init_pipeline = ts_complex_ridge_smoothing_pipeline()
model = Fedot(problem='ts_forecasting',
task_params=task.task_params,
timeout=5,
n_jobs=1,
composer_params={
'max_depth': 5,
'num_of_generations': 20,
'pop_size': 15,
'max_arity': 4,
'cv_folds': None,
'validation_blocks': None,
'initial_assumption': init_pipeline,
'available_operations': ['lagged', 'smoothing', 'diff_filter', 'gaussian_filter',
'ridge', 'lasso', 'linear', 'cut']
})
# fit model
pipeline = model.fit(train_data)
pipeline.show()
# use model to obtain forecast
forecast = model.predict(test_data)
target = np.ravel(test_data.target)
# visualize results
visualize_result(train_data, test_data, target, forecast, is_multi_ts)
print(f'MAE: {mean_absolute_error(target, forecast)}')
print(f'RMSE: {mean_squared_error(target, forecast)}')
print(f'MAPE: {mean_absolute_percentage_error(target, forecast)}')
print(model.get_metrics(metric_names=['rmse', 'mae', 'mape'], target=target))
if __name__ == '__main__':
forecast_length = 60
run_multi_ts_forecast(forecast_length, is_multi_ts=True)
run_multi_ts_forecast(forecast_length, is_multi_ts=False)
import datetime
import os
from copy import deepcopy
import numpy as np
from matplotlib import pyplot as plt
from sklearn.metrics import mean_squared_error, mean_absolute_error
from cases.multi_ts_level_forecasting import prepare_data
from examples.simple.time_series_forecasting.ts_pipelines import *
from fedot.core.composer.composer_builder import ComposerBuilder
from fedot.core.composer.gp_composer.gp_composer import PipelineComposerRequirements
from fedot.core.composer.gp_composer.specific_operators import parameter_change_mutation
from fedot.core.data.data import InputData
from fedot.core.data.data_split import train_test_data_setup
from fedot.core.optimisers.gp_comp.gp_optimiser import GPGraphOptimiserParameters
from fedot.core.optimisers.gp_comp.operators.mutation import MutationTypesEnum
from fedot.core.pipelines.node import PrimaryNode, SecondaryNode
from fedot.core.pipelines.pipeline import Pipeline
from fedot.core.repository.quality_metrics_repository import \
MetricsRepository, RegressionMetricsEnum
from fedot.core.repository.tasks import Task, TaskTypesEnum, TsForecastingParams
from fedot.core.utils import fedot_project_root
def calculate_metrics(target, predicted):
......@@ -27,28 +21,6 @@ def calculate_metrics(target, predicted):
return rmse, mae
def initial_pipeline():
"""
Return pipeline with the following structure:
lagged - ridge \
-> ridge -> final forecast
lagged - ridge /
"""
node_lagged_1 = PrimaryNode("lagged")
node_lagged_1.custom_params = {'window_size': 50}
node_smoth = PrimaryNode("smoothing")
node_lagged_2 = SecondaryNode("lagged", nodes_from=[node_smoth])
node_lagged_2.custom_params = {'window_size': 30}
node_ridge = SecondaryNode("ridge", nodes_from=[node_lagged_1])
node_lasso = SecondaryNode("lasso", nodes_from=[node_lagged_2])
node_final = SecondaryNode("ridge", nodes_from=[node_ridge, node_lasso])
pipeline = Pipeline(node_final)
return pipeline
def get_available_operations():
""" Function returns available operations for primary and secondary nodes """
primary_operations = ['lagged', 'smoothing', 'diff_filter', 'gaussian_filter']
......@@ -81,31 +53,11 @@ def compose_pipeline(pipeline, train_data, task):
return obtained_pipeline
def prepare_data(forecast_length, multi_ts):
columns_to_use = ['61_91', '56_86', '61_86', '66_86']
target_column = '61_91'
task = Task(TaskTypesEnum.ts_forecasting,
TsForecastingParams(forecast_length=forecast_length))
file_path = os.path.join(str(fedot_project_root()), 'cases/data/arctic/topaz_multi_ts.csv')
if multi_ts:
data = InputData.from_csv_multi_time_series(
file_path=file_path,
task=task,
columns_to_use=columns_to_use)
else:
data = InputData.from_csv_time_series(
file_path=file_path,
task=task,
target_column=target_column)
train_data, test_data = train_test_data_setup(data)
return train_data, test_data, task
def run_multiple_ts_forecasting(forecast_length, multi_ts):
def run_multiple_ts_forecasting(forecast_length, is_multi_ts):
# separate data on test/train
train_data, test_data, task = prepare_data(forecast_length, multi_ts=multi_ts)
train_data, test_data, task = prepare_data(forecast_length, is_multi_ts=is_multi_ts)
# pipeline initialization
pipeline = initial_pipeline()
pipeline = ts_complex_ridge_smoothing_pipeline()
# pipeline fit and predict
pipeline.fit(train_data)
prediction_before = np.ravel(np.array(pipeline.predict(test_data).predict))
......@@ -135,7 +87,7 @@ def run_multiple_ts_forecasting(forecast_length, multi_ts):
rmse_tuning, mae_tuning = calculate_metrics(np.ravel(test_data.target), predict_after_tuning)
# visualization of results
if multi_ts:
if is_multi_ts:
history = np.ravel(train_data.target[:, 0])
else:
history = np.ravel(train_data.target)
......@@ -158,4 +110,4 @@ def run_multiple_ts_forecasting(forecast_length, multi_ts):
if __name__ == '__main__':
run_multiple_ts_forecasting(forecast_length=60, multi_ts=True)
run_multiple_ts_forecasting(forecast_length=60, is_multi_ts=True)
......@@ -119,7 +119,7 @@ def ts_complex_ridge_smoothing_pipeline():
Where smoothing - rolling mean
"""
pip_builder = PipelineBuilder() \
.add_sequence('smoothing', 'lagged', 'rigde', branch_idx=0) \
.add_sequence('smoothing', 'lagged', 'ridge', branch_idx=0) \
.add_sequence('lagged', 'ridge', branch_idx=1).join_branches('ridge')
pipeline = pip_builder.to_pipeline()
......
......@@ -104,9 +104,12 @@ def _apply_mutation(new_graph: Any, mutation_prob: float, mutation_type: Union[M
mutation_func = mutation_type
else:
mutation_func = mutation_by_type[mutation_type]
graph_copy = deepcopy(new_graph)
new_graph = mutation_func(new_graph, requirements=requirements,
params=params,
max_depth=max_depth)
if not new_graph.nodes:
return graph_copy
elif mutation_type is not MutationTypesEnum.none:
raise ValueError(f'Required mutation type is not found: {mutation_type}')
return new_graph
......
......@@ -11,7 +11,7 @@ from fedot.core.data.data_split import train_test_data_setup
from fedot.core.log import default_log
from fedot.core.pipelines.node import PrimaryNode, SecondaryNode
from fedot.core.pipelines.pipeline import Pipeline
from fedot.core.repository.tasks import Task, TaskTypesEnum
from fedot.core.repository.tasks import Task, TaskTypesEnum, TsForecastingParams
from fedot.preprocessing.preprocessing import DataPreprocessor
from ..api.test_main_api import get_dataset
from ..tasks.test_classification import get_binary_classification_data
......@@ -143,3 +143,19 @@ def test_api_composer_divide_operations():
assert primary == available_operations
assert secondary == available_operations
def test_api_composer_available_operations():
""" Checks if available_operations goes through all fitting process"""
task = Task(TaskTypesEnum.ts_forecasting,
TsForecastingParams(forecast_length=1))
train_data, _, _ = get_dataset(task_type='ts_forecasting')
available_operations = ['lagged']
model = Fedot(problem='ts_forecasting',
task_params=task.task_params,
timeout=0.01,
composer_params={'available_operations': available_operations,
'pop_size': 500}
)
model.fit(train_data)
assert model.params.api_params['available_operations'] == available_operations
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment