Machine Learning A-Z学习笔记7

Machine Learning A-Z学习笔记7

第七章 SVM

1.简单原理

1.1 介绍无监督学习

我们对机器学习分类时,把它分成两类监督学习和无监督学习,在前面我们对监督学习进行了详细的介绍,总结一下它的特点就是,我们有一组训练集,每个数据都有一个标签,即我们知道这个数据是输入哪一类的,而我们的无监督学习却是这样的,它的训练数据集是没有标签的,所以我们只能根据哪些数据聚集在一起,然后把他们分成一类,这也就是聚类的含义。

1.2svm

支持向量机(support vector machine, SVM),是用来二分类的一种分类方法。如图,红点和绿点需要画一条线来区分,那我们可以花很多条线,但只有一调效果最好,这条线也是我们说的垂直平分线。

在这里插入图片描述

在这里插入图片描述

而SVM的分类线是由支持向量所支撑,所以才有Support这个词。而SVM中的支持向量是由最极端的特例所決定,這也是SVM跟其他分类方法最不同的地方

在这里插入图片描述

然后後什么是最大间隔超平面/分类器(maximum margin hyperplane/classifier)呢,他是由支持向量,所构成的法向量,两点的距离就是最大边界(maximum margin)。

在这里插入图片描述

一个简单的例子,黄色的苹果和绿色的橘子

在这里插入图片描述

而支持向量的向量是怎么来的呢,以二维平面作为例子,点就是指向量。

2.相关代码


# Support Vector Machine (SVM)
"""
SVM二分類-年紀+收入VS購買SUV
"""

# Importing the libraries

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset

dataset = pd.read_csv('Social_Network_Ads.csv')
X = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4].values

# Splitting the dataset into the Training set and Test set
"""
數據切割
"""
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

# Feature Scaling
"""
特徵縮放-標準化
"""
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Fitting SVM to the Training set
"""
建立SVM分類器
-kernel = 'linear':線性分類
"""
from sklearn.svm import SVC
classifier = SVC(kernel = 'linear', random_state = 0)
classifier.fit(X_train, y_train)

# Predicting the Test set results
"""
預測
"""
y_pred = classifier.predict(X_test)

# Making the Confusion Matrix
"""
混淆矩陣
"""
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

# Visualising the Training set results
"""
訓練結果可視化
"""
from matplotlib.colors import ListedColormap
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
                     np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
                c = ListedColormap(('orange', 'blue'))(i), label = j)
plt.title('SVM (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()

# Visualising the Test set results
"""
測試結果可視化
"""
from matplotlib.colors import ListedColormap
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
                     np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
                c = ListedColormap(('orange', 'blue'))(i), label = j)
plt.title('SVM (Test set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值