Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ai gen files #1301

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/examples/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ In this section you can find notebooks and useful pipeline structures for variou
.. toctree::
:glob:
:maxdepth: 1
:caption: Contents

classification_example
regression_example
Expand All @@ -17,3 +16,4 @@ In this section you can find notebooks and useful pipeline structures for variou
classification_pipelines
regression_pipelines
ts_pipelines
simple/index
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

.. _fedot_classification_example:

====================================================================
Classification Example with FEDOT Framework
====================================================================

This example demonstrates how to use the FEDOT (Framework for Evidential Data Transformation) to perform a classification task. The example uses a predefined dataset for training and testing, and it showcases the setup and execution of a classification pipeline with hyperparameter tuning and evaluation metrics.

Overview
--------

The FEDOT framework is designed to automate the process of building and optimizing machine learning pipelines. This example specifically focuses on a classification problem, where the goal is to predict a categorical target variable based on input features.

The example is structured into several logical blocks:

1. **Initialization and Configuration**: Setting up the FEDOT instance with the desired problem type, configuration options, and evaluation metrics.
2. **Data Loading**: Specifying the paths to the training and testing datasets.
3. **Model Training**: Fitting the FEDOT pipeline to the training data.
4. **Prediction**: Generating probability predictions on the test data.
5. **Visualization**: Plotting the prediction results.

Step-by-Step Guide
------------------

Below is a detailed breakdown of the code, ensuring that each line is explained and can be easily understood and replicated.

Initialization and Configuration
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: python

from fedot import FedotBuilder
from fedot.core.utils import fedot_project_root

if __name__ == '__main__':
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'

fedot = (FedotBuilder(problem='classification')
.setup_composition(timeout=10, with_tuning=True, preset='best_quality')
.setup_pipeline_evaluation(max_pipeline_fit_time=5, metric=['roc_auc', 'precision'])
.build())

In this block, the FEDOT framework is imported, and the paths to the training and testing datasets are defined. The FEDOT instance is then configured for a classification problem, with a timeout for the composition process, hyperparameter tuning enabled, and a preset for the best quality. The evaluation metrics are set to `roc_auc` and `precision`.

Data Loading
^^^^^^^^^^^^

The data loading is implicit in the paths defined in the initialization block. The paths are constructed using the `fedot_project_root()` function, which returns the root directory of the FEDOT project, and then the paths to the specific CSV files are appended.

Model Training
^^^^^^^^^^^^^^^^^^^

.. code-block:: python

fedot.fit(features=train_data_path, target='target')

This line fits the FEDOT pipeline to the training data. The `features` parameter is set to the path of the training dataset, and the `target` parameter specifies the name of the target column in the dataset.

Prediction
^^^^^^^^^^

.. code-block:: python

fedot.predict_proba(features=test_data_path)

Here, the FEDOT pipeline is used to generate probability predictions for the test dataset. The `features` parameter is set to the path of the test dataset.

Visualization
^^^^^^^^^^^^^^^^^^

.. code-block:: python

fedot.plot_prediction()

The final line of the example plots the prediction results, providing a visual representation of the model's performance.

Conclusion
----------

This example provides a comprehensive guide on how to use the FEDOT framework for a classification task. By following this guide, users can replicate the example and adapt it to their own datasets and classification problems.

.. note::
Ensure that the required datasets are available at the specified paths and that the FEDOT framework is properly installed and configured.

.. seealso::
For more information on the FEDOT framework, visit the `official documentation <https://github.com/nccr-itmo/FEDOT>`_.

This documentation page is formatted in .rst (reStructuredText) for use in Sphinx-based documentation systems, which are commonly used for Python projects. It provides a clear and structured explanation of the code example, ensuring that users can understand and apply the example to their own classification tasks.
13 changes: 13 additions & 0 deletions docs/source/examples/simple/api_builder/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Api builder
======================

This section provides basic examples of FEDOT usage:

.. toctree::
:glob:
:maxdepth: 1

classification_with_api_builder
multiple_ts_forecasting_tasks


Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

Fedot Time Series Forecasting Example
================================================================

Overview
--------

This example demonstrates how to use the Fedot framework for time series forecasting. It sets up a Fedot model builder with specific configurations, applies it to multiple datasets, and evaluates the performance of the generated models.

Step-by-Step Guide
------------------

1. Importing Necessary Modules
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: python

from fedot import FedotBuilder
from fedot.core.utils import fedot_project_root

2. Setting Up the Fedot Builder
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: python

if __name__ == '__main__':
SEED = 42

builder = (FedotBuilder('ts_forecasting')
.setup_composition(preset='fast_train', timeout=0.5, with_tuning=True, seed=SEED)
.setup_evolution(num_of_generations=3)
.setup_pipeline_evaluation(metric='mae'))

In this block, the Fedot builder is initialized with a focus on time series forecasting. It sets up the composition with a fast training preset, a timeout of 0.5 seconds, tuning enabled, and a fixed seed for reproducibility. The evolution setup specifies 3 generations, and the evaluation metric is set to MAE (Mean Absolute Error).

3. Defining the Dataset Path
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: python

datasets_path = fedot_project_root() / 'examples/data/ts'

This line defines the path to the directory containing the time series datasets.

4. Processing Each Dataset
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: python

resulting_models = {}
for data_path in datasets_path.iterdir():
if data_path.name == 'ts_sea_level.csv':
continue
fedot = builder.build()
fedot.fit(data_path, target='value')
fedot.predict(features=fedot.train_data, validation_blocks=2)
fedot.plot_prediction()
fedot.current_pipeline.show()
resulting_models[data_path.stem] = fedot

In this loop, each dataset file (excluding 'ts_sea_level.csv') is processed. For each file:

- A Fedot model is built using the configured builder.
- The model is trained on the dataset with the target column 'value'.
- Predictions are made using the training data with 2 validation blocks.
- A plot of the prediction is generated.
- The current pipeline is displayed.
- The model is stored in a dictionary with the dataset filename stem as the key.

Conclusion
----------

This example provides a comprehensive guide on using Fedot for time series forecasting. It demonstrates how to configure and use the Fedot builder to process multiple datasets, evaluate models, and visualize predictions. Users can easily adapt this example to their own datasets and forecasting tasks by modifying the dataset paths and configurations as needed.
106 changes: 106 additions & 0 deletions docs/source/examples/simple/classification/api_classification.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

Fedot Classification Example
============================

This example demonstrates how to use the Fedot framework for automated machine learning (AutoML) to perform classification tasks. It compares a baseline model with a more sophisticated automated model that includes hyperparameter tuning.

Overview
--------

The example uses the Fedot library to train and evaluate classification models on a dataset. It first sets up a baseline model using a predefined Random Forest model and then sets up an automated model with hyperparameter tuning. The performance of both models is evaluated, and the automated model's predictions are visualized if the `visualization` parameter is set to `True`.

Step-by-Step Guide
------------------

1. **Import Necessary Modules**

.. code-block:: python

from fedot import Fedot
from fedot.core.utils import fedot_project_root, set_random_seed

2. **Define the `run_classification_example` Function**

This function takes three parameters: `timeout`, `visualization`, and `with_tuning`. It sets up the problem type, data paths, and initializes the models.

.. code-block:: python

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

3. **Initialize and Train the Baseline Model**

The baseline model is initialized with a predefined Random Forest model and trained on the training data.

.. code-block:: python

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

4. **Make Predictions with the Baseline Model**

The baseline model makes predictions on the test data and its metrics are printed.

.. code-block:: python

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

5. **Initialize and Train the Automated Model**

The automated model is initialized with settings for best quality, hyperparameter tuning, and other parameters. It is trained on the training data.

.. code-block:: python

auto_model = Fedot(problem=problem, timeout=timeout, n_jobs=-1, preset='best_quality',
max_pipeline_fit_time=5, metric=['roc_auc', 'precision'], with_tuning=with_tuning)
auto_model.fit(features=train_data_path, target='target')

6. **Make Predictions with the Automated Model**

The automated model makes probability predictions on the test data, and its metrics are printed with a specified rounding order.

.. code-block:: python

prediction = auto_model.predict_proba(features=test_data_path)
print(auto_model.get_metrics(rounding_order=4))

7. **Visualize the Predictions (Optional)**

If `visualization` is set to `True`, the predictions of the automated model are visualized.

.. code-block:: python

if visualization:
auto_model.plot_prediction()

8. **Return the Predictions**

The function returns the predictions made by the automated model.

.. code-block:: python

return prediction

9. **Run the Example**

The example is executed with a specified timeout and visualization.

.. code-block:: python

if __name__ == '__main__':
set_random_seed(42)
run_classification_example(timeout=10.0, visualization=True)

Usage
-----

To use this example, you can copy and paste the code into your Python environment. Ensure that the Fedot library is installed and that the paths to the datasets are correct. You can modify the `timeout`, `visualization`, and `with_tuning` parameters to suit your needs.

.. note::
This example assumes that the required datasets are available at the specified paths within the Fedot project structure. If you are using different datasets, you will need to adjust the `train_data_path` and `test_data_path` variables accordingly.

.. note::
For more information on the Fedot library and its capabilities, please refer to the `Fedot documentation <https://fedot.readthedocs.io/>`_.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

.. _classification_tuning_example:

====================================================================
Classification Tuning Example with Fedot
====================================================================

This example demonstrates how to use the Fedot framework for tuning a classification pipeline. The example generates synthetic classification datasets with varying parameters and applies a Random Forest classifier, which is then optionally tuned using a simultaneous tuner.

.. note::
This example requires the `Fedot` framework and its dependencies to be installed.

.. contents:: Table of Contents
:depth: 2
:local:

Setup and Imports
-----------------

The first step is to import necessary modules and libraries:

.. code-block:: python

import numpy as np
from golem.core.tuning.simultaneous import SimultaneousTuner
from sklearn.metrics import roc_auc_score as roc_auc
from sklearn.model_selection import train_test_split

from examples.simple.classification.classification_pipelines import classification_random_forest_pipeline
from fedot.core.data.data import InputData
from fedot.core.pipelines.tuning.tuner_builder import TunerBuilder
from fedot.core.repository.dataset_types import DataTypesEnum
from fedot.core.repository.metrics_repository import ClassificationMetricsEnum
from fedot.core.repository.tasks import Task, TaskTypesEnum
from fedot.core.utils import set_random_seed
from fedot.utilities.synth_dataset_generator import classification_dataset

Data Preparation
----------------

The `get_classification_dataset` function prepares synthetic classification datasets with specified parameters:

.. code-block:: python

def get_classification_dataset(features_options, samples_amount=250,
features_amount=5, classes_amount=2, weights=None):
...
return x_data_train, y_data_train, x_data_test, y_data_test

Model Prediction Conversion
---------------------------

The `convert_to_labels` function converts model predictions to binary labels:

.. code-block:: python

def convert_to_labels(root_operation, prediction):
...
return preds

Classification Tuning Experiment
--------------------------------

The `run_classification_tuning_experiment` function runs the classification tuning experiment:

.. code-block:: python

def run_classification_tuning_experiment(pipeline, tuner=None):
...
if tuner is not None:
...
print('Obtained metrics after tuning:')
print(f"{roc_auc(y_test, preds_tuned):.4f}\n")

Running the Example
-------------------

To run the example, execute the following script:

.. code-block:: python

if __name__ == '__main__':
set_random_seed(2020)
run_classification_tuning_experiment(pipeline=classification_random_forest_pipeline(),
tuner=SimultaneousTuner)

.. note::
Ensure you have the necessary permissions and dependencies installed to run the script.

Conclusion
----------

This example showcases the use of Fedot for tuning a classification pipeline, demonstrating how to generate synthetic datasets, apply a classifier, and optionally tune the pipeline for better performance.
Loading
Loading