分析手順の設計
==============
本項では、分析手順の設計について、SAMPO/FABとsklearn-fabにおける実現方法およびスクリプトの差異を示します。
分析手順の設計は、データと前処理、学習器とパラメーター、などを定め、実現したい分析を実行するために実施します。

**SAMPO/FAB**

* SPD, SRCにより、分析手順を設計します。

**sklearn-fab**

* estimator選択、パラメーター指定などにより、分析手順を設計します。
* 分析手順を一括実行するための、\ sklearn.pipeline.Pipeline_\を使用することもできます。
* SAMPO/FABのSPD内で定義される、global_settingsに相当するものは必要ありません。

.. csv-table::
  :header: "SAMPO/FAB", "sklearn-fab"

  "
  .. code-block:: python
    :caption: **データ準備**

    import pandas as pd

    # 学習用データ
    train_data = pd.read_csv('../data/train_data.csv')
    train_data.insert(0, '_sid', list(range(train_data.shape[0])))
    # 予測用データ
    predict_data = pd.read_csv('../data/predict_data.csv')
    predict_data.insert(0, '_sid', list(range(predict_data.shape[0])))
  ", "
  .. code-block:: python
    :caption: **データ準備**

    import pandas as pd

    # 学習用データ
    train_data = pd.read_csv('../data/train_data.csv')
    # 予測用データ
    predict_data = pd.read_csv('../data/predict_data.csv')
  "
  "
  .. code-block:: python
    :caption: **ASD**

    from sampotools.api import gen_asd_from_pandas_df

    asd = gen_asd_from_pandas_df(train_data)
  ", ""
  "
  .. code-block:: python
    :caption: **SPD**

    from sampo.api import gen_spd

    spd_content = '''
    dl -> std -> rg

    ---
    components:
        dl:
            component: DataLoader

        std:
            component: StandardizeFDComponent
            features: scale == 'real' or scale == 'integer'

        rg:
            component: FABHMEBernGateLinearRgComponent
            features: name != 'price'
            target: name == 'price'
            standardize_target: True
            tree_depth: 3
            shrink_threshold: 2.0

    global_settings:
        keep_attributes:
            - price
        feature_exclude:
            - price
    '''

    spd = gen_spd(template=spd_content)
  ", "
  .. code-block:: python
    :caption: **sklearn.pipeline.Pipelineを使用しない場合**

    from sklearn.preprocessing import StandardScaler
    from sklearn_fab import SklearnFABBernGateLinearRegressor

    # SPD内のstd componentに相当するインスタンスを作成
    scaler = StandardScaler()
    # SPD内のrg componentに相当するインスタンスを作成
    estimator = SklearnFABBernGateLinearRegressor(tree_depth=3, shrink_threshold=2.0)

  .. code-block:: python
    :caption: **sklearn.pipeline.Pipelineを使用した場合**

    from sklearn.pipeline import Pipeline
    from sklearn.preprocessing import StandardScaler
    from sklearn_fab import SklearnFABBernGateLinearRegressor

    # SPD内のstd, rg componentに相当する機能をもつパイプラインを作成
    pipeline = Pipeline([('scaler', StandardScaler()),
                         ('estimator', SklearnFABBernGateLinearRegressor(tree_depth=3, shrink_threshold=2.0))])
  "
  "
  .. code-block:: python
    :caption: **学習用SRC**

    train_src_temp = '''
    train:
        type: learn
        data_sources:
            dl:
                df: {{ data_df }}
                attr_schema: {{ asd }}
    '''
  ", ""
  "
  .. code-block:: python
    :caption: **予測用SRC**

    predict_src_temp = '''
    predict:
        type: predict
        data_sources:
            dl:
                df: {{ data_df }}
                attr_schema: {{ asd }}
        model_process: {{ model_process }}
    '''
  ", ""

.. _sklearn.pipeline.Pipeline: https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html
