2019年1月10日にワークショップでMicrosoft Azure Machine Learning Studio (以下は、ML Studio)を学ぶ機会があった。ML Studioを使って自動車の価格予測をするモデルを作成した。その時に他の参加者からデプロイしたWebサービスをPower BIで活用できないかという質問があったので試しに作ってみる。
やりたい事
Power BIからML StudioでデプロイしたモデルのWebサービスを呼び出して、価格の予測する。
- 初めに自動車の情報をインポート
- インポートした情報を入力にWebサービスで価格を予測

データセットは、ML Studio のサンプルとして用意されているAutomobile prise data (Raw)を使用しており以下のコードのカラムもこのデータセットに準拠しています。
進め方
Power BI Desktop で検証しました。
Power BI サービスでも動くと思います。
PowerQuery で作成しているため、Excel でも動くと思います。
- 予測する自動車のデータをExcelで用意してテーブルを設定
- Power Bi DesktopでExcelファイルを読み込む
- カスタム列を追加してコードを書く
カスタム列
let
apikey = "YOUR_API_KEY_HERE",
endpoint = "https://ussouthcentral.services.azureml.net/workspaces/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/services/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/execute",
parameter = "?api-version=2.0&details=true",
url = endpoint & parameter,
#"txt_symboling" = Number.ToText([#"symboling"]),
#"txt_normalized-losses" = Number.ToText([#"normalized-losses"]),
#"txt_make" = [#"make"],
#"txt_fuel-type" = [#"fuel-type"],
#"txt_aspiration" = [#"aspiration"],
#"txt_num-of-doors" = [#"num-of-doors"],
#"txt_body-style" = [#"body-style"],
#"txt_drive-wheels" = [#"drive-wheels"],
#"txt_engine-location" = [#"engine-location"],
#"txt_wheel-base" = Number.ToText([#"wheel-base"]),
#"txt_length" = Number.ToText([#"length"]),
#"txt_width" = Number.ToText([#"width"]),
#"txt_height" = Number.ToText([#"height"]),
#"txt_curb-weight" = Number.ToText([#"curb-weight"]),
#"txt_engine-type" = [#"engine-type"],
#"txt_num-of-cylinders" = [#"num-of-cylinders"],
#"txt_engine-size" = Number.ToText([#"engine-size"]),
#"txt_fuel-system" = [#"fuel-system"],
#"txt_bore" = Number.ToText([#"bore"]),
#"txt_stroke" = Number.ToText([#"stroke"]),
#"txt_compression-ratio" = Number.ToText([#"compression-ratio"]),
#"txt_horsepower" = Number.ToText([#"horsepower"]),
#"txt_peak-rpm" = Number.ToText([#"peak-rpm"]),
#"txt_city-mpg" = Number.ToText([#"city-mpg"]),
#"txt_highway-mpg" = Number.ToText([#"highway-mpg"]),
#"txt_price" = Number.ToText([#"price"]),
jsonbody = "{
""Inputs"": {
""input1"": {
""ColumnNames"": [
""symboling"",
""normalized-losses"",
""make"",
""fuel-type"",
""aspiration"",
""num-of-doors"",
""body-style"",
""drive-wheels"",
""engine-location"",
""wheel-base"",
""length"",
""width"",
""height"",
""curb-weight"",
""engine-type"",
""num-of-cylinders"",
""engine-size"",
""fuel-system"",
""bore"",
""stroke"",
""compression-ratio"",
""horsepower"",
""peak-rpm"",
""city-mpg"",
""highway-mpg"",
""price""
],
""Values"": [
[
""" & #"txt_symboling" & """,
""" & #"txt_normalized-losses" & """,
""" & #"txt_make" & """,
""" & #"txt_fuel-type" & """,
""" & #"txt_aspiration" & """,
""" & #"txt_num-of-doors" & """,
""" & #"txt_body-style" & """,
""" & #"txt_drive-wheels" & """,
""" & #"txt_engine-location" & """,
""" & #"txt_wheel-base" & """,
""" & #"txt_length" & """,
""" & #"txt_width" & """,
""" & #"txt_height" & """,
""" & #"txt_curb-weight" & """,
""" & #"txt_engine-type" & """,
""" & #"txt_num-of-cylinders" & """,
""" & #"txt_engine-size" & """,
""" & #"txt_fuel-system" & """,
""" & #"txt_bore" & """,
""" & #"txt_stroke" & """,
""" & #"txt_compression-ratio" & """,
""" & #"txt_horsepower" & """,
""" & #"txt_peak-rpm" & """,
""" & #"txt_city-mpg" & """,
""" & #"txt_highway-mpg" & """,
""" & #"txt_price" & """
]
]
}
},
""GlobalParameters"": {}
}",
bytesbody = Text.ToBinary(jsonbody),
headers = [#"Authorization" = "Bearer " & apikey,
#"Content-Type" = "application/json"],
bytesresp = Web.Contents(url, [Headers=headers, Content=bytesbody]),
jsonresp = Json.Document(bytesresp),
Results = jsonresp[Results],
output1 = Results[output1],
value = output1[value],
Values = value[Values],
Values1 = Values{0},
convTable = Table.FromList(Values1, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
convTable
参考
以下のページを参考しました。
チュートリアル: Power BI を Text Analytics Cognitive Service と統合する