Ex6_机器学习_吴恩达课程作业(Python):SVM支持向量机(Support Vector Machines)

Ex6_机器学习_吴恩达课程作业(Python):SVM支持向量机(Support Vector Machines)文章目录Ex6_机器学习_吴恩达课程作业(Python):SVM支持向量机(Support Vector Machines)0. Pre-condition00. Self-created FunctionsloadData(path):读取数据plotData(X, y):可视化数据plotBoundary(classifier, X):绘制类别间的决策边界displayBoundarie
摘要由CSDN通过智能技术生成

Ex6_机器学习_吴恩达课程作业(Python):SVM支持向量机(Support Vector Machines)

使用说明:

本文章为关于吴恩达老师在Coursera上的机器学习课程的学习笔记。

  • 本文第一部分首先介绍课程对应周次的知识回顾以及重点笔记,以及代码实现的库引入。
  • 本文第二部分包括代码实现部分中的自定义函数实现细节
  • 本文第三部分即为与课程练习题目相对应的具体代码实现。

0. Pre-condition

This section includes some introductions of libraries.

# This file includes self-created functions used in exercise 3
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import re  # regular expression for e-mail processing
import nltk.stem.porter  # 英文分词算法
from scipy.io import loadmat
from sklearn import svm

00. Self-created Functions

This section includes self-created functions.

  • loadData(path):读取数据
    # Load data from the given file  读取数据
    # ARGS: { path: 数据路径 }
    def loadData(path):
        data = loadmat(path)
        return data['X'], data['y']
    
  • plotData(X, y):可视化数据
    # Visualize data  可视化数据
    # ARGS: { X: 训练集; y: 标签集 }
    def plotData(X, y):
        plt.figure(figsize=[8, 6])
        plt.scatter(X[:, 0], X[:, 1], c=y.flatten())
        plt.xlabel('X1')
        plt.ylabel('X2')
        plt.title('Data Visualization')
        # plt.show()
    
    
  • plotBoundary(classifier, X):绘制类别间的决策边界
    # Plot the boundary between two classes  绘制类别间的决策边界
    # ARGS: { classifier: 分类器; X: 训练集 }
    def plotBoundary(classifier, X):
        x_min, x_max = X[:, 0].min() * 1.2, X[:, 0].max() * 1.1
        y_min, y_max = X[:, 1].min() * 1.2, X[:, 1].max() * 1.1
        xx, yy = np.meshgrid(np.linspace(x_min, x_max, 500),
                             np.linspace(y_min, y_max, 500))
        # 利用传入的分类器,对预测样本做出类别预测
        Z = classifier.predict(np.c_[xx.flatten(), yy.flatten()])
        Z = Z.reshape(xx.shape)
        plt.contour(xx, yy, Z)
    
  • displayBoundaries(X, y):绘制不同SVM参数C下的的决策边界(线性核)
    # Display boundaries for different situations with different C (1 and 100)
    # 改变SVM参数C,绘制各情况下的的决策边界
    # ARGS: { X: 训练集 ; y: 标签集 }
    def displayBoundaries(X, y):
        # 此处使用skilearn的包,采用线性核函数,获取多个SVM模型
        models = [svm.SVC(C=C, kernel='linear') for C in [1, 100]]
        # 给定训练集X和标签集y,训练得到的多个SVM模型,获得多个分类器
        classifiers = [model.fit(X, y.flatten()) for model in models]
        # 输出信息
        titles = ['SVM Decision Boundary with C = {}'.format(C) for C in [1, 100]]
        # 对于每个分类器,绘制其得出的决定边界
        for classifier, title in zip(classifiers, titles):
            plotData(X, y)
            plotBoundary
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值