Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
ydl_ai_recommender
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
闫发泽
ydl_ai_recommender
Commits
9121e5a1
Commit
9121e5a1
authored
May 17, 2023
by
王金柱
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新xgboost模型预测过程中各阶段的耗时统计
parent
321b7e4c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
15 deletions
+31
-15
recommender.py
src/core/recommender.py
+31
-15
No files found.
src/core/recommender.py
View file @
9121e5a1
...
...
@@ -25,6 +25,18 @@ from ydl_ai_recommender.src.utils.log import create_logger
logger
=
create_logger
(
__name__
,
'service_xgb.log'
,
is_rotating
=
True
)
def
cost_time
(
desc
):
def
the_func_cost_time
(
func
):
def
fun
(
*
args
,
**
kwargs
):
t
=
time
.
perf_counter
()
result
=
func
(
*
args
,
**
kwargs
)
logger
.
info
(
'函数:{},{} 耗时:{} ms'
.
format
(
str
(
func
.
__name__
),
desc
,
round
((
time
.
perf_counter
()
-
t
)
*
1000
,
2
)))
return
result
return
fun
return
the_func_cost_time
class
Recommender
():
def
__init__
(
self
)
->
None
:
...
...
@@ -266,20 +278,22 @@ class RecommendByXgboost(Recommender):
self
.
model
=
xgb
.
XGBClassifier
(
objective
=
'binary:logistic'
,
nthread
=-
1
,
**
self
.
params
)
self
.
model
.
load_model
(
os
.
path
.
join
(
get_project_path
(),
'model_data/xgb_model.bin'
))
@cost_time
(
desc
=
''
)
def
recall_data
(
self
,
user_id
,
size
=
0
,
is_merge
=
True
):
return
self
.
recommender
.
recommend
(
user_id
,
size
=
size
,
is_merge
=
True
)
@cost_time
(
desc
=
'模型推荐整个流程'
)
def
recommend
(
self
,
user_id
,
size
=
0
,
is_merge
=
True
):
s_u_profile_time
=
time
.
time
()
user_profile
=
self
.
get_user_profile
(
user_id
)
if
not
user_profile
:
return
self
.
_recommend_top
(
size
)
recall_start
=
time
.
time
()
# recommend_result = self.recommender.recommend(user_id, size=size, is_merge=True)
recommend_result
=
None
logger
.
info
(
'recall call time:{}ms'
.
format
(
int
((
time
.
time
()
-
recall_start
)
*
1000
)))
# recommend_result = self.recall_data(user_id, size=size, is_merge=True)
recommend_result
=
None
data_time
=
time
.
time
()
predit_data
=
self
.
trans_feature_data
(
user_id
,
recommend_result
)
logger
.
info
(
'data_time: {}ms'
.
format
(
int
((
time
.
time
()
-
data_time
)
*
1000
)))
predit_data
=
self
.
trans_feature_data
(
user_id
,
user_profile
,
recommend_result
)
doctor_ids
=
predit_data
.
pop
(
'doctor_id'
)
doctor_ids
=
doctor_ids
.
to_numpy
()
...
...
@@ -290,13 +304,14 @@ class RecommendByXgboost(Recommender):
result_dict
=
sorted
(
result_dict
.
items
(),
key
=
lambda
x
:
x
[
1
],
reverse
=
True
)
recommend_data
=
[{
'counselor'
:
int
(
c_id
),
'score'
:
float
(
proba
),
'score'
:
round
(
float
(
proba
),
4
),
'from'
:
'similar_users {}'
.
format
(
user_id
),
}
for
(
c_id
,
proba
)
in
result_dict
[
0
:
50
]]
return
recommend_data
def
trans_feature_data
(
self
,
user_id
,
counselor_data
):
user_feature_data
=
self
.
trans_user_feature_data
(
user_id
)
@cost_time
(
desc
=
''
)
def
trans_feature_data
(
self
,
user_id
,
user_profile
,
counselor_data
):
user_feature_data
=
self
.
trans_user_feature_data
(
user_id
,
user_profile
)
counselor_feature_data
=
self
.
trans_counselor_feature_data
(
counselor_data
)
counselor_num
=
len
(
counselor_feature_data
)
...
...
@@ -307,9 +322,9 @@ class RecommendByXgboost(Recommender):
predit_feature_data
=
pd
.
concat
([
user_feature_data_dataframe
,
counselor_feature_data
],
axis
=
1
)
return
predit_feature_data
def
trans_user_feature_data
(
self
,
user_id
):
user_profile
=
self
.
get_user_profile
(
user_id
)
def
trans_user_feature_data
(
self
,
user_id
,
user_profile
):
if
not
user_profile
:
user_profile
=
self
.
get_user_profile
(
user_id
)
from_login_encoder
=
self
.
get_encoder_from_dict
(
'ffrom_login'
,
user_profile
[
'ffrom_login'
])
user_login_city_encoder
=
self
.
get_encoder_from_dict
(
'user_login_city'
,
user_profile
[
'user_login_city'
])
...
...
@@ -327,6 +342,7 @@ class RecommendByXgboost(Recommender):
# return counselor_profiles
return
self
.
all_counselors
@cost_time
(
desc
=
'获取用户画像'
)
def
get_user_profile
(
self
,
user_id
):
if
user_id
==
'0'
:
return
[]
...
...
@@ -347,7 +363,6 @@ class RecommendByXgboost(Recommender):
try
:
get_profile_time
=
time
.
time
()
response
=
requests
.
request
(
'POST'
,
self
.
dmp_url
,
headers
=
headers
,
json
=
payload
)
logger
.
info
(
'get user profile cost: {}ms'
.
format
(
int
((
time
.
time
()
-
get_profile_time
)
*
1000
)))
resp
=
response
.
json
()
return
resp
[
'data'
][
'objects'
][
0
]
except
Exception
as
e
:
...
...
@@ -398,11 +413,11 @@ if __name__ == '__main__':
print
()
s_time
=
time
.
time
()
recommender
=
RecommendByXgboost
()
recommender
.
recommend
(
'
1204
7'
)
recommender
.
recommend
(
'
325122
7'
)
print
(
'all cost time: '
.
format
(
time
.
time
()
-
s_time
),
recommender
.
recommend
(
'12047'
))
# print()
# print()
# s_time = time.time()
# recommender.recommend('3
0004410
')
# recommender.recommend('3
7298
')
# print('all cost time: '.format(time.time() - s_time), recommender.recommend('12047'))
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment