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-generated docs #1282

Closed
wants to merge 7 commits into from
Closed
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 cases/time_series_gapfilling_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def run_gapfilling_case(file_path):
# Example of using the algorithm to fill in gaps in a time series
# The data is daily air temperature values from the weather station
if __name__ == '__main__':
dataframe = run_gapfilling_case('cases/data/gapfilling/ts_temperature_gapfilling.csv')
dataframe = run_gapfilling_case('cases/data/gapfilling.rst/ts_temperature_gapfilling.csv')

# Display metrics
print_metrics(dataframe)
Expand Down
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
106 changes: 106 additions & 0 deletions docs/source/examples/simple/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/>`_.
83 changes: 83 additions & 0 deletions docs/source/examples/simple/api_explain.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

Fedot API Explain Example
=========================

Overview
--------

This example demonstrates how to use the Fedot framework to build a classification model and explain its predictions using a surrogate decision tree. The example uses a dataset from the Fedot project's cases directory, specifically the cancer training dataset. The model is built using a predefined Random Forest model and then explained using a surrogate decision tree method.

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

1. **Import Necessary Libraries**

The first step is to import the necessary libraries, which include `pandas` for data manipulation and `Fedot` for the machine learning framework.

.. code-block:: python

import pandas as pd
from fedot import Fedot
from fedot.core.utils import fedot_project_root

2. **Define the Function to Run the Example**

The function `run_api_explain_example` is defined with parameters for visualization, timeout, and whether to perform model tuning.

.. code-block:: python

def run_api_explain_example(visualization=False, timeout=None, with_tuning=True):

3. **Load the Training Data**

The training data is loaded from a CSV file located in the Fedot project's cases/data/cancer directory.

.. code-block:: python

train_data = pd.read_csv(f'{fedot_project_root()}/cases/data/cancer/cancer_train.csv', index_col=0)

4. **Prepare Visualization Parameters**

The feature and class names are extracted for visualization purposes.

.. code-block:: python

feature_names = train_data.columns.tolist()
target_name = feature_names.pop()
target = train_data[target_name]
class_names = target.unique().astype(str).tolist()

5. **Build the Classification Model**

A Fedot model is initialized with the problem type 'classification', a timeout, and whether to perform model tuning. The model is then fitted using the training data and a predefined Random Forest model.

.. code-block:: python

model = Fedot(problem='classification', timeout=timeout, with_tuning=with_tuning)
model.fit(features=train_data, target=target_name, predefined_model='rf')

6. **Explain the Model Predictions**

The model's predictions are explained using a surrogate decision tree method. If visualization is enabled, the explanation is saved as an image.

.. code-block:: python

explainer = model.explain(
method='surrogate_dt', visualization=visualization,
save_path=figure_path, dpi=200, feature_names=feature_names,
class_names=class_names, precision=6
)

7. **Run the Example**

The example is executed with visualization enabled and a timeout of 5 seconds.

.. code-block:: python

if __name__ == '__main__':
run_api_explain_example(visualization=True, timeout=5)

Conclusion
----------

This example showcases the use of the Fedot framework for building a classification model and explaining its predictions. It demonstrates how to load data, build a model, and use a surrogate decision tree to explain the model's decisions. The example can be easily adapted for different datasets and models by modifying the data loading and model configuration sections.
124 changes: 124 additions & 0 deletions docs/source/examples/simple/api_forecasting.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

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

Summary
-------

This example demonstrates how to use the Fedot framework for time series forecasting. It includes the process of loading a time series dataset, setting up the forecasting task, training a model, and visualizing the results. The example is structured to be easily adaptable for different datasets and forecasting horizons.

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

### Importing Necessary Libraries

.. code-block:: python

import logging
import random

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

from fedot import Fedot
from fedot.core.data.data import InputData
from fedot.core.data.data_split import train_test_data_setup
from fedot.core.repository.dataset_types import DataTypesEnum
from fedot.core.repository.tasks import TsForecastingParams, Task, TaskTypesEnum
from fedot.core.utils import fedot_project_root

logging.raiseExceptions = False

### Defining Paths and Datasets

.. code-block:: python

_TS_EXAMPLES_DATA_PATH = fedot_project_root().joinpath('examples/data/ts')

TS_DATASETS = {
'm4_daily': _TS_EXAMPLES_DATA_PATH.joinpath('M4Daily.csv'),
'm4_monthly': _TS_EXAMPLES_DATA_PATH.joinpath('M4Monthly.csv'),
'm4_quarterly': _TS_EXAMPLES_DATA_PATH.joinpath('M4Quarterly.csv'),
'm4_weekly': _TS_EXAMPLES_DATA_PATH.joinpath('M4Weekly.csv'),
'm4_yearly': _TS_EXAMPLES_DATA_PATH.joinpath('M4Yearly.csv'),
'australia': _TS_EXAMPLES_DATA_PATH.joinpath('australia.csv'),
'beer': _TS_EXAMPLES_DATA_PATH.joinpath('beer.csv'),
'salaries': _TS_EXAMPLES_DATA_PATH.joinpath('salaries.csv'),
'stackoverflow': _TS_EXAMPLES_DATA_PATH.joinpath('stackoverflow.csv'),
'test_sea': fedot_project_root().joinpath('test', 'data', 'simple_sea_level.csv')
}

### Function to Load and Prepare Time Series Data

.. code-block:: python

def get_ts_data(dataset='m4_monthly', horizon: int = 30, m4_id=None, validation_blocks=None):
time_series = pd.read_csv(TS_DATASETS[dataset])

task = Task(TaskTypesEnum.ts_forecasting,
TsForecastingParams(forecast_length=horizon))
if 'm4' in dataset:
if not m4_id:
label = random.choice(np.unique(time_series['label']))
else:
label = m4_id
print(label)
time_series = time_series[time_series['label'] == label]
idx = time_series['datetime'].values
else:
label = dataset
if dataset not in ['australia']:
idx = pd.to_datetime(time_series['idx'].values)
else:
# non datetime indexes
idx = time_series['idx'].values

time_series = time_series['value'].values
train_input = InputData(idx=idx,
features=time_series,
target=time_series,
task=task,
data_type=DataTypesEnum.ts)
train_data, test_data = train_test_data_setup(train_input, validation_blocks=validation_blocks)
return train_data, test_data, label

### Main Function for Time Series Forecasting

.. code-block:: python

def run_ts_forecasting_example(dataset='australia', horizon: int = 30, timeout: float = None,
visualization=False, validation_blocks=2, with_tuning=True):
train_data, test_data, label = get_ts_data(dataset, horizon, validation_blocks=validation_blocks)
# init model for the time series forecasting

model = Fedot(problem='ts_forecasting',
task_params=Task(TaskTypesEnum.ts_forecasting,
TsForecastingParams(forecast_length=horizon)).task_params,
timeout=timeout,
n_jobs=-1,
metric='mae',
with_tuning=with_tuning)

model.fit(train_data)

pred_fedot = model.forecast(test_data)
if visualization:
model.current_pipeline.show()
plt.plot(train_data.idx, train_data.features, label='features')
plt.plot(test_data.idx, test_data.target, label='target')
plt.plot(test_data.idx, pred_fedot, label='fedot')
plt.grid()
plt.legend()
plt.show()

return pred_fedot

### Running the Example

.. code-block:: python

if __name__ == '__main__':
run_ts_forecasting_example(dataset='m4_monthly', horizon=14, timeout=2., validation_blocks=None, visualization=True)

This documentation page provides a comprehensive guide to using the Fedot framework for time series forecasting. It includes all necessary code snippets and explanations to ensure that users can easily understand and adapt the example for their own purposes.
Loading
Loading