Watson IoT Platform - Predictive Analyticsでリアルタイム解析する

※この手順はSPSS Modelerを使用します。お持ちでない方は、ごめんなさい。

少しWatson IoT Platformを使っていらっしゃる、というかたのためのガイドです。
[Watson IoT Platformを使ってみる](http://qiita.com/egplnt/items/af867711a7191923b2ff)でIoTアプリを準備しました。
Predictive Analyticsを使ってリアルタイムにIoTデータを解析します。

#Predictive Analyticsを用意します
1. Bluemixにログインし、「データ＆分析」のメニューから「Predictive Analytics」サービスをインスタンス化します
![スクリーンショット 2016-06-17 13.41.12.png](https://qiita-image-store.s3.amazonaws.com/0/125842/8036708f-c479-01a0-0db7-5468c3146762.png)

2. 使用しているIoTアプリの「接続」メニューから上記で作成したPredictive Analyticsサービスを接続します。
![スクリーンショット 2016-06-17 13.47.21.png](https://qiita-image-store.s3.amazonaws.com/0/125842/ad5f359c-2d66-48cf-33df-d06324a3b697.png)

3. 接続されたら、「資格情報の表示」をクリックして、表示される`access_key`と`url`を書きとめておきます（画面は省略）
4. Predictive Analyticsのタイルをクリックしてダッシュボード画面へ入ります。
5. 画面右端の「New Model Stream」にstrファイルをドロップするかファイル選択で指定します。
![スクリーンショット 2016-06-17 13.51.31.png](https://qiita-image-store.s3.amazonaws.com/0/125842/dfc71b6c-9758-d05d-7380-524cf3a8a66b.png)

6. Contextを指定するポップアップが表示されるので任意の文字列を指定します。このポップアップがブラウザ画面の上のほうに表示されることがあるので画面を上へスクロールして確認します。
![スクリーンショット 2016-06-17 13.53.55.png](https://qiita-image-store.s3.amazonaws.com/0/125842/f1fb21eb-5442-5ee0-4384-31fe7e24e509.png)

7. ダッシュボード画面に戻り、画面下部にContextIDとともにdeployされたstrファイルが表示される事を確認します。

#Node-REDから呼び出します
1. Node-REDのfunctionノードから上記のPredictive Analyticsサービスを呼び出します。
![スクリーンショット 2016-06-17 13.56.28.png](https://qiita-image-store.s3.amazonaws.com/0/125842/13818c7c-d1e2-8beb-6d43-2ad5dfba7e23.png)

2. Methodは`POST`、URLに下記を指定します。
`https://palbyp.pmservice.ibmcloud.com/pm/v1/score/コンテキスト値?accesskey=アクセスキー値(172桁)`
3. `Inject`ノードを使って適当なデータを送付し、リアルタイムでスコアリングできるかを確認します。
4. 送付する値の指定方法は、`{'tablename':'テーブル名','header':['項目1', '項目2', '項目3',...],'data':[[値1, 値2, 値3,...]]}`の形式にします。
5. 例：`{'tablename':'test_data.txt','header':['time', 'accel', 'gyro', 'temp', 'humid', 'outcome'],'data':[[100008, 331, 22, 35, 48, 0]]}`

#サンプル
1. （近日中に提供予定）
![スクリーンショット 2016-06-17 14.44.55.png](https://qiita-image-store.s3.amazonaws.com/0/125842/c84b902a-9d0c-b519-7e1e-1f798b2835cb.png)

- サンプルフロー１
![スクリーンショット 2016-06-17 14.41.04.png](https://qiita-image-store.s3.amazonaws.com/0/125842/4ef92a56-f3d4-3a99-193c-92e1d42bc364.png)

```
[{"id":"e8616ab3.913248","type":"http request","z":"70883484.827c6c","name":"予測モデルへの問い合わせ","method":"POST","ret":"txt","url":"","x":640,"y":120,"wires":[["3831e97e.344a2e"]]},{"id":"ff1239b3.c2937","type":"debug","z":"70883484.827c6c","name":"出力","active":true,"console":"false","complete":"payload","x":810,"y":320,"wires":[]},{"id":"51769a01.f5c2c4","type":"template","z":"70883484.827c6c","name":"緊急アクション","field":"","fieldType":"msg","syntax":"mustache","template":"機械が壊れます！すぐ停止してください！！","x":660,"y":300,"wires":[["ff1239b3.c2937","d3fb7051.c149d8"]]},{"id":"3831e97e.344a2e","type":"function","z":"70883484.827c6c","name":"予測結果の抽出","func":"var obj = JSON.parse(msg.payload);\nvar predVal = obj[0].data[0][6];\nvar propensity = obj[0].data[0][7];\n\nmsg.propensity = ( Math.round( propensity * 10000 ) ) / 100;\n\nif ( predVal == \"0\") {\n    msg.prediction = \"壊れる\";\n} else {\n    msg.prediction = \"うまくいく\";\n}\n\nmsg.payload = \"確率\" + msg.propensity + \"％で\" + msg.prediction + \"と思います。\";\nreturn msg;\n","outputs":1,"noerr":0,"x":120,"y":300,"wires":[["1945de52.3be322","f2ebd32a.a0ead8","4408ea26.209cbc","43df4be7.a05efc"]]},{"id":"4466fd20.ab94ac","type":"function","z":"70883484.827c6c","name":"問い合わせ値の準備","func":"msg.payload = {'tablename':'test_data.txt','header':\n['time', 'accel', 'gyro', 'temp', 'humid', 'outcome'],\n'data':[[100007, 3, 12, 30, 50, 1]]};return msg;","outputs":1,"noerr":0,"x":360,"y":120,"wires":[["e8616ab3.913248"]]},{"id":"935f5363.f5907","type":"inject","z":"70883484.827c6c","name":"大丈夫なケース","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":140,"y":120,"wires":[["4466fd20.ab94ac"]]},{"id":"de10d25c.05cd88","type":"function","z":"70883484.827c6c","name":"問い合わせ値の準備","func":"msg.payload = {'tablename':'test_data.txt','header':\n['time', 'accel', 'gyro', 'temp', 'humid', 'outcome'],\n'data':[[100008, 331, 22, 35, 48, 0]]};\nreturn msg;\n","outputs":1,"noerr":0,"x":360,"y":60,"wires":[["e8616ab3.913248"]]},{"id":"d6b8f346.36bb88","type":"inject","z":"70883484.827c6c","name":"壊れるケース","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":130,"y":60,"wires":[["de10d25c.05cd88"]]},{"id":"8420ecb.439c21","type":"function","z":"70883484.827c6c","name":"問い合わせ値の準備","func":"msg.payload = \n{'tablename':'test_data.txt','header':\n['time', 'accel', 'gyro', 'temp', 'humid', 'outcome'],\n'data':[[100008, 10, 5, 40, 40, 1]]};\nreturn msg;\n","outputs":1,"noerr":0,"x":360,"y":180,"wires":[["e8616ab3.913248"]]},{"id":"dce80875.6d876","type":"inject","z":"70883484.827c6c","name":"壊れそうなケース","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":140,"y":180,"wires":[["8420ecb.439c21"]]},{"id":"1945de52.3be322","type":"debug","z":"70883484.827c6c","name":"","active":false,"console":"false","complete":"prediction","x":320,"y":380,"wires":[]},{"id":"f2ebd32a.a0ead8","type":"debug","z":"70883484.827c6c","name":"","active":false,"console":"false","complete":"propensity","x":320,"y":420,"wires":[]},{"id":"a12dc9c9.161c18","type":"switch","z":"70883484.827c6c","name":"閾値で判定","property":"propensity","propertyType":"msg","rules":[{"t":"gte","v":"80","vt":"str"}],"checkall":"true","outputs":1,"x":490,"y":300,"wires":[["51769a01.f5c2c4"]]},{"id":"4408ea26.209cbc","type":"switch","z":"70883484.827c6c","name":"「壊れる」場合","property":"prediction","propertyType":"msg","rules":[{"t":"eq","v":"壊れる","vt":"str"}],"checkall":"true","outputs":1,"x":320,"y":300,"wires":[["a12dc9c9.161c18"]]},{"id":"43df4be7.a05efc","type":"debug","z":"70883484.827c6c","name":"","active":true,"console":"false","complete":"false","x":310,"y":340,"wires":[]},{"id":"d3fb7051.c149d8","type":"e-mail","z":"70883484.827c6c","server":"smtp.gmail.com","port":"465","name":"","dname":"","x":810,"y":280,"wires":[]}]

```

- サンプルフロー２
![スクリーンショット 2016-06-28 13.52.19.png](https://qiita-image-store.s3.amazonaws.com/0/125842/4f25fe68-1301-c983-4d42-f1230c45ddd9.png)

```
[{"id":"dcc0f34a.df6b98","type":"template","z":"6f9e4a89.ccb3cc","name":"危険","field":"","fieldType":"msg","syntax":"mustache","template":"予測値 ({{payload}}) は危険です！点検してください！！","x":590,"y":320,"wires":[["b4d809e9.f0794"]]},{"id":"b4d809e9.f0794","type":"debug","z":"6f9e4a89.ccb3cc","name":"結果の出力","active":false,"console":"false","complete":"payload","x":750,"y":300,"wires":[]},{"id":"1302ed57.eaa183","type":"template","z":"6f9e4a89.ccb3cc","name":"安全","field":"","fieldType":"msg","syntax":"mustache","template":"予測値 ({{payload}}) は安全です。","x":590,"y":280,"wires":[["b4d809e9.f0794"]]},{"id":"6b4768d7.bce78","type":"switch","z":"6f9e4a89.ccb3cc","name":"予測結果からのアクション","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"0.45","vt":"str"},{"t":"gt","v":"0.45","vt":"str"}],"checkall":"true","outputs":2,"x":380,"y":300,"wires":[["1302ed57.eaa183"],["dcc0f34a.df6b98"]]},{"id":"a28a0f6d.a770b","type":"function","z":"6f9e4a89.ccb3cc","name":"予測結果の抽出","func":"var obj = JSON.parse(msg.payload);\nmsg.payload = obj[0].data[0][6];\nreturn msg;\n","outputs":1,"noerr":0,"x":140,"y":300,"wires":[["6b4768d7.bce78"]]},{"id":"d149a597.fb587","type":"http request","z":"6f9e4a89.ccb3cc","name":"予測モデルへの問い合わせ","method":"POST","ret":"txt","url":"https://palbyp.pmservice.ibmcloud.com/pm/v1/score/<context_id値>?accesskey=<アクセスキー>","x":660,"y":160,"wires":[["3707b134.cf97d6","a28a0f6d.a770b"]]},{"id":"fd5c586d.9ea348","type":"function","z":"6f9e4a89.ccb3cc","name":"問い合わせ値の準備","func":"msg.payload = {'tablename':'demo_data.txt','header':['ID', 'DATE', 'ACCEL', 'TEMP', 'HUMID', 'VENDOR', 'GENERATION', 'NUMPART', 'NUMJOINT', 'HOWMAINTAINED', 'RENTAL', 'STOCK', 'LEASE', 'RISK'],'data':[[100756, 44, 59944, 30, 40, 'PartUSA', 'standard', 1, 2, 'monthly', 'yes', 2, 0, 'rankA']]};\nreturn msg;\n","outputs":1,"noerr":0,"x":360,"y":160,"wires":[["33689096.b9559","d149a597.fb587"]]},{"id":"f7f5b4f4.6a1a8","type":"inject","z":"6f9e4a89.ccb3cc","name":"稼働データ","topic":"","payload":"{                 \"tablename\":\"demo_data_160615_02.txt\",                  \"header\":[\"ID\", \"DATE\", \"ACCEL\", \"TEMP\", \"HUMID\", \"VENDOR\", \"GENERATION\", \"NUMPART\", \"NUMJOINT\", \"HOWMAINTAINED\", \"RENTAL\", \"STOCK\", \"LEASE\", \"RISK\"],                  \"data\":[[100756, 44, 59944, 30, 40, \"PartUSA\", \"standard\", 1, 2, \"monthly\", \"yes\", 2, 0, \"rankA\"]]             }   ","payloadType":"json","repeat":"","crontab":"","once":false,"x":120,"y":160,"wires":[["fd5c586d.9ea348"]]},{"id":"33689096.b9559","type":"debug","z":"6f9e4a89.ccb3cc","name":"","active":false,"console":"false","complete":"false","x":600,"y":100,"wires":[]},{"id":"3707b134.cf97d6","type":"debug","z":"6f9e4a89.ccb3cc","name":"","active":false,"console":"false","complete":"false","x":780,"y":240,"wires":[]},{"id":"c8352660.937ea","type":"function","z":"6f9e4a89.ccb3cc","name":"問い合わせ値の準備","func":"msg.payload = {'tablename':'demo_data.txt','header':['ID', 'DATE', 'ACCEL', 'TEMP', 'HUMID', 'VENDOR', 'GENERATION', 'NUMPART', 'NUMJOINT', 'HOWMAINTAINED', 'RENTAL', 'STOCK', 'LEASE', 'RISK'],'data':[[100668, 35, 59692, 10, 30, 'IndiaGear', 'hybrid', 1, 1, 'monthly', 'yes', 1, 0, 'rankC']]};\nreturn msg;\n","outputs":1,"noerr":0,"x":360,"y":100,"wires":[["d149a597.fb587"]]},{"id":"88a47558.d7a788","type":"inject","z":"6f9e4a89.ccb3cc","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120,"y":100,"wires":[["c8352660.937ea"]]}]

```