Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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()
@kokumura
Copy link
Author

kokumura commented Nov 19, 2016

jqコマンドで評価値(k=1..5)の平均を算出できる

cat metrics/result-*.json | jq '[.precisionUserRecommend.precisionMetrics[].percentage]|add/5'
cat metrics/result-*.json | jq '[.precisionItemRecommend.precisionMetrics[].percentage]|add/5'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment