AI教程网 - 未来以来,拥抱AI;新手入门,从AI教程网开始......

如何使用支持向量机学习非线性数据集

AI前沿 AI君 54℃

 支持向量机(SVM)

什么是支持向量机呢?支持向量机是监督机器学习模型,可对数据进行分类分析。实际上,支持向量机算法是寻找能将实例进行分离的优秀超平面的过程。

如何使用支持向量机学习非线性数据集

如果数据像上面那样是线性可分离的,那么我们用一个线性分类器就能将两个类分开。如果我们的数据是非线性可分的,我们应该怎么做呢?就像这样:

如何使用支持向量机学习非线性数据集

正如我们所看到的,即使来自不同类的数据点是可分离的,我们也不能简单地画一条直线来进行分类。

如何使用支持向量机学习非线性数据集

那么我们如何使用支持向量机来拟合非线性机器学习数据集呢?

使用SVM进行实验

创建机器学习数据集

首先创建非线性机器学习数据集。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() 

如何使用支持向量机学习非线性数据集

尝试使用线性支持向量机

我们首先尝试使用线性支持向量机,Python实现如下:

  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值越高,算法对数据集的正确分离就越严格。对于线性分类器,我们使用kernel=’linear’。

如何使用支持向量机学习非线性数据集

如我们所见,即使我们将成本设置得很高,但这条线也无法很好地分离红点和蓝点。

径向基函数核

到目前为止,我们使用的线性分类器为:

如何使用支持向量机学习非线性数据集

正如我们所看到的,g(x)是一个线性函数。当g(x) >为0时,预测值为1。当g(x) <0时,预测值为-1。但是由于我们不能使用线性函数处理像上面这样的非线性数据,我们需要将线性函数转换成另一个函数。

如何使用支持向量机学习非线性数据集

这个分类器似乎是我们非线性数据的理想选择。让我们来看看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是调节函数。因此,gamma值越小,sigma值就越大,分类器对各个点之间的距离就越不敏感。

如何使用支持向量机学习非线性数据集

让我们把伽玛放大看看会发生什么

  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() 

如何使用支持向量机学习非线性数据集

好像将伽玛值提高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() 

如何使用支持向量机学习非线性数据集

这是否意味着如果我们将伽玛提高到10000,它将更加准确呢?事实上,如果伽玛值太大,则分类器最终会对差异不敏感。

如何使用支持向量机学习非线性数据集

让我们增加C。C是与整个机器学习数据集的错误分类相关的成本。换句话说,增加C将增加对整个数据集的敏感性,而不仅仅是单个数据点。

  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() 

如何使用支持向量机学习非线性数据集

我们已经找到了参数,因此我们的SVM分类器可以成功地将两组点分开。

最后

我希望本文能让您对SVM分类器是什么以及如何使用它来学习非线机器学习性数据集有一个直观的认识。如果数据是高维的,您则无法通过可视化来判断分类器的性能。好的做法是根据训练集进行训练,并在测试集上使用混淆矩阵或f1-分数等指标。

作者:不靠谱的猫_今日头条
原文链接:https://www.toutiao.com/a6828957941589082628/

转载请注明:www.ainoob.cn » 如何使用支持向量机学习非线性数据集

喜欢 (0)or分享 (0)