Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
F
ffm-baseline
Project
Project
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ML
ffm-baseline
Commits
3331d70b
Commit
3331d70b
authored
Oct 15, 2018
by
高雅喆
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add node2vec gray ctr stat
parent
ab641006
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
108 additions
and
57 deletions
+108
-57
node2vec_ctr.py
eda/gray_stat/node2vec_ctr.py
+108
-57
No files found.
eda/gray_stat/node2vec_ctr.py
View file @
3331d70b
# -*- coding: UTF-8 -*-
# -*- coding: UTF-8 -*-
import
pymysql
from
utils
import
con_sql
,
get_yesterday_date
,
get_between_day
import
datetime
import
time
import
pandas
as
pd
DIRECTORY_PATH
=
"/data2/ffm
/"
OUTPUT_PATH
=
"/data2/models/eda/node2vec
/"
def
get_yesterday_date
():
#自动获取昨天的日期,如"2018-08-08"
class
GrayStat
(
object
):
def
__init__
(
self
,
cid_type
,
uid_type
,
ndays
=
get_yesterday_date
()):
"""
cid_type : diary,answer,question
uid_type : 8:_8结尾;6:_6结尾;6|8:_6或者_8结尾;^68:不是6或者8结尾的
ndays : '2018-08-30'....
"""
"""
:rtype : str
self
.
cid_type
=
cid_type
self
.
uid_type
=
uid_type
self
.
ndays
=
ndays
def
get_uid_count
(
self
):
sql
=
"select count(distinct(device_id)) from data_feed_click
\
where stat_date='{0}'
\
and (cid_type='{1}' or cid_type='diary_video')
\
and device_id regexp '[{2}]$'
\
and device_id in
\
(select device_id
\
from nd_device_cid_similarity_matrix_tmp
\
where stat_date='{0}')"
.
format
(
self
.
ndays
,
self
.
cid_type
,
self
.
uid_type
)
uid_count
=
con_sql
(
sql
)[
0
][
0
]
return
uid_count
def
get_uid_clk_times
(
self
):
sql
=
"select count(device_id) from data_feed_click
\
where stat_date='{0}'
\
and (cid_type='{1}' or cid_type='diary_video')
\
and device_id regexp '[{2}]$'
\
and device_id in
\
(select device_id
\
from nd_device_cid_similarity_matrix_tmp
\
where stat_date='{0}')"
.
format
(
self
.
ndays
,
self
.
cid_type
,
self
.
uid_type
)
uid_clk_times
=
con_sql
(
sql
)[
0
][
0
]
return
uid_clk_times
def
get_uid_imp_times
(
self
):
sql
=
"select count(device_id) from data_feed_exposure
\
where stat_date='{0}'
\
and cid_type='{1}'
\
and device_id regexp '[{2}]$'
\
and device_id in
\
(select device_id
\
from nd_device_cid_similarity_matrix_tmp
\
where stat_date='{0}')"
.
format
(
self
.
ndays
,
self
.
cid_type
,
self
.
uid_type
)
uid_imp_times
=
con_sql
(
sql
)[
0
][
0
]
return
uid_imp_times
class
AllStat
(
object
):
def
__init__
(
self
,
cid_type
,
uid_type
,
ndays
=
get_yesterday_date
()):
"""
"""
today
=
datetime
.
date
.
today
()
cid_type : diary,answer,question
yesterday
=
today
-
datetime
.
timedelta
(
days
=
1
)
uid_type : 8:_8结尾;6:_6结尾;6|8:_6或者_8结尾;^68:不是6或者8结尾的
yesterday
=
yesterday
.
strftime
(
"
%
Y-
%
m-
%
d"
)
ndays : '2018-08-30'....
print
(
yesterday
)
"""
return
yesterday
self
.
cid_type
=
cid_type
#today = datetime.date.today().strftime("%Y%m%d")
self
.
uid_type
=
uid_type
#return today
self
.
ndays
=
ndays
def
get_uid_count
(
self
):
def
get_data
():
sql
=
"select count(distinct(device_id)) from data_feed_click
\
conn2db
=
pymysql
.
connect
(
host
=
'10.66.157.22'
,
port
=
4000
,
user
=
'root'
,
passwd
=
'3SYz54LS9#^9sBvC'
,
db
=
'jerry_prod'
)
where stat_date='{0}'
\
cursor
=
conn2db
.
cursor
()
and (cid_type='{1}' or cid_type='diary_video')
\
sql
=
"select device_id from nd_device_cid_similarity_matrix where device_id regexp '[3|4]$'"
and device_id regexp '[{2}]$'"
.
format
(
self
.
ndays
,
self
.
cid_type
,
self
.
uid_type
)
cursor
.
execute
(
sql
)
uid_count
=
con_sql
(
sql
)[
0
][
0
]
result
=
cursor
.
fetchall
()
return
uid_count
device_id
=
tuple
(
pd
.
DataFrame
(
list
(
result
))[
0
]
.
values
.
tolist
())
cursor
.
close
()
def
get_uid_clk_times
(
self
):
return
device_id
sql
=
"select count(device_id) from data_feed_click
\
where stat_date='{0}'
\
and (cid_type='{1}' or cid_type='diary_video')
\
def
ctr
(
date
):
and device_id regexp '[{2}]$'"
.
format
(
self
.
ndays
,
self
.
cid_type
,
self
.
uid_type
)
device_id
=
get_data
()
uid_clk_times
=
con_sql
(
sql
)[
0
][
0
]
sql_click
=
"select count(cid) from data_feed_click "
\
return
uid_clk_times
"where cid_type = 'diary' "
\
"and stat_date = '{}' and device_id in {};"
.
format
(
date
,
device_id
)
def
get_uid_imp_times
(
self
):
db
=
pymysql
.
connect
(
host
=
'10.66.157.22'
,
port
=
4000
,
user
=
'root'
,
passwd
=
'3SYz54LS9#^9sBvC'
,
db
=
'jerry_prod'
)
sql
=
"select count(device_id) from data_feed_exposure
\
cursor
=
db
.
cursor
()
where stat_date='{0}'
\
cursor
.
execute
(
sql_click
)
and cid_type='{1}'
\
click
=
cursor
.
fetchone
()[
0
]
and device_id regexp '[{2}]$'"
.
format
(
self
.
ndays
,
self
.
cid_type
,
self
.
uid_type
)
print
(
"点击数:"
+
str
(
click
))
uid_imp_times
=
con_sql
(
sql
)[
0
][
0
]
sql_exp
=
"select count(cid) from data_feed_exposure "
\
return
uid_imp_times
"where cid_type = 'diary' and stat_date = '{}' and "
\
"device_id in {}"
.
format
(
date
,
device_id
)
cursor
.
execute
(
sql_exp
)
def
main
():
exp
=
cursor
.
fetchone
()[
0
]
date_list
=
get_between_day
(
'2018-10-11'
,
'2018-10-14'
)
print
(
"曝光数:"
+
str
(
exp
))
output
=
OUTPUT_PATH
+
"ctr.csv"
if
exp
!=
0
:
result
=
[]
print
(
"点击率:"
+
str
(
click
/
exp
))
for
my_date
in
date_list
:
g_class
=
GrayStat
(
"diary"
,
"3|4"
,
my_date
)
return
click
,
exp
,
click
/
exp
a_class
=
AllStat
(
"diary"
,
"3|4"
,
my_date
)
line1
=
str
(
g_class
.
get_uid_count
())
+
"
\t
"
+
str
(
g_class
.
get_uid_imp_times
())
+
"
\t
"
+
str
(
g_class
.
get_uid_clk_times
())
def
rate2file
():
line2
=
str
(
a_class
.
get_uid_count
())
+
"
\t
"
+
str
(
a_class
.
get_uid_imp_times
())
+
"
\t
"
+
str
(
a_class
.
get_uid_clk_times
())
output_path
=
DIRECTORY_PATH
+
"node2vec_ctr.csv"
g_ctr
=
g_class
.
get_uid_clk_times
()
/
g_class
.
get_uid_imp_times
()
with
open
(
output_path
,
'a+'
)
as
f
:
a_ctr
=
a_class
.
get_uid_clk_times
()
/
a_class
.
get_uid_imp_times
()
line
=
get_yesterday_date
()
.
replace
(
'-'
,
''
)
+
','
+
str
(
temp_data
[
0
])
+
','
+
str
(
temp_data
[
1
])
+
','
+
str
(
temp_data
[
2
])
+
'
\n
'
growth_rate
=
(
g_ctr
-
a_ctr
)
/
a_ctr
line
=
my_date
+
"
\t
"
+
str
(
round
(
g_ctr
*
100
,
2
))
+
'
%
'
+
"
\t
"
+
str
(
round
(
a_ctr
*
100
,
2
))
+
'
%
'
+
"
\t
"
+
\
str
(
round
(
growth_rate
*
100
,
2
))
+
'
%
'
+
"
\t
"
+
line1
+
"
\t
"
+
line2
+
"
\n
"
result
.
append
(
line
)
with
open
(
output
,
"a+"
)
as
f
:
for
line
in
result
:
f
.
write
(
line
)
f
.
write
(
line
)
if
__name__
==
"__main__"
:
#ctr(date)
if
__name__
==
'__main__'
:
date
=
get_yesterday_date
()
main
()
temp_data
=
ctr
(
date
)
\ No newline at end of file
rate2file
()
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