国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - 吳恩達(dá)機(jī)器學(xué)習(xí)練習(xí):SVM支持向量機(jī)

吳恩達(dá)機(jī)器學(xué)習(xí)練習(xí):SVM支持向量機(jī)

2021-10-10 23:44Cowry5 Python

這篇文章主要為我們帶來了吳恩達(dá)機(jī)器學(xué)習(xí)的一個(gè)練習(xí):SVM支持向量機(jī),通過本次練習(xí)相信你能對(duì)機(jī)器學(xué)習(xí)深入更進(jìn)一步,需要的朋友可以參考下

1 Support Vector Machines

1.1 Example Dataset 1

%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
from scipy.io import loadmat
from sklearn import svm

大多數(shù)SVM的庫會(huì)自動(dòng)幫你添加額外的特征X?已經(jīng)θ?,所以無需手動(dòng)添加

mat = loadmat("./data/ex6data1.mat")
print(mat.keys())
# dict_keys(["__header__", "__version__", "__globals__", "X", "y"])
X = mat["X"]
y = mat["y"]
def plotData(X, y):
    plt.figure(figsize=(8,5))
    plt.scatter(X[:,0], X[:,1], c=y.flatten(), cmap="rainbow")
    plt.xlabel("X1")
    plt.ylabel("X2")
    plt.legend() 
plotData(X, y)

吳恩達(dá)機(jī)器學(xué)習(xí)練習(xí):SVM支持向量機(jī)

def plotBoundary(clf, X):
    """plot decision bondary"""
    x_min, x_max = X[:,0].min()*1.2, X[:,0].max()*1.1
    y_min, y_max = X[:,1].min()*1.1,X[:,1].max()*1.1
    xx, yy = np.meshgrid(np.linspace(x_min, x_max, 500),
                         np.linspace(y_min, y_max, 500))
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    plt.contour(xx, yy, Z)
models = [svm.SVC(C, kernel="linear") for C in [1, 100]]
clfs = [model.fit(X, y.ravel()) for model in models]
title = ["SVM Decision Boundary with C = {} (Example Dataset 1".format(C) for C in [1, 100]]
for model,title in zip(clfs,title):
    plt.figure(figsize=(8,5))
    plotData(X, y)
    plotBoundary(model, X)
    plt.title(title)

吳恩達(dá)機(jī)器學(xué)習(xí)練習(xí):SVM支持向量機(jī)

吳恩達(dá)機(jī)器學(xué)習(xí)練習(xí):SVM支持向量機(jī)

可以從上圖看到,當(dāng)C比較小時(shí)模型對(duì)誤分類的懲罰增大,比較嚴(yán)格,誤分類少,間隔比較狹窄。

當(dāng)C比較大時(shí)模型對(duì)誤分類的懲罰增大,比較寬松,允許一定的誤分類存在,間隔較大。

1.2 SVM with Gaussian Kernels

這部分,使用SVM做非線性分類。我們將使用高斯核函數(shù)。

為了用SVM找出一個(gè)非線性的決策邊界,我們首先要實(shí)現(xiàn)高斯核函數(shù)。我可以把高斯核函數(shù)想象成一個(gè)相似度函數(shù),用來測(cè)量一對(duì)樣本的距離,(x ? ? ?,y ? ? ?)

吳恩達(dá)機(jī)器學(xué)習(xí)練習(xí):SVM支持向量機(jī)

這里我們用sklearn自帶的svm中的核函數(shù)即可。

1.2.1 Gaussian Kernel

def gaussKernel(x1, x2, sigma):
    return np.exp(- ((x1 - x2) ** 2).sum() / (2 * sigma ** 2))
gaussKernel(np.array([1, 2, 1]),np.array([0, 4, -1]), 2.)  # 0.32465246735834974

1.2.2 Example Dataset 2

mat = loadmat("./data/ex6data2.mat")
X2 = mat["X"]
y2 = mat["y"]

plotData(X2, y2)

吳恩達(dá)機(jī)器學(xué)習(xí)練習(xí):SVM支持向量機(jī)

sigma = 0.1
gamma = np.power(sigma,-2.)/2
clf = svm.SVC(C=1, kernel="rbf", gamma=gamma)
modle = clf.fit(X2, y2.flatten())
plotData(X2, y2)
plotBoundary(modle, X2)

吳恩達(dá)機(jī)器學(xué)習(xí)練習(xí):SVM支持向量機(jī)

1.2.3 Example Dataset 3

mat3 = loadmat("data/ex6data3.mat")
X3, y3 = mat3["X"], mat3["y"]
Xval, yval = mat3["Xval"], mat3["yval"]
plotData(X3, y3)

吳恩達(dá)機(jī)器學(xué)習(xí)練習(xí):SVM支持向量機(jī)

Cvalues = (0.01, 0.03, 0.1, 0.3, 1., 3., 10., 30.)
sigmavalues = Cvalues
best_pair, best_score = (0, 0), 0
for C in Cvalues:
    for sigma in sigmavalues:
        gamma = np.power(sigma,-2.)/2
        model = svm.SVC(C=C,kernel="rbf",gamma=gamma)
        model.fit(X3, y3.flatten())
        this_score = model.score(Xval, yval)
        if this_score > best_score:
            best_score = this_score
            best_pair = (C, sigma)
