自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何使用支持向量機學習非線性數(shù)據(jù)集

人工智能 機器學習
什么是支持向量機呢?支持向量機是監(jiān)督機器學習模型,可對數(shù)據(jù)進行分類分析。實際上,支持向量機算法是尋找能將實例進行分離的優(yōu)秀超平面的過程。

 支持向量機(SVM)

[[326874]]

什么是支持向量機呢?支持向量機是監(jiān)督機器學習模型,可對數(shù)據(jù)進行分類分析。實際上,支持向量機算法是尋找能將實例進行分離的優(yōu)秀超平面的過程。

 

如何使用支持向量機學習非線性數(shù)據(jù)集

 

如果數(shù)據(jù)像上面那樣是線性可分離的,那么我們用一個線性分類器就能將兩個類分開。如果我們的數(shù)據(jù)是非線性可分的,我們應該怎么做呢?就像這樣:

 

如何使用支持向量機學習非線性數(shù)據(jù)集

 

正如我們所看到的,即使來自不同類的數(shù)據(jù)點是可分離的,我們也不能簡單地畫一條直線來進行分類。

 

如何使用支持向量機學習非線性數(shù)據(jù)集

 

那么我們?nèi)绾问褂弥С窒蛄繖C來擬合非線性機器學習數(shù)據(jù)集呢?

使用SVM進行實驗

創(chuàng)建機器學習數(shù)據(jù)集

首先創(chuàng)建非線性機器學習數(shù)據(jù)集。Python代碼如下:

 

  1. # Import packages to visualize the classifer 
  2. from matplotlib.colors import ListedColormap 
  3. import matplotlib.pyplot as plt 
  4. import warnings 
  5.  
  6. # Import packages to do the classifying 
  7. import numpy as np 
  8. from sklearn.svm import SVC 
  9.  
  10. Create Dataset 
  11. np.random.seed(0) 
  12. X_xor = np.random.randn(200, 2) 
  13. y_xor = np.logical_xor(X_xor[:, 0] > 0, 
  14.                        X_xor[:, 1] > 0) 
  15. y_xor = np.where(y_xor, 1, -1) 
  16.  
  17. fig = plt.figure(figsize=(10,10)) 
  18. plt.scatter(X_xor[y_xor == 1, 0], 
  19.             X_xor[y_xor == 1, 1], 
  20.             c='b', marker='x'
  21.             label='1'
  22. plt.scatter(X_xor[y_xor == -1, 0], 
  23.             X_xor[y_xor == -1, 1], 
  24.             c='r'
  25.             marker='s'
  26.             label='-1'
  27.  
  28. plt.xlim([-3, 3]) 
  29. plt.ylim([-3, 3]) 
  30. plt.legend(loc='best'
  31. plt.tight_layout() 
  32. plt.show() 

 

如何使用支持向量機學習非線性數(shù)據(jù)集

 

 

嘗試使用線性支持向量機

我們首先嘗試使用線性支持向量機,Python實現(xiàn)如下:

 

  1. # Import packages to do the classifying 
  2. from mlxtend.plotting import plot_decision_regions 
  3. import numpy as np 
  4. from sklearn.svm import SVC 
  5.  
  6. Create a SVC classifier using a linear kernel 
  7. svm = SVC(kernel='linear', C=1000, random_state=0) 
  8. # Train the classifier 
  9. svm.fit(X_xor, y_xor) 
  10.  
  11. # Visualize the decision boundaries 
  12. fig = plt.figure(figsize=(10,10)) 
  13. plot_decision_regions(X_xor, y_xor, clf=svm) 
  14. plt.legend(loc='upper left'
  15. plt.tight_layout() 
  16. plt.show() 

C是與錯誤分類相關的成本。C值越高,算法對數(shù)據(jù)集的正確分離就越嚴格。對于線性分類器,我們使用kernel='linear'。

 

如何使用支持向量機學習非線性數(shù)據(jù)集

 

如我們所見,即使我們將成本設置得很高,但這條線也無法很好地分離紅點和藍點。

徑向基函數(shù)核

到目前為止,我們使用的線性分類器為:

 

如何使用支持向量機學習非線性數(shù)據(jù)集

 

正如我們所看到的,g(x)是一個線性函數(shù)。當g(x) >為0時,預測值為1。當g(x) <0時,預測值為-1。但是由于我們不能使用線性函數(shù)處理像上面這樣的非線性數(shù)據(jù),我們需要將線性函數(shù)轉換成另一個函數(shù)。

 

如何使用支持向量機學習非線性數(shù)據(jù)集

 

這個分類器似乎是我們非線性數(shù)據(jù)的理想選擇。讓我們來看看Python的代碼:

 

  1. Create a SVC classifier using an RBF kernel 
  2. svm = SVC(kernel='rbf', random_state=0, gamma=1/100, C=1) 
  3. # Train the classifier 
  4. svm.fit(X_xor, y_xor) 
  5.  
  6. # Visualize the decision boundaries 
  7. fig = plt.figure(figsize=(10,10)) 
  8. plot_decision_regions(X_xor, y_xor, clf=svm) 
  9. plt.legend(loc='upper left'
  10. plt.tight_layout() 
  11. plt.show() 

gamma是1 / sigma。請記住,sigma是調(diào)節(jié)函數(shù)。因此,gamma值越小,sigma值就越大,分類器對各個點之間的距離就越不敏感。

 

如何使用支持向量機學習非線性數(shù)據(jù)集

 

讓我們把伽瑪放大看看會發(fā)生什么

 

  1. Create a SVC classifier using an RBF kernel 
  2. svm = SVC(kernel='rbf', random_state=0, gamma=1, C=1) 
  3. # Train the classifier 
  4. svm.fit(X_xor, y_xor) 
  5.  
  6. # Visualize the decision boundaries 
  7. fig = plt.figure(figsize=(10,10)) 
  8. plot_decision_regions(X_xor, y_xor, clf=svm) 
  9. plt.legend(loc='upper left'
  10. plt.tight_layout() 
  11. plt.show() 

 

如何使用支持向量機學習非線性數(shù)據(jù)集

 

 

好像將伽瑪值提高100倍可以提高分類器對訓練集的準確性。把伽馬值再乘以10會怎么樣呢?

 

  1. Create a SVC classifier using an RBF kernel 
  2. svm = SVC(kernel='rbf', random_state=0, gamma=10, C=1) 
  3. # Train the classifier 
  4. svm.fit(X_xor, y_xor) 
  5.  
  6. # Visualize the decision boundaries 
  7. fig = plt.figure(figsize=(10,10)) 
  8. plot_decision_regions(X_xor, y_xor, clf=svm) 
  9. plt.legend(loc='upper left'
  10. plt.tight_layout() 
  11. plt.show() 

 

如何使用支持向量機學習非線性數(shù)據(jù)集

 

 

這是否意味著如果我們將伽瑪提高到10000,它將更加準確呢?事實上,如果伽瑪值太大,則分類器最終會對差異不敏感。

 

如何使用支持向量機學習非線性數(shù)據(jù)集

 

讓我們增加C。C是與整個機器學習數(shù)據(jù)集的錯誤分類相關的成本。換句話說,增加C將增加對整個數(shù)據(jù)集的敏感性,而不僅僅是單個數(shù)據(jù)點。

 

  1. from ipywidgets import interact, interactive, fixed, interact_manual 
  2. import ipywidgets as widgets 
  3.  
  4. warnings.filterwarnings("ignore"
  5.  
  6. @interact(x=[1, 10, 1000, 10000, 100000]) 
  7. def svc(x=1): 
  8.   # Create a SVC classifier using an RBF kernel 
  9.   svm = SVC(kernel='rbf', random_state=0, gamma=.01, C=x) 
  10.   # Train the classifier 
  11.   svm.fit(X_xor, y_xor) 
  12.  
  13.   # Visualize the decision boundaries 
  14.   fig = plt.figure(figsize=(10,10)) 
  15.   plot_decision_regions(X_xor, y_xor, clf=svm) 
  16.   plt.legend(loc='upper left'
  17.   plt.tight_layout() 
  18.   plt.show() 

 

如何使用支持向量機學習非線性數(shù)據(jù)集

 

 

我們已經(jīng)找到了參數(shù),因此我們的SVM分類器可以成功地將兩組點分開。

最后

我希望本文能讓您對SVM分類器是什么以及如何使用它來學習非線機器學習性數(shù)據(jù)集有一個直觀的認識。如果數(shù)據(jù)是高維的,您則無法通過可視化來判斷分類器的性能。好的做法是根據(jù)訓練集進行訓練,并在測試集上使用混淆矩陣或f1-分數(shù)等指標。

責任編輯:華軒 來源: 今日頭條
相關推薦

2017-02-07 14:40:52

2015-10-23 10:23:26

大數(shù)據(jù)向量機

2014-07-08 10:31:08

機器學習

2022-07-22 08:00:00

深度學習數(shù)據(jù)機器學習

2023-09-04 12:58:35

數(shù)據(jù)模型

2021-08-17 10:29:16

人工智能AIDevOps

2024-02-19 14:37:14

機器學習非線性降維

2022-04-18 09:00:00

數(shù)據(jù)庫向量機器學習

2020-06-24 07:53:03

機器學習技術人工智能

2023-11-15 19:17:58

騰訊云向量數(shù)據(jù)庫

2017-10-08 15:04:57

支持向量機機器學習核函數(shù)

2017-10-09 11:21:46

神經(jīng)網(wǎng)絡OpenAI非線性

2021-05-14 08:58:18

非線性安全Go

2015-03-04 10:06:20

光纖OFDM信號

2024-10-12 17:13:53

2023-07-28 08:00:00

人工智能向量數(shù)據(jù)庫

2024-11-21 08:00:00

向量搜索人工智能

2017-05-17 08:24:08

TensorFlow機器學習線性回歸

2023-01-05 08:00:00

點贊
收藏

51CTO技術棧公眾號