Commit 321b7e4c by 王金柱

更新xgboost模型

parent aaa313b4
...@@ -270,24 +270,22 @@ class RecommendByXgboost(Recommender): ...@@ -270,24 +270,22 @@ class RecommendByXgboost(Recommender):
s_u_profile_time = time.time() s_u_profile_time = time.time()
user_profile = self.get_user_profile(user_id) user_profile = self.get_user_profile(user_id)
logger.info('s_u_profile_time: {} '.format(time.time()-s_u_profile_time))
if not user_profile: if not user_profile:
return self._recommend_top(size) return self._recommend_top(size)
recall_start = time.time() recall_start = time.time()
# recommend_result = self.recommender.recommend(user_id, size=size, is_merge=True) # recommend_result = self.recommender.recommend(user_id, size=size, is_merge=True)
recommend_result = None recommend_result = None
logger.info('recall call time:{}'.format(time.time()-recall_start)) logger.info('recall call time:{}ms'.format(int((time.time()-recall_start)*1000)))
data_time = time.time() data_time = time.time()
predit_data = self.trans_feature_data(user_id, recommend_result) predit_data = self.trans_feature_data(user_id, recommend_result)
logger.info('data_time: {}'.format(time.time()-data_time)) logger.info('data_time: {}ms'.format(int((time.time()-data_time)*1000)))
doctor_ids = predit_data.pop('doctor_id') doctor_ids = predit_data.pop('doctor_id')
doctor_ids = doctor_ids.to_numpy() doctor_ids = doctor_ids.to_numpy()
pre_time = time.time() pre_time = time.time()
predit_result = self.model.predict_proba(predit_data)[:, 1] predit_result = self.model.predict_proba(predit_data)[:, 1]
logger.info('predit_time:{}'.format(time.time()-pre_time)) logger.info('predit_time:{}ms'.format(int((time.time()-pre_time)*1000)))
result_dict = dict(zip(doctor_ids, predit_result)) result_dict = dict(zip(doctor_ids, predit_result))
result_dict = sorted(result_dict.items(), key=lambda x:x[1], reverse=True) result_dict = sorted(result_dict.items(), key=lambda x:x[1], reverse=True)
recommend_data = [{ recommend_data = [{
...@@ -349,7 +347,7 @@ class RecommendByXgboost(Recommender): ...@@ -349,7 +347,7 @@ class RecommendByXgboost(Recommender):
try: try:
get_profile_time = time.time() get_profile_time = time.time()
response = requests.request('POST', self.dmp_url, headers=headers, json=payload) response = requests.request('POST', self.dmp_url, headers=headers, json=payload)
self.logger.info(' get user profile cost {} ms'.format((time.time()-get_profile_time)*1000)) logger.info('get user profile cost: {}ms'.format(int((time.time()-get_profile_time)*1000)))
resp = response.json() resp = response.json()
return resp['data']['objects'][0] return resp['data']['objects'][0]
except Exception as e: except Exception as e:
...@@ -391,20 +389,20 @@ class RecommendByXgboost(Recommender): ...@@ -391,20 +389,20 @@ class RecommendByXgboost(Recommender):
if __name__ == '__main__': if __name__ == '__main__':
s_time = time.time() # s_time = time.time()
recommender1 = UserCFRecommender() # recommender1 = UserCFRecommender()
recommender1.recommend('30004410') # recommender1.recommend('30004410')
print('all cost time: {}'.format(time.time() - s_time), recommender1.recommend('12047')) # print('all cost time: {}'.format(time.time() - s_time), recommender1.recommend('12047'))
print() print()
print() print()
s_time = time.time() s_time = time.time()
recommender = RecommendByXgboost() recommender = RecommendByXgboost()
recommender.recommend('30004410') recommender.recommend('12047')
print('all cost time: '.format(time.time()-s_time), recommender.recommend('12047')) print('all cost time: '.format(time.time()-s_time), recommender.recommend('12047'))
print() # print()
print() # print()
s_time = time.time() # s_time = time.time()
recommender.recommend('30004410') # recommender.recommend('30004410')
print('all cost time: '.format(time.time() - s_time), recommender.recommend('12047')) # print('all cost time: '.format(time.time() - s_time), recommender.recommend('12047'))
\ No newline at end of file \ No newline at end of file
...@@ -13,11 +13,12 @@ from tornado.concurrent import run_on_executor ...@@ -13,11 +13,12 @@ from tornado.concurrent import run_on_executor
from ydl_ai_recommender.src.utils.log import create_logger from ydl_ai_recommender.src.utils.log import create_logger
from ydl_ai_recommender.src.core.recommender import UserCFRecommender from ydl_ai_recommender.src.core.recommender import UserCFRecommender
from ydl_ai_recommender.src.core.recommender import RecommendByXgboost
logger = create_logger(__name__, 'service.log', is_rotating=True) logger = create_logger(__name__, 'service.log', is_rotating=True)
recommender = UserCFRecommender(top_n=2, k=50, u2c='order') recommender = UserCFRecommender(top_n=2, k=50, u2c='order')
recommenderByXgb = RecommendByXgboost()
class RecommendHandler(tornado.web.RequestHandler): class RecommendHandler(tornado.web.RequestHandler):
executor = ThreadPoolExecutor(1) executor = ThreadPoolExecutor(1)
...@@ -112,7 +113,7 @@ class RecommendXgbHandler(tornado.web.RequestHandler): ...@@ -112,7 +113,7 @@ class RecommendXgbHandler(tornado.web.RequestHandler):
logger.info('request@@uid=%s@@size=%s', uid, size) logger.info('request@@uid=%s@@size=%s', uid, size)
try: try:
start_time = time.time() start_time = time.time()
recommend_result = recommender.recommend(uid, size=size, is_merge=True) recommend_result = recommenderByXgb.recommend(uid, size=size, is_merge=True)
logger.info('request@@uid=%s@@size=%s, cost %s ms', uid, size, (time.time()-start_time)*1000) logger.info('request@@uid=%s@@size=%s, cost %s ms', uid, size, (time.time()-start_time)*1000)
ret = { ret = {
'status': 'success', 'status': 'success',
...@@ -139,7 +140,7 @@ if __name__ == '__main__': ...@@ -139,7 +140,7 @@ if __name__ == '__main__':
tornado.options.parse_command_line() tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r'/ai_counselor_recommend', RecommendHandler), app = tornado.web.Application(handlers=[(r'/ai_counselor_recommend', RecommendHandler),
(r'/ai_counselor_recommend/xgb/v1', RecommendXgbHandler)] (r'/ai_counselor_recommend/xgb/v1', RecommendXgbHandler)]
, autoreload=False, debug=False) , autoreload=True, debug=False)
http_server = tornado.httpserver.HTTPServer(app) http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(tornado.options.options.port) http_server.listen(tornado.options.options.port)
tornado.ioloop.IOLoop.instance().start() tornado.ioloop.IOLoop.instance().start()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment