PyCaretやーる（Windows10、Python3.8）

# はじめに
AutoMLのPyCaretやっていきまーす

# 開発環境
- Windows 10 PC
- Python 3.8

# 導入

~~~
conda create -n py38 python=3.8
conda activate py38
~~~

~~~
pip install pycaret[full]
~~~

# Classification

1．ワインのクオリティデータセットをダウンロード（winequality-white.csv, winequality-red.csv）

https://archive.ics.uci.edu/ml/datasets/wine+quality

2．モデルを作成する

~~~wine_classification_train.py
import pandas as pd 
from sklearn.model_selection import train_test_split
from pycaret.classification import *

white_wine = pd.read_csv("./winequality-white.csv", sep=';')
red_wine = pd.read_csv("./winequality-red.csv", sep=';')
white_wine['is_red'] = 0.0
red_wine['is_red'] = 1.0
data_df = pd.concat([white_wine, red_wine], axis=0)
data_labels = data_df['quality'] >= 7
data_df = data_df.drop(['quality'], axis=1)
print(data_df.head())

train, test = train_test_split(data_df)
s = setup(train, target='is_red')
best = compare_models()
evaluate_model(best)
predictions = predict_model(best, data=test)
save_model(best, 'wine_classification')
~~~

3．モデル（wine_classification.pkl）が保存される

4．保存したモデルを読み込み推論する

~~~wine_classification_prediction.py
import pandas as pd 
from sklearn.model_selection import train_test_split
from pycaret.classification import *

white_wine = pd.read_csv("./winequality-white.csv", sep=';')
red_wine = pd.read_csv("./winequality-red.csv", sep=';')
white_wine['is_red'] = 0.0
red_wine['is_red'] = 1.0
data_df = pd.concat([white_wine, red_wine], axis=0)
data_labels = data_df['quality'] >= 7
data_df = data_df.drop(['quality'], axis=1)

train, test = train_test_split(data_df)
saved_model = load_model('wine_classification')
new_prediction = predict_model(saved_model, data=test)
print(new_prediction.head())
~~~

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/2906e3bf-3899-8cc0-bf2c-c70484829cc1.png)


# Regression

1．ダイアモンドのデータセットを用いて学習する

~~~diamond_regression_train.py
import pandas as pd 
from sklearn.model_selection import train_test_split
from pycaret.regression import *
from pycaret.datasets import get_data

data_df = get_data('diamond')
train, test = train_test_split(data_df)
s = setup(train, target = 'Price')
best = compare_models()
evaluate_model(best)
predictions = predict_model(best, data=test)
save_model(best, 'diamond_regression')
~~~

2．学習したモデルを読み込み推論する

~~~diamond_regression_prediction.py
import pandas as pd 
from sklearn.model_selection import train_test_split
from pycaret.regression import *
from pycaret.datasets import get_data

data_df = get_data('diamond')
train, test = train_test_split(data_df)
saved_model = load_model('diamond_regression')
new_prediction = predict_model(saved_model, data=test)
print(new_prediction.head())
~~~

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/14512a50-6bcb-89aa-ef01-8acd674b3735.png)

# Clustering

保留

# Anomaly Detection

保留

# Time Series Forecasting App with PyCaret and Streamlit

https://youtu.be/fQthJvYEqx8

# EDA with PyCaret

https://youtu.be/Pm5VOuYqU4Q

1．pycaretのインストール

~~~
pip install pycaret[full]
~~~

もしくは

~~~
pip install pycaret
pip install autoviz
~~~

2．jupyter notebook上でやる

~~~
jupyter notebook
~~~

3．irisデータセットの読み込み

~~~python
from pycaret.datasets import get_data
data = get_data('iris')
~~~

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/00b13d10-5a96-8022-0169-2af444c1c925.png)

4．セットアップ

~~~python
from pycaret.classification import *
s = setup(data, target = 'species', session_id = 123)
~~~

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/4bcc73e8-0b9b-c07a-696d-2e0466dc125c.png)

5．EDA

~~~python
eda()
~~~

~~~
Alert! from version 0.1.39, fter importing, you must do '%matplotlib inline' to display charts in Jupyter Notebooks.
    AV = AutoViz_Class()
    dfte = AV.AutoViz(filename, sep=',', depVar='', dfte=None, header=0, verbose=0, lowess=False,
               chart_format='svg',max_rows_analyzed=150000,max_cols_analyzed=30, save_plot_dir=None)
Note: verbose=0 or 1 generates charts and displays them in your local Jupyter notebook.
      verbose=2 does not display plots but saves them in AutoViz_Plots folder in local machine.
Updated: chart_format='bokeh' generates and displays charts in your local Jupyter notebook.
      chart_format='server' generates and displays charts in the browser - one tab for each chart.
      chart_format='html' silently saves charts HTML format - they are also interactive!

Shape of your Data Set loaded: (150, 5)
#######################################################################################
######################## C L A S S I F Y I N G  V A R I A B L E S  ####################
#######################################################################################
Classifying variables in data set...
    4 Predictors classified...
        No variables removed since no ID or low-information variables found in data set

################ Multi_Classification problem #####################
~~~

||||
|:--|:--|:--|
|![bokeh_plot (1).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/d87e9529-bbd7-de43-27fb-6819869b1244.png)|![bokeh_plot (2).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/e162cef1-7b27-181d-fdb0-3d3a9c8ec559.png)|![bokeh_plot (3).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/772691c2-65d5-9a18-6a90-ad41e616302e.png)|
|![bokeh_plot (4).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/3b548b21-b3fe-f22c-1e5d-4704d6ae8c62.png)|![bokeh_plot (5).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/cb49fe6c-6a05-adc6-d428-bdea08783aae.png)|![bokeh_plot (6).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/49ad0ea2-6b00-6e16-1765-21abfaec52a1.png)|
|![bokeh_plot (7).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/f86c0c65-77aa-6b22-c432-f939ae4d6280.png)|![bokeh_plot (8).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/abd45e23-864a-23e1-40f3-f56bf2a621ec.png)|![bokeh_plot (9).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/08086fc0-1d82-16cf-3e25-065270a551a1.png)|

Databricksではsetupのところを下記にする必要がある

~~~python
s = setup(data, target = 'species', session_id = 123, silent = True, html = False)
~~~

https://qiita.com/manabian/items/ec6559bc0a3cad2f987b

DatabricksだとEDAの表示はできなさそう、だれかできる人いたら教えてください。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/cae28115-c9bb-6158-0897-2288debd7b0a.png)

# 参考文献

## Tutorials 

https://pycaret.gitbook.io/docs/get-started/tutorials

https://github.com/pycaret/pycaret/tree/master/tutorials

## Examples

https://github.com/pycaret/pycaret/tree/master/examples

## Videos

https://pycaret.gitbook.io/docs/learn-pycaret/videos

## Cheat sheet

https://pycaret.gitbook.io/docs/learn-pycaret/cheat-sheet

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/b06541a6-53fe-8a0f-1123-abd5f37d4fb5.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/83ad0ee1-a8d0-09ca-49da-38b586eed92c.png)