【Udacity】朴素贝叶斯

机器学习与朴素贝叶斯
本文通过类比葡萄酒酿造过程来解释机器学习的基本概念,并介绍了监督学习中的朴素贝叶斯算法。文中提供了如何使用Python的scikit-learn库进行数据训练及测试的示例代码,并说明了不同版本之间的API调用差异。
  • 机器学习就像酿制葡萄酒——好的葡萄(数据)+好的酿酒方法(机器学习算法)
  • 监督分类 supervised classification

  • Features ——>Labels
  • 保留10%的数据作为测试数据集

监督学习之朴素贝叶斯 Naive Bayes——寻找决策面
scikit-learn使用入门

googlesearch sklearn+Naive Bayes

关于sklearn版本
  • 视频——基于v0.17
  • 项目——基于v0.18

sklearn的现在稳定版为0.18,官方文档也升级到了0.18。但是,0.18版并不兼容0.17的代码。如果你安装了0.18版,sklearn.cross_validation, sklearn.grid_search and sklearn.learning_curve 等方法都不能直接调用。

新的API调用方法是

from sklearn.model_selection import train_test_split

计算准确度
def NB_Accuracy(features_train, labels_train, features_test, labels_test):
    
    ### 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
    ### this is slightly different than the example, 
    ### where we just print the accuracy
    ### you might need to import an sklearn module

    ### Method #1:
    accuracy = clf.score(features_test, labels_test)
    return accuracy
    ### Method #2:
    from sklearn.metrics import accuracy_score
    print accuracy_score(pred, labels_test)

转载于:https://www.cnblogs.com/Neo007/p/7594429.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值