15-7-NaiveBayesian-recognise-junk-mail.py 592 B

1234567891011121314151617181920212223
  1. from datasets import Datasets
  2. from sklearn.model_selection import train_test_split, cross_val_score
  3. from sklearn.naive_bayes import GaussianNB
  4. # 朴素贝叶斯
  5. def main():
  6. x, y = Datasets.load_spambase()
  7. x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.3)
  8. # 训练
  9. gnb = GaussianNB()
  10. # gnb.fit(x_train, y_train)
  11. # print(gnb.score(x_test, y_test)) # 0.8204199855177408
  12. # 交叉验证
  13. sorces = cross_val_score(gnb, x, y, cv=10, scoring="accuracy")
  14. print(sorces.mean()) # 0.8217730830896915
  15. if __name__ == "__main__":
  16. main()