監(jiān)督式學(xué)習(xí)
監(jiān)督表示你有不同的例子切平,并且你知道不同例子中的正確答案
我們給模型多個(gè)案例头谜,每個(gè)案例都有許多特征和屬性私爷,模型如果能夠挑選出正確的特征煞聪,那么就可以對(duì)新的案例進(jìn)行分類
練習(xí)
下面哪些問題可以通過監(jiān)督分類解決对雪?
□ √ 拿一冊帶標(biāo)簽的照片,試著認(rèn)出照片中的某個(gè)人
□ × 對(duì)銀行數(shù)據(jù)進(jìn)行分析米绕,尋找異常的交易瑟捣,將其標(biāo)記為涉嫌欺詐
沒有給出異常交易的明確定義,沒有例子說明它的含義
□ √ 根據(jù)某人的音樂喜好以及其所愛音樂的特點(diǎn)栅干,比如節(jié)奏或流派推薦一首他們可能會(huì)喜歡的歌
□ ×根據(jù)優(yōu)達(dá)學(xué)城學(xué)生的學(xué)習(xí)風(fēng)格迈套,將其分成不同的組群
我們不知道學(xué)生屬于什么類型,沒有已經(jīng)知道正確答案的大量樣本
特征和標(biāo)簽
在機(jī)器學(xué)習(xí)中碱鳞,我們通常將特征作為輸入桑李,然后嘗試輸出標(biāo)簽
以歌曲識(shí)別為例,輸入的特征為intensity,tempo,genre,gender,輸出的特征位like/dont like
對(duì)一個(gè)全新的數(shù)據(jù)點(diǎn)窿给,你能了解多少
特征可視化
機(jī)器學(xué)習(xí)算法可以定義決策面(決策邊界)(decision surface)
決策面通常位于兩個(gè)不同的類的之間的某個(gè)位置上贵白,
在一側(cè),算法將預(yù)測每個(gè)可能的數(shù)據(jù)點(diǎn)是那一類崩泡,
在另一側(cè)禁荒,算法將預(yù)測每個(gè)可能的數(shù)據(jù)點(diǎn)是另一類
決策面將一類和另一類分開,并且能夠泛化到之前未見過的數(shù)據(jù)點(diǎn)上
當(dāng)決策面為直線時(shí)角撞,我們稱之為線性決策面
機(jī)器學(xué)習(xí)算法所做的是獲取數(shù)據(jù)呛伴,將其轉(zhuǎn)化為決策面勃痴,決策面適用于以后所有的情況,能幫助我們確定一個(gè)新的數(shù)據(jù)點(diǎn)屬于哪一類
尋找決策面的算法:樸素貝葉斯
sklearn naive bayes/gaussian naive bayes
import numpy as np
X=np.array([[-1,-1],[-2,-1],[-3,-2],[1,1],[2,1],[3,2]])
Y=np.array([1,1,1,2,2,2])
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB() #創(chuàng)建分類器
clf.fit(X,Y) #提供訓(xùn)練數(shù)據(jù) X是特征 Y是標(biāo)簽
print(clf.predict([[-0.8,-1]])) #讓完成訓(xùn)練的分類器進(jìn)行預(yù)測 我們想知道這個(gè)特定點(diǎn)的標(biāo)簽是什么
使用fit的過程就是分類器學(xué)習(xí)模式的過程热康,然后分類器能用學(xué)得的模式進(jìn)行預(yù)測
計(jì)算 GaussianNB 準(zhǔn)確性
評(píng)估分類器效果沛申,計(jì)算分類器進(jìn)行分類的準(zhǔn)確率
確定分類器效果的指標(biāo)叫做:準(zhǔn)確率
準(zhǔn)確率:
the number of points that are classified correctly divided by the total number of points in the test set.
法一:
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
### this is slightly different than the example,
### where we just print the accuracy
### you might need to import an sklearn module
accuracy = clf.score(features_test,labels_test)
return accuracy
法二:
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(features_train,labels_train)
pred = clf.predict(features_test)
from sklearn.metrics import accuracy_score
print accuracy_score(pred,labels_test
在機(jī)器學(xué)習(xí)中,你可能要泛化在某些方面存在差別的新數(shù)據(jù)(訓(xùn)練+測試)
算法泛化新數(shù)據(jù)
在本課程中姐军,我們應(yīng)該始終執(zhí)行的操作是:
保存10%的數(shù)據(jù)铁材,將其用作測試集,你將通過這些數(shù)據(jù)真正了解你在學(xué)習(xí)數(shù)據(jù)模式方面的進(jìn)展
匯報(bào)結(jié)果時(shí)奕锌,使用測試后的結(jié)果著觉,因?yàn)樗芨谩⒏降亓私鈹?shù)據(jù)在訓(xùn)練時(shí)的表現(xiàn)
樸素貝葉斯 使我們從文本源中鑒別標(biāo)簽
之所以被稱為樸素貝葉斯歇攻,是因?yàn)樗雎粤藈ord orders
樸素貝葉斯:
優(yōu)點(diǎn):
易于執(zhí)行
特征空間非常大
運(yùn)行非常容易固惯,非常有效
缺點(diǎn):
會(huì)有間斷
由多個(gè)單詞組成且意義不明顯的短語梆造,就不太適用樸素貝葉斯
因此根據(jù)想要解決的問題和要解決的數(shù)據(jù)集缴守,我們不能把樸素貝葉斯算法看成是黑匣子,而是當(dāng)成理論性的理解镇辉,理解算法如何運(yùn)行屡穗,以及是否適合你所要解決的問題,然后我們要進(jìn)行測試忽肛,訓(xùn)練集/測試集村砂,我們檢查測試集的表現(xiàn)以及它如何運(yùn)行,如果表現(xiàn)得不盡如人意屹逛,那可能是錯(cuò)誤的算法或者是錯(cuò)誤的參數(shù)
迷你項(xiàng)目
我們有一組郵件础废,分別由同一家公司的兩個(gè)人撰寫其中半數(shù)的郵件。我們的目標(biāo)是僅根據(jù)郵件正文區(qū)分每個(gè)人寫的郵件罕模。在這個(gè)迷你項(xiàng)目一開始评腺,我們將使用樸素貝葉斯,并在之后的項(xiàng)目中擴(kuò)展至其他算法淑掌。
我們會(huì)先給你一個(gè)字符串列表蒿讥。每個(gè)字符串代表一封經(jīng)過預(yù)處理的郵件的正文;然后抛腕,我們會(huì)提供代碼芋绸,用來將數(shù)據(jù)集分解為訓(xùn)練集和測試集(在下節(jié)課中,你將學(xué)習(xí)如何進(jìn)行預(yù)處理和分解担敌,但是現(xiàn)在請(qǐng)使用我們提供的代碼)摔敛。
樸素貝葉斯特殊的一點(diǎn)在于,這種算法非常適合文本分類全封。在處理文本時(shí)舷夺,常見的做法是將每個(gè)單詞看作一個(gè)特征苦酱,這樣就會(huì)有大量的特征。此算法的相對(duì)簡單性和樸素貝葉斯獨(dú)立特征的這一假設(shè)给猾,使其能夠出色完成文本的分類疫萤。在這個(gè)迷你項(xiàng)目中,你將在計(jì)算機(jī)中下載并安裝 sklearn敢伸,然后使用樸素貝葉斯根據(jù)作者對(duì)郵件進(jìn)行分類扯饶。
-
使用 pip 安裝一系列 Python 包:
安裝 sklearn: pip install scikit-learn
此處包含 sklearn 安裝說明,可供參考
安裝自然語言工具包:pip install nltk
獲取機(jī)器學(xué)習(xí)簡介源代碼池颈。你將需要 git 來復(fù)制資源庫:git clone https://github.com/udacity/ud120-projects.git
你只需操作一次尾序,基礎(chǔ)代碼包含所有迷你項(xiàng)目的初始代碼。進(jìn)入 tools/ 目錄躯砰,運(yùn)行 startup.py每币。該程序首先檢查 python 模塊,然后下載并解壓縮我們在后期將大量使用的大型數(shù)據(jù)集琢歇。下載和解壓縮需要一些時(shí)間兰怠,但是你無需等到全部完成再開始第一部分。
startup.py代碼如下:
#!/usr/bin/python
print
print "checking for nltk"
try:
import nltk
except ImportError:
print "you should install nltk before continuing"
print "checking for numpy"
try:
import numpy
except ImportError:
print "you should install numpy before continuing"
print "checking for scipy"
try:
import scipy
except:
print "you should install scipy before continuing"
print "checking for sklearn"
try:
import sklearn
except:
print "you should install sklearn before continuing"
print
print "downloading the Enron dataset (this may take a while)"
print "to check on progress, you can cd up one level, then execute <ls -lthr>"
print "Enron dataset should be last item on the list, along with its current size"
print "download will complete at about 423 MB"
import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tar.gz"
urllib.urlretrieve(url, filename="../enron_mail_20150507.tar.gz")
print "download complete!"
print
print "unzipping Enron dataset (this may take a while)"
import tarfile
import os
os.chdir("..")
tfile = tarfile.open("enron_mail_20150507.tar.gz", "r:gz")
tfile.extractall(".")
print "you're ready to go!"
作者身份準(zhǔn)確率和對(duì)NB分類器計(jì)時(shí)
import sys
from time import time
sys.path.append("../tools/")
from email_preprocess import preprocess
'''
features_train and features_test are the features for the training
and testing datasets, respectively
labels_train and labels_test are the corresponding item labels
'''
features_train, features_test, labels_train, labels_test = preprocess()
### your code goes here ###
from sklearn.naive_bayes import GaussianNB
clf=GaussianNB()
t0 = time()
clf.fit(features_train,labels_train)
print "training time:", round(time()-t0, 3), "s" #訓(xùn)練分類器的所需時(shí)間
t0 = time()
clf.predict(features_test)
print "predicting time:", round(time()-t0, 3), "s" #分類器預(yù)測的所需時(shí)間
print clf.score(features_test,labels_test)