我正在 python/django 中建立一个网站,并想预测用户提交是有效的还是垃圾邮件。
用户对他们的提交有一个接受率,就像这个网站一样。
用户可以审核其他用户的提交;这些审核稍后由管理员进行元审核。
鉴于这种:
- 提交接受率为60%的注册用户A提交了一些东西。
- 用户 B 将 A 的帖子审核为有效提交。但是,用户 B 有 70% 的情况是错误的。
- 用户 C 将 A 的帖子审核为垃圾邮件。用户 C 通常是对的。如果用户 C 说某事是垃圾邮件/非垃圾邮件,那么 80% 的时间都是正确的。
如何预测 A 的帖子成为垃圾邮件的可能性?
编辑:我制作了一个模拟这种情况的python脚本:
#!/usr/bin/env python
import random
def submit(p):
"""Return 'ham' with (p*100)% probability"""
return 'ham' if random.random() < p else 'spam'
def moderate(p, ham_or_spam):
"""Moderate ham as ham and spam as spam with (p*100)% probability"""
if ham_or_spam == 'spam':
return 'spam' if random.random() < p else 'ham'
if ham_or_spam == 'ham':
return 'ham' if random.random() < p else 'spam'
NUMBER_OF_SUBMISSIONS = 100000
USER_A_HAM_RATIO = 0.6 # Will submit 60% ham
USER_B_PRECISION = 0.3 # Will moderate a submission correctly 30% of the time
USER_C_PRECISION = 0.8 # Will moderate a submission correctly 80% of the time
user_a_submissions = [submit(USER_A_HAM_RATIO) \
for i in xrange(NUMBER_OF_SUBMISSIONS)]
print "User A has made %d submissions. %d of them are 'ham'." \
% ( len(user_a_submissions), user_a_submissions.count('ham'))
user_b_moderations = [ moderate( USER_B_PRECISION, ham_or_spam) \
for ham_or_spam in user_a_submissions]
user_b_moderations_which_are_correct = \
[i for i, j in zip(user_a_submissions, user_b_moderations) if i == j]
print "User B has correctly moderated %d submissions." % \
len(user_b_moderations_which_are_correct)
user_c_moderations = [ moderate( USER_C_PRECISION, ham_or_spam) \
for ham_or_spam in user_a_submissions]
user_c_moderations_which_are_correct = \
[i for i, j in zip(user_a_submissions, user_c_moderations) if i == j]
print "User C has correctly moderated %d submissions." % \
len(user_c_moderations_which_are_correct)
i = 0
j = 0
k = 0
for a, b, c in zip(user_a_submissions, user_b_moderations, user_c_moderations):
if b == 'spam' and c == 'ham':
i += 1
if a == 'spam':
j += 1
elif a == "ham":
k += 1
print "'spam' was identified as 'spam' by user B and 'ham' by user C %d times." % j
print "'ham' was identified as 'spam' by user B and 'ham' by user C %d times." % k
print "If user B says it's spam and user C says it's ham, it will be spam \
%.2f percent of the time, and ham %.2f percent of the time." % \
( float(j)/i*100, float(k)/i*100)
运行脚本会给我这个输出:
- 用户 A 提交了 100000 次。其中60194个是“火腿”。
- 用户 B 已正确审核了 29864 个提交。
- 用户 C 已正确审核了 79990 个提交。
- “垃圾邮件”被用户 B 识别为“垃圾邮件”,被用户 C 识别为“火腿”2346 次。
- 用户 B 将“ham”识别为“垃圾邮件”,用户 C 将“ham”识别为 33634 次。
- 如果用户 B 说它是垃圾邮件,而用户 C 说它是 ham,那么 6.52% 的时间它是垃圾邮件,93.48% 的时间是 ham。
这里的概率合理吗?这是模拟场景的正确方法吗?