print("best_pair={}, best_score={}".format(best_pair, best_score))
# best_pair=(1.0, 0.1), best_score=0.965
model = svm.SVC(C=1., kernel="rbf", gamma = np.power(.1, -2.)/2)
model.fit(X3, y3.flatten())
plotData(X3, y3)
plotBoundary(model, X3)

吳恩達(dá)機(jī)器學(xué)習(xí)練習(xí):SVM支持向量機(jī)

# 這我的一個(gè)練習(xí)畫圖的,和作業(yè)無關(guān),給個(gè)畫圖的參考。
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
# we create 40 separable points
np.random.seed(0)
X = np.array([[3,3],[4,3],[1,1]])
Y = np.array([1,1,-1])
# fit the model
clf = svm.SVC(kernel="linear")
clf.fit(X, Y)
# get the separating hyperplane
w = clf.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(-5, 5)
yy = a * xx - (clf.intercept_[0]) / w[1]
# plot the parallels to the separating hyperplane that pass through the
# support vectors
b = clf.support_vectors_[0]
yy_down = a * xx + (b[1] - a * b[0])
b = clf.support_vectors_[-1]
yy_up = a * xx + (b[1] - a * b[0])
# plot the line, the points, and the nearest vectors to the plane
plt.figure(figsize=(8,5))
plt.plot(xx, yy, "k-")
plt.plot(xx, yy_down, "k--")
plt.plot(xx, yy_up, "k--")
# 圈出支持向量
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
            s=150, facecolors="none", edgecolors="k", linewidths=1.5)
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.rainbow)
plt.axis("tight")
plt.show()
print(clf.decision_function(X))

吳恩達(dá)機(jī)器學(xué)習(xí)練習(xí):SVM支持向量機(jī)

[ 1. 1.5 -1. ]

2 Spam Classification

2.1 Preprocessing Emails

這部分用SVM建立一個(gè)垃圾郵件分類器。你需要將每個(gè)email變成一個(gè)n維的特征向量,這個(gè)分類器將判斷給定一個(gè)郵件x是垃圾郵件(y=1)或不是垃圾郵件(y=0)。

take a look at examples from the dataset

with open("data/emailSample1.txt", "r") as f:
    email = f.read()
    print(email)
> Anyone knows how much it costs to host a web portal ?
>
Well, it depends on how many visitors you"re expecting.
This can be anywhere from less than 10 bucks a month to a couple of $100. 
You should checkout http://www.rackspace.com/ or perhaps Amazon EC2 
if youre running something big..
To unsubscribe yourself from this mailing list, send an email to:
groupname-unsubscribe@egroups.com

可以看到,郵件內(nèi)容包含 a URL, an email address(at the end), numbers, and dollar amounts. 很多郵件都會(huì)包含這些元素,但是每封郵件的具體內(nèi)容可能會(huì)不一樣。因此,處理郵件經(jīng)常采用的方法是標(biāo)準(zhǔn)化這些數(shù)據(jù),把所有URL當(dāng)作一樣,所有數(shù)字看作一樣。

例如,我們用唯一的一個(gè)字符串‘httpaddr"來替換所有的URL,來表示郵件包含URL,而不要求具體的URL內(nèi)容。這通常會(huì)提高垃圾郵件分類器的性能,因?yàn)槔]件發(fā)送者通常會(huì)隨機(jī)化URL,因此在新的垃圾郵件中再次看到任何特定URL的幾率非常小。

我們可以做如下處理:

 1. Lower-casing: 把整封郵件轉(zhuǎn)化為小寫。
  2. Stripping HTML: 移除所有HTML標(biāo)簽,只保留內(nèi)容。
  3. Normalizing URLs: 將所有的URL替換為字符串 “httpaddr”.
  4. Normalizing Email Addresses: 所有的地址替換為 “emailaddr”
  5. Normalizing Dollars: 所有dollar符號(hào)($)替換為“dollar”.
  6. Normalizing Numbers: 所有數(shù)字替換為“number”
  7. Word Stemming(詞干提取): 將所有單詞還原為詞源。例如,“discount”, “discounts”, “discounted” and “discounting”都替換為“discount”。
  8. Removal of non-words: 移除所有非文字類型,所有的空格(tabs, newlines, spaces)調(diào)整為一個(gè)空格.
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
from sklearn import svm
import re #regular expression for e-mail processing
# 這是一個(gè)可用的英文分詞算法(Porter stemmer)
from stemming.porter2 import stem
# 這個(gè)英文算法似乎更符合作業(yè)里面所用的代碼,與上面效果差不多
import nltk, nltk.stem.porter
def processEmail(email):
    """做除了Word Stemming和Removal of non-words的所有處理"""
    email = email.lower()
    email = re.sub("<[^<>]>", " ", email)  # 匹配<開頭,然后所有不是< ,> 的內(nèi)容,知道>結(jié)尾,相當(dāng)于匹配<...>
    email = re.sub("(http|https)://[^s]*", "httpaddr", email )  # 匹配//后面不是空白字符的內(nèi)容,遇到空白字符則停止
    email = re.sub("[^s]+@[^s]+", "emailaddr", email)
    email = re.sub("[$]+", "dollar", email)
    email = re.sub("[d]+", "number", email) 
    return email

接下來就是提取詞干,以及去除非字符內(nèi)容。

def email2TokenList(email):
    """預(yù)處理數(shù)據(jù),返回一個(gè)干凈的單詞列表"""
    # I"ll use the NLTK stemmer because it more accurately duplicates the
    # performance of the OCTAVE implementation in the assignment
    stemmer = nltk.stem.porter.PorterStemmer()
    email = preProcess(email)
    # 將郵件分割為單個(gè)單詞,re.split() 可以設(shè)置多種分隔符
    tokens = re.split("[ @$/#.-:&*+=[]?!(){},"">\_<;\%]", email)
    # 遍歷每個(gè)分割出來的內(nèi)容
    tokenlist = []
    for token in tokens:
        # 刪除任何非字母數(shù)字的字符
        token = re.sub("[^a-zA-Z0-9]", "", token);
        # Use the Porter stemmer to 提取詞根
        stemmed = stemmer.stem(token)
        # 去除空字符串‘",里面不含任何字符
        if not len(token): continue
        tokenlist.append(stemmed)
    return tokenlist  

2.1.1 Vocabulary List(詞匯表)

在對(duì)郵件進(jìn)行預(yù)處理之后,我們有一個(gè)處理后的單詞列表。下一步是選擇我們想在分類器中使用哪些詞,我們需要去除哪些詞。

我們有一個(gè)詞匯表vocab.txt,里面存儲(chǔ)了在實(shí)際中經(jīng)常使用的單詞,共1899個(gè)。

我們要算出處理后的email中含有多少vocab.txt中的單詞,并返回在vocab.txt中的index,這就我們想要的訓(xùn)練單詞的索引。

def email2VocabIndices(email, vocab):
    """提取存在單詞的索引"""
    token = email2TokenList(email)
    index = [i for i in range(len(vocab)) if vocab[i] in token ]
    return index

2.2 Extracting Features from Emails

def email2FeatureVector(email):
    """
    將email轉(zhuǎn)化為詞向量,n是vocab的長度。存在單詞的相應(yīng)位置的值置為1,其余為0
    """
    df = pd.read_table("data/vocab.txt",names=["words"])
    vocab = df.as_matrix()  # return array
    vector = np.zeros(len(vocab))  # init vector
    vocab_indices = email2VocabIndices(email, vocab)  # 返回含有單詞的索引
    # 將有單詞的索引置為1
    for i in vocab_indices:
        vector[i] = 1
    return vector
vector = email2FeatureVector(email)
print("length of vector = {}
num of non-zero = {}".format(len(vector), int(vector.sum())))

length of vector = 1899

num of non-zero = 45

2.3 Training SVM for Spam Classification

讀取已經(jīng)訓(xùn)提取好的特征向量以及相應(yīng)的標(biāo)簽。分訓(xùn)練集和測(cè)試集。

# Training set
mat1 = loadmat("data/spamTrain.mat")
X, y = mat1["X"], mat1["y"]
# Test set
mat2 = scipy.io.loadmat("data/spamTest.mat")
Xtest, ytest = mat2["Xtest"], mat2["ytest"]
clf = svm.SVC(C=0.1, kernel="linear")
clf.fit(X, y)

2.4 Top Predictors for Spam

predTrain = clf.score(X, y)
predTest = clf.score(Xtest, ytest)
predTrain, predTest

(0.99825, 0.989)

到此這篇關(guān)于機(jī)器學(xué)習(xí)SVM支持向量機(jī)的練習(xí)文章就介紹到這了,更多相關(guān)機(jī)器學(xué)習(xí)內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/Cowry5/article/details/80465922

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 在线亚洲一区 | 精品免费国产一区二区三区四区 | 午夜免费电影 | 国产黄色av网站 | 北条麻妃99 | 国产精品久久久久久久久久久新郎 | 欧美成人激情视频 | 91视频免费在线看 | 91色在线视频 | 国产成人久久一区二区三区 | 日韩成人在线播放 | 久久久久久亚洲 | 久久精品在线 | 免费观看av | 国产一区二区三区免费 | 成人免费一区二区三区视频网站 | 久久视频一区 | 99久久久无码国产精品 | 亚洲va中文字幕 | 亚洲综合色视频在线观看 | 依人九九宗合九九九 | 伊人99 | 超色视频在线观看 | 在线观看日韩 | 夜夜骚av | 久久久久亚洲精品 | 免费成人在线视频网站 | 99在线观看 | 色婷婷综合网 | 成人免费一区二区三区视频网站 | 免费一级毛片在线播放放视频 | 成人永久免费视频 | 精品国产乱码久久久久久久 | 欧美激情一区 | 欧美成人a| 国产精品久久久久久亚洲调教 | 97精品国产97久久久久久免费 | 一级毛片黄 | 日韩av成人 | 国产精品久久久久无码av | 亚洲精品久久久久久国产精华液 |