diff --git a/technical/tips.md b/technical/tips.md index 8a7342c..f2bd736 100644 --- a/technical/tips.md +++ b/technical/tips.md @@ -21,7 +21,7 @@ Azure ML で用いる計算リソースはなるべく低コストで運用します。 - クォータの設定 - Workspace 単位で各 VM タイプごとで利用できるコア数を制限することができます。手順は「[ワークスペースレベルのクォータ](https://docs.microsoft.com/ja-JP/azure/machine-learning/how-to-manage-quotas#workspace-level-quotas)」をご参照ください。 - Compute Cluster は、`最小ノード数`を 0 にすることで Job が実行されていないときの課金コストを抑えることができます。また`低優先度`オプションを利用するとコストを 80 % 削減できます。 - - Compute Instances には 2021年2月現時点では自動シャットダウンの機能がありません。手動、もしくは Python SDK ([ComputeInstance Class](https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.compute.computeinstance.computeinstance?view=azure-ml-py))、Azure CLI ([computeinstance stop](https://docs.microsoft.com/en-us/cli/azure/ext/azure-cli-ml/ml/computetarget/computeinstance?view=azure-cli-latest#ext_azure_cli_ml_az_ml_computetarget_computeinstance_stop)) を用いて停止スクリプトを作成してください。 + - Compute Instance の自動開始と停止の機能がプレビューで発表されました。詳細は [自動開始と停止をスケジュール設定する (プレビュー)](https://docs.microsoft.com/ja-JP/azure/machine-learning/how-to-create-manage-compute-instance?tabs=python#schedule-automatic-start-and-stop-preview) をご参照ください。 - その他、詳細は 「[Azure Machine Learning のコストを計画して管理する](https://docs.microsoft.com/ja-jp/azure/machine-learning/concept-plan-manage-cost)」 をご参照ください。 - なお、Azure Machine Learning 全体の正確なコスト計算は現在価格表では行えません。例えば Compute Instances で確保されているディスクやロードバランサーなどのコストを加味する必要があります。「[Azure Machine Learning のコスト見積もりについて](https://jpmlblog.github.io/blog/2020/06/18/AML-estimate-costs/)」をご参照ください。 @@ -31,83 +31,84 @@ * **pyodbc を用いた DB とのやり取り** + Database に格納されているデータを取得する方法として、Azure ML の Dataset ではなく pyodbc を利用したい場合があります。モデル学習時の環境を定義情報を管理する機能として Environment (環境) があります。pyodbc では通常 OS レベルでのパッケージのインストールが必要なため、python のパッケージを pip や conda でインストールだけでは不十分です。下記のコードを参考に Azure ML Environment を作成してください。 -
-1. Azure ML Environment の定義 (Python) - - -```python -from azureml.core import Workspace, Environment -from azureml.core.environment import CondaDependencies - -# 環境 Environment の名称 -env = Environment("pyodbc-env") - -# Docker File の定義 -dockerfile = r""" -FROM mcr.microsoft.com/azureml/openmpi3.1.2-ubuntu18.04 -RUN echo "Hello from custom container!" - -RUN apt-get update -RUN apt-get install locales -RUN locale-gen en_US.UTF-8 -RUN update-locale LANG=en_US.UTF-8 - -# Install MS SQL v13 driver for Odbc -RUN apt-get install -y curl -RUN apt-get install apt-transport-https -RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - -RUN curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list -RUN exit -RUN apt-get update -RUN ACCEPT_EULA=Y apt-get install -y msodbcsql17 -RUN apt-get install -y unixodbc-dev -""" - -# Set base image to None, because the image is defined by dockerfile. -env.docker.base_image = None -env.docker.base_dockerfile = dockerfile - -env.python.conda_dependencies = CondaDependencies.create(conda_packages=['scikit-learn','pyodbc'], - pip_packages=['azureml-defaults','azureml-dataprep[pandas]']) -``` -
- -
2. Python Script での利用 (Python) +
+ 1. Azure ML Environment の定義 (Python) + + + ```python + from azureml.core import Workspace, Environment + from azureml.core.environment import CondaDependencies + + # 環境 Environment の名称 + env = Environment("pyodbc-env") + + # Docker File の定義 + dockerfile = r""" + FROM mcr.microsoft.com/azureml/openmpi3.1.2-ubuntu18.04 + RUN echo "Hello from custom container!" + + RUN apt-get update + RUN apt-get install locales + RUN locale-gen en_US.UTF-8 + RUN update-locale LANG=en_US.UTF-8 + + # Install MS SQL v13 driver for Odbc + RUN apt-get install -y curl + RUN apt-get install apt-transport-https + RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - + RUN curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list + RUN exit + RUN apt-get update + RUN ACCEPT_EULA=Y apt-get install -y msodbcsql17 + RUN apt-get install -y unixodbc-dev + """ + + # Set base image to None, because the image is defined by dockerfile. + env.docker.base_image = None + env.docker.base_dockerfile = dockerfile + + env.python.conda_dependencies = CondaDependencies.create(conda_packages=['scikit-learn','pyodbc'], + pip_packages=['azureml-defaults','azureml-dataprep[pandas]']) + ``` +
+ +
2. Python Script での利用 (Python) + + + ```python + import pandas as pd + import pyodbc + + server = 'tcp:xxxx' + database = 'xxxx' + username = 'xxxx' + password = 'xxxx' + cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password) + cursor = cnxn.cursor() + + sql = """ + SELECT * FROM [dbo].[FactTable] + """ + df = pd.read_sql(sql, cnxn) + print(df.head()) + ``` + +
+
参考情報 + + - [Azure Machine Learning でソフトウェア環境を作成して使用する](https://docs.microsoft.com/ja-jp/azure/machine-learning/how-to-use-environments) + - [Microsoft ODBC Driver for SQL Server をインストールする (Linux)](https://docs.microsoft.com/ja-jp/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=azure-sqldw-latest#ubuntu17) - -```python -import pandas as pd -import pyodbc - -server = 'tcp:xxxx' -database = 'xxxx' -username = 'xxxx' -password = 'xxxx' -cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password) -cursor = cnxn.cursor() - -sql = """ -SELECT * FROM [dbo].[FactTable] -""" -df = pd.read_sql(sql, cnxn) -print(df.head()) -``` -
+
-参考情報 -- [Azure Machine Learning でソフトウェア環境を作成して使用する](https://docs.microsoft.com/ja-jp/azure/machine-learning/how-to-use-environments) - -- [Microsoft ODBC Driver for SQL Server をインストールする (Linux)](https://docs.microsoft.com/ja-jp/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=azure-sqldw-latest#ubuntu17) - - - * **重複行データを取り除く方法** - Azure Synapse Pipeline (Azure Data Factory) の Data Flow で簡単に除外すること可能です。 + Azure Synapse Pipeline (Azure Data Factory) の DataFlow で重複業データを簡単に除外することができます。 - 参考 : [Azure Data Factory - Data Flowで重複行を取り除く (最初の行だけ選択する)方法メモ](https://zenn.dev/shohei_aio/articles/5c9716ac817b79) * **Excel ファイル の各シートごとの処理の実行方法** @@ -121,9 +122,6 @@ print(df.head()) ## 特徴量エンジニアリング - - - * **Parquet ファイルで I/O を改善する** 大きいデータファイルを扱うときは CSV ではなく parquet ファイルの利用をお勧めします。parquet は列指向のファイルフォーマットで圧縮率が高いです。Azure ML の AutoML で大きいデータサイズを扱う際に有効でした。