# 料理推薦Webアプリを作成しよう
このレッスンでは、以前のレッスンで学んだ技術と、このシリーズ全体で使用されているおいしい料理データセットを使用して、分類モデルを構築します。さらに、保存したモデルを使用する小さなWebアプリを構築し、OnnxのWebランタイムを活用します。
機械学習の最も実用的な用途の一つは推薦システムの構築であり、今日からその方向に一歩を踏み出すことができます!
[](https://youtu.be/17wdM9AHMfg "Applied ML")
> 🎥 上の画像をクリックしてビデオを見る: Jen Looperが分類された料理データを使用してWebアプリを構築します
## [レッスン前クイズ](https://gray-sand-07a10f403.1.azurestaticapps.net/quiz/25/)
このレッスンで学ぶこと:
- モデルを構築してOnnxモデルとして保存する方法
- Netronを使用してモデルを検査する方法
- 推論のためにWebアプリでモデルを使用する方法
## モデルを構築しよう
ビジネスシステムにこれらの技術を活用するためには、応用機械学習システムを構築することが重要です。Onnxを使用することで、Webアプリケーション内でモデルを使用し(必要に応じてオフラインコンテキストでも使用可能)、JavaScriptアプリケーションで使用することができます。
[前のレッスン](../../3-Web-App/1-Web-App/README.md)では、UFO目撃情報についての回帰モデルを構築し、それをFlaskアプリで使用しました。このアーキテクチャは非常に有用ですが、フルスタックのPythonアプリであり、JavaScriptアプリケーションを使用する要件があるかもしれません。
このレッスンでは、推論のための基本的なJavaScriptベースのシステムを構築できます。しかし、まずモデルを訓練し、Onnxで使用するために変換する必要があります。
## 演習 - 分類モデルを訓練しよう
まず、使用したクリーンな料理データセットを使用して分類モデルを訓練します。
1. 有用なライブラリをインポートすることから始めます:
```python
!pip install skl2onnx
import pandas as pd
```
Scikit-learnモデルをOnnx形式に変換するために '[skl2onnx](https://onnx.ai/sklearn-onnx/)' が必要です。
1. 次に、前のレッスンと同じ方法でデータを処理し、`read_csv()`を使用してCSVファイルを読み込みます:
```python
data = pd.read_csv('../data/cleaned_cuisines.csv')
data.head()
```
1. 不要な最初の2列を削除し、残りのデータを 'X' として保存します:
```python
X = data.iloc[:,2:]
X.head()
```
1. ラベルを 'y' として保存します:
```python
y = data[['cuisine']]
y.head()
```
### 訓練ルーチンを開始しよう
'SVC' ライブラリを使用します。これは高い精度を持っています。
1. Scikit-learnから適切なライブラリをインポートします:
```python
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score,precision_score,confusion_matrix,classification_report
```
1. 訓練セットとテストセットを分けます:
```python
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3)
```
1. 前のレッスンで行ったように、SVC分類モデルを構築します:
```python
model = SVC(kernel='linear', C=10, probability=True,random_state=0)
model.fit(X_train,y_train.values.ravel())
```
1. 次に、`predict()`を呼び出してモデルをテストします:
```python
y_pred = model.predict(X_test)
```
1. モデルの品質を確認するために分類レポートを出力します:
```python
print(classification_report(y_test,y_pred))
```
以前見たように、精度は良好です:
```output
precision recall f1-score support
chinese 0.72 0.69 0.70 257
indian 0.91 0.87 0.89 243
japanese 0.79 0.77 0.78 239
korean 0.83 0.79 0.81 236
thai 0.72 0.84 0.78 224
accuracy 0.79 1199
macro avg 0.79 0.79 0.79 1199
weighted avg 0.79 0.79 0.79 1199
```
### モデルをOnnxに変換しよう
適切なテンソル数で変換を行うことを確認してください。このデータセットには380の成分が記載されているため、`FloatTensorType`にその数を記載する必要があります:
1. 380のテンソル数を使用して変換します。
```python
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
initial_type = [('float_input', FloatTensorType([None, 380]))]
options = {id(model): {'nocl': True, 'zipmap': False}}
```
1. onxを作成し、ファイル **model.onnx** として保存します:
```python
onx = convert_sklearn(model, initial_types=initial_type, options=options)
with open("./model.onnx", "wb") as f:
f.write(onx.SerializeToString())
```
> Note、変換スクリプトに[オプション](https://onnx.ai/sklearn-onnx/parameterized.html)を渡すことができます。この場合、'nocl'をTrue、'zipmap'をFalseに設定しました。これは分類モデルなので、辞書のリストを生成するZipMapを削除するオプションがあります(必要ありません)。`nocl` refers to class information being included in the model. Reduce your model's size by setting `nocl` to 'True'.
Running the entire notebook will now build an Onnx model and save it to this folder.
## View your model
Onnx models are not very visible in Visual Studio code, but there's a very good free software that many researchers use to visualize the model to ensure that it is properly built. Download [Netron](https://github.com/lutzroeder/Netron) and open your model.onnx file. You can see your simple model visualized, with its 380 inputs and classifier listed:

Netron is a helpful tool to view your models.
Now you are ready to use this neat model in a web app. Let's build an app that will come in handy when you look in your refrigerator and try to figure out which combination of your leftover ingredients you can use to cook a given cuisine, as determined by your model.
## Build a recommender web application
You can use your model directly in a web app. This architecture also allows you to run it locally and even offline if needed. Start by creating an `index.html` file in the same folder where you stored your `model.onnx`ファイル。
1. このファイル _index.html_ に次のマークアップを追加します:
```html