Created
November 18, 2016 16:47
-
-
Save kokumura/0ee89a3020135ddddb20107e42b41128 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
import json | |
class AzureReco: | |
BASE_URL = 'https://westus.api.cognitive.microsoft.com/recommendations/v4.0' | |
def __init__(self, subscription_key): | |
self._subscription_key = subscription_key | |
def _common_header(self): | |
return {'Ocp-Apim-Subscription-Key': self._subscription_key} | |
def get_last_build(self, model_id): | |
r = requests.get( | |
self.BASE_URL+'/models/{model_id}/builds'.format(**locals()), | |
params={'onlyLastRequestedBuild': True}, | |
headers=self._common_header(), | |
) | |
builds = r.json()['builds'] | |
return builds[0] if builds else None | |
def delete_build(self, model_id, build_id): | |
r = requests.delete( | |
self.BASE_URL+'/models/{model_id}/builds/{build_id}'.format(**locals()), | |
headers=self._common_header(), | |
) | |
def start_recommendation_build(self, model_id, recom_model_params): | |
headers = self._common_header() | |
headers['content-type'] = 'application/json' | |
params = { | |
"description": "", | |
"buildType": "recommendation", | |
"buildParameters": { | |
"recommendation": recom_model_params, | |
}, | |
} | |
r = requests.post( | |
self.BASE_URL+'/models/{model_id}/builds'.format(**locals()), | |
data=json.dumps(params), | |
headers=headers, | |
) | |
ret = r.json() | |
print(ret) | |
return ret | |
def get_build_metrics(self, model_id, build_id): | |
r = requests.get( | |
self.BASE_URL+'/models/{model_id}/builds/{build_id}/metrics'.format(**locals()), | |
headers=self._common_header(), | |
) | |
return r.json() | |
API_KEY = '__SET__YOUR__API__KEY__' | |
MODEL_ID = '__SET__YOUR__MODEL__ID__' | |
def main(): | |
model_params_base = { | |
"numberOfModelIterations": 10, | |
"numberOfModelDimensions": 10, | |
"itemCutOffLowerBound": 2, | |
"userCutOffLowerBound": 2, | |
"enableModelingInsights": True, | |
"enableU2I": True, | |
"splitterStrategy": "LastEventSplitter", | |
} | |
recoapi = AzureReco(API_KEY) | |
for num_dim in (10,20,30,40): | |
for num_iter in (10,20,30,40): | |
print('build (%d,%d)' % (num_dim, num_iter)) | |
model_params = model_params_base.copy() | |
# パラメータをセット | |
model_params.update({ | |
"numberOfModelIterations": num_iter, | |
"numberOfModelDimensions": num_dim, | |
}) | |
# ビルド開始 | |
build_id = recoapi.start_recommendation_build(MODEL_ID, model_params)['buildId'] | |
print('build_id = %d' % build_id) | |
# ビルドが完了するまで待つ | |
while True: | |
status = recoapi.get_last_build(MODEL_ID)['status'] | |
print('status = %s' % status) | |
if status in ('NotStarted', 'Running',): | |
time.sleep(30) # sleepを短くしすぎるとAPI Rate Limitでエラーになるので注意 | |
continue | |
elif status == 'Succeeded': | |
break | |
else: | |
break | |
# 評価値を取得 | |
metrics = recoapi.get_build_metrics(MODEL_ID, build_id) | |
with open('metrics/result-%d-%d.json'%(num_dim,num_iter), 'w+') as f: | |
f.write(json.dumps(metrics)) | |
# 保持できるBuildは1モデルあたり20個が上限なので、用済みになったBuildはすぐに消す。 | |
recoapi.delete_build(MODEL_ID, build_id) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jqコマンドで評価値(k=1..5)の平均を算出できる