Skip to content

Commit

Permalink
move seed setting out of the methods
Browse files Browse the repository at this point in the history
  • Loading branch information
IIaKyJIuH committed Jul 12, 2023
1 parent bdbdbb3 commit 2e845ae
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cases/credit_scoring/credit_scoring_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from fedot.core.data.data import InputData
from fedot.core.pipelines.pipeline import Pipeline
from fedot.core.utils import fedot_project_root
from fedot.core.utils import set_random_seed


def calculate_validation_metric(pipeline: Pipeline, dataset_to_validate: InputData) -> float:
Expand All @@ -27,7 +28,6 @@ def run_credit_scoring_problem(train_file_path, test_file_path,
**composer_args):
automl = Fedot(problem='classification',
timeout=timeout,
seed=42,
preset=BEST_QUALITY_PRESET_NAME,
logging_level=logging.DEBUG,
**composer_args)
Expand Down Expand Up @@ -63,6 +63,8 @@ def get_scoring_data():


if __name__ == '__main__':
set_random_seed(42)

full_path_train, full_path_test = get_scoring_data()
run_credit_scoring_problem(full_path_train,
full_path_test,
Expand Down
10 changes: 6 additions & 4 deletions examples/advanced/additional_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from fedot.core.pipelines.pipeline import Pipeline
from fedot.core.pipelines.pipeline_builder import PipelineBuilder
from fedot.core.utils import fedot_project_root
from fedot.core.utils import set_random_seed


def run_additional_learning_example():
seed = 42
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'

Expand All @@ -23,7 +23,7 @@ def run_additional_learning_example():

problem = 'classification'

auto_model = Fedot(problem=problem, seed=seed, timeout=5, preset='best_quality',
auto_model = Fedot(problem=problem, timeout=5, preset='best_quality',
initial_assumption=PipelineBuilder().add_node('scaling').add_node('logit').build())

auto_model.fit(features=deepcopy(train_data.head(1000)), target='target')
Expand All @@ -41,15 +41,15 @@ def run_additional_learning_example():
train_data = train_data.head(5000)
timeout = 1

auto_model_from_atomized = Fedot(problem=problem, seed=seed, preset='best_quality', timeout=timeout,
auto_model_from_atomized = Fedot(problem=problem, preset='best_quality', timeout=timeout,
logging_level=logging.INFO,
initial_assumption=atomized_model)
auto_model_from_atomized.fit(features=deepcopy(train_data), target='target')
auto_model_from_atomized.predict_proba(features=deepcopy(test_data))
auto_model_from_atomized.current_pipeline.show()
print('auto_model_from_atomized', auto_model_from_atomized.get_metrics(deepcopy(test_data_target)))

auto_model_from_pipeline = Fedot(problem=problem, seed=seed, preset='best_quality', timeout=timeout,
auto_model_from_pipeline = Fedot(problem=problem, preset='best_quality', timeout=timeout,
logging_level=logging.INFO,
initial_assumption=non_atomized_model)
auto_model_from_pipeline.fit(features=deepcopy(train_data), target='target')
Expand All @@ -59,4 +59,6 @@ def run_additional_learning_example():


if __name__ == '__main__':
set_random_seed(42)

run_additional_learning_example()
6 changes: 4 additions & 2 deletions examples/advanced/multimodal_text_num_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from fedot.core.data.data_split import train_test_data_setup
from fedot.core.data.multi_modal import MultiModalData
from fedot.core.utils import fedot_project_root
from fedot.core.utils import set_random_seed


def run_multi_modal_example(file_path: str, visualization: bool = False, with_tuning: bool = True,
Expand All @@ -23,12 +24,11 @@ def run_multi_modal_example(file_path: str, visualization: bool = False, with_tu
F1 metrics of the model.
"""
task = 'classification'
seed = 42
path = fedot_project_root().joinpath(file_path)
data = MultiModalData.from_csv(file_path=path, task=task, target_columns='variety', index_col=None)
fit_data, predict_data = train_test_data_setup(data, shuffle_flag=True, split_ratio=0.7)

automl_model = Fedot(problem=task, timeout=timeout, seed=seed, with_tuning=with_tuning, n_jobs=1)
automl_model = Fedot(problem=task, timeout=timeout, with_tuning=with_tuning, n_jobs=1)
automl_model.fit(features=fit_data,
target=fit_data.target)

Expand All @@ -44,4 +44,6 @@ def run_multi_modal_example(file_path: str, visualization: bool = False, with_tu


if __name__ == '__main__':
set_random_seed(42)

run_multi_modal_example(file_path='examples/data/multimodal_wine.csv', visualization=True)
5 changes: 4 additions & 1 deletion examples/advanced/multiobj_optimisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from fedot.api.main import Fedot
from fedot.core.utils import fedot_project_root
from fedot.core.utils import set_random_seed


def run_classification_multiobj_example(visualization=False, timeout=1, with_tuning=True):
Expand All @@ -12,7 +13,7 @@ def run_classification_multiobj_example(visualization=False, timeout=1, with_tun
problem = 'classification'

metric_names = ['f1', 'node_number']
auto_model = Fedot(problem=problem, timeout=timeout, preset='best_quality', seed=42,
auto_model = Fedot(problem=problem, timeout=timeout, preset='best_quality',
metric=metric_names,
with_tuning=with_tuning)
auto_model.fit(features=train_data, target='class')
Expand All @@ -27,4 +28,6 @@ def run_classification_multiobj_example(visualization=False, timeout=1, with_tun


if __name__ == '__main__':
set_random_seed(42)

run_classification_multiobj_example(visualization=True)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from fedot.api.main import Fedot
from fedot.core.data.multi_modal import MultiModalData
from fedot.core.utils import fedot_project_root
from fedot.core.utils import set_random_seed
from fedot.remote.infrastructure.clients.test_client import TestClient
from fedot.remote.remote_evaluator import RemoteEvaluator, RemoteTaskParams

Expand Down Expand Up @@ -60,14 +61,16 @@ def run_automl(data: MultiModalData, features_to_use,
'cv_folds': None,
'validation_blocks': None}

automl = Fedot(problem='ts_forecasting', seed=1, timeout=timeout, **composer_params)
automl = Fedot(problem='ts_forecasting', timeout=timeout, **composer_params)

obtained_pipeline = automl.fit(data)
forecast = automl.forecast(data)
return forecast, obtained_pipeline


if __name__ == '__main__':
set_random_seed(1)

features_to_use = ['wind_speed', 'sea_height']

data = MultiModalData.from_csv_time_series(
Expand Down
8 changes: 5 additions & 3 deletions examples/simple/classification/api_classification.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from fedot.api.main import Fedot
from fedot.core.utils import fedot_project_root
from fedot.core.utils import set_random_seed


def run_classification_example(timeout: float = None, visualization=False, with_tuning=True):
seed = 42
problem = 'classification'
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'

baseline_model = Fedot(problem=problem, timeout=timeout, seed=seed)
baseline_model = Fedot(problem=problem, timeout=timeout)
baseline_model.fit(features=train_data_path, target='target', predefined_model='rf')

baseline_model.predict(features=test_data_path)
print(baseline_model.get_metrics())

auto_model = Fedot(problem=problem, seed=seed, timeout=timeout, n_jobs=-1, preset='best_quality',
auto_model = Fedot(problem=problem, timeout=timeout, n_jobs=-1, preset='best_quality',
max_pipeline_fit_time=5, metric='roc_auc', with_tuning=with_tuning)
auto_model.fit(features=train_data_path, target='target')
prediction = auto_model.predict_proba(features=test_data_path)
Expand All @@ -26,4 +26,6 @@ def run_classification_example(timeout: float = None, visualization=False, with_


if __name__ == '__main__':
set_random_seed(42)

run_classification_example(timeout=10.0, visualization=True)
3 changes: 3 additions & 0 deletions test/unit/common_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import numpy as np

from fedot.core.data.data import InputData, OutputData
from fedot.core.utils import set_random_seed


def is_predict_ignores_target(predict_func, input_data, data_arg_name, predict_args=None):
set_random_seed(0)

if predict_args is None:
predict_args = {}

Expand Down

0 comments on commit 2e845ae

Please sign in to comment.