Machine Learning机器学习 - Supervised Learning监督学习 - Native Bayes

来源:互联网 发布:c wpf 数据绑定 编辑:程序博客网 时间:2024/05/16 15:52

理论篇 Bayes Theorem

  1. 在维基百科上Bayes Theorem的解释已经很全面,特别是Drug Testing那段挺精彩,是Bayes Theorem理解的核心。
  2. 参考文章数学之美番外篇:平凡而又神奇的贝叶斯方法【中文】,有更精彩的描述。
  3. 上篇文章中提到的这篇文章How to Write a Spelling Corrector也很有启发。

实现篇 Python - 调用scikit Native Bayes库函数

def NBAccuracy(features_train, labels_train, features_test, labels_test):    """ compute the accuracy of your Naive Bayes classifier """    ### import the sklearn module for GaussianNB    from sklearn.naive_bayes import GaussianNB    ### create classifier    clf = GaussianNB()    ### fit the classifier on the training features and labels    clf.fit(features_train, labels_train)    ### use the trained classifier to predict labels for the test features    pred = clf.predict(features_test)    ### calculate and return the accuracy on the test data    from sklearn.metrics import accuracy_score    accuracy = accuracy_score(labels_test, pred)    return accuracy
0 0
原创粉丝点击