ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 概述 在实际应用中,这些概率通常是从训练数据中计算得出的,而不是手动设置。上述示例是一个简化的演示,实际情况下朴素贝叶斯分类器可以包括更多特征和更复杂的计算。 ```python # 词汇表 vocabulary = {'money': 0.8, 'free': 0.7, 'click': 0.6, 'meeting': 0.2} def classify_email(email_text): # 将邮件文本分词 words = email_text.split() # 初始化概率 spam_probability = 1.0 non_spam_probability = 1.0 # 计算概率 for word in words: if word in vocabulary: # 计算是垃圾邮箱的概率 spam_probability *= vocabulary[word] # 计算不是垃圾邮箱的概率 non_spam_probability *= 1 - vocabulary[word] # 根据概率比较判断 if spam_probability > non_spam_probability: return "垃圾邮件" else: return "非垃圾邮件" # 示例邮件 email_example = "Get free money now!" # 分类 result = classify_email(email_example) print(f"The email is classified as: {result}") # The email is classified as: 垃圾邮件 ```