• 02-32 线性支持向量9-机(鸢尾花分类)



    更新、更全的《机器学习》的更新网站,更有python、go、数据结构与算法、爬虫、人工智能教学等着你:https://www.cnblogs.com/nickchen121/p/11686958.html

    线性支持向量机(鸢尾花分类)

    一、导入模块

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    from matplotlib.colors import ListedColormap
    from matplotlib.font_manager import FontProperties
    from sklearn import datasets
    from sklearn.svm import SVC
    %matplotlib inline
    font = FontProperties(fname='/Library/Fonts/Heiti.ttc')
    

    二、获取数据

    iris_data = datasets.load_iris()
    X = iris_data.data[:, [2, 3]]
    y = iris_data.target
    label_list = ['山鸢尾', '杂色鸢尾', '维吉尼亚鸢尾']
    

    三、构建决策边界

    def plot_decision_regions(X, y, classifier=None):
        marker_list = ['o', 'x', 's']
        color_list = ['r', 'b', 'g']
        cmap = ListedColormap(color_list[:len(np.unique(y))])
    
        x1_min, x1_max = X[:, 0].min()-1, X[:, 0].max()+1
        x2_min, x2_max = X[:, 1].min()-1, X[:, 1].max()+1
        t1 = np.linspace(x1_min, x1_max, 666)
        t2 = np.linspace(x2_min, x2_max, 666)
    
        x1, x2 = np.meshgrid(t1, t2)
        y_hat = classifier.predict(np.array([x1.ravel(), x2.ravel()]).T)
        y_hat = y_hat.reshape(x1.shape)
        plt.contourf(x1, x2, y_hat, alpha=0.2, cmap=cmap)
        plt.xlim(x1_min, x1_max)
        plt.ylim(x2_min, x2_max)
    
        for ind, clas in enumerate(np.unique(y)):
            plt.scatter(X[y == clas, 0], X[y == clas, 1], alpha=0.8, s=50,
                        c=color_list[ind], marker=marker_list[ind], label=label_list[clas])
    

    四、线性可分支持向量机

    4.1 训练模型

    svm = SVC(kernel='linear', random_state=1)
    svm.fit(X, y)
    
    SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
      decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
      kernel='linear', max_iter=-1, probability=False, random_state=1,
      shrinking=True, tol=0.001, verbose=False)
    

    4.2 可视化

    plot_decision_regions(X, y, classifier=svm)
    plt.xlabel('花瓣长度(cm)', fontproperties=font)
    plt.ylabel('花瓣宽度(cm)', fontproperties=font)
    plt.title('线性可分支持向量机(鸢尾花分类)', fontproperties=font, fontsize=20)
    plt.legend(prop=font)
    plt.show()
    

    png

    五、线性支持向量机

    5.1 训练模型(C=0.01)

    svm = SVC(kernel='linear', C=0.01, random_state=1)
    svm.fit(X, y)
    
    SVC(C=0.01, cache_size=200, class_weight=None, coef0=0.0,
      decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
      kernel='linear', max_iter=-1, probability=False, random_state=1,
      shrinking=True, tol=0.001, verbose=False)
    

    5.2 可视化

    plot_decision_regions(X, y, classifier=svm)
    plt.xlabel('花瓣长度(cm)', fontproperties=font)
    plt.ylabel('花瓣宽度(cm)', fontproperties=font)
    plt.title('线性支持向量机(鸢尾花分类)', fontproperties=font, fontsize=20)
    plt.legend(prop=font)
    plt.show()
    

    png

    5.3 训练模型(C=100)

    svm = SVC(kernel='linear', C=100, random_state=1)
    svm.fit(X, y)
    
    SVC(C=100, cache_size=200, class_weight=None, coef0=0.0,
      decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
      kernel='linear', max_iter=-1, probability=False, random_state=1,
      shrinking=True, tol=0.001, verbose=False)
    

    5.4 可视化

    plot_decision_regions(X, y, classifier=svm)
    plt.xlabel('花瓣长度(cm)', fontproperties=font)
    plt.ylabel('花瓣宽度(cm)', fontproperties=font)
    plt.title('线性支持向量机(鸢尾花分类)', fontproperties=font, fontsize=20)
    plt.legend(prop=font)
    plt.show()
    

    png

  • 相关阅读:
    中国剩余定理(普通+扩展)
    因子和(洛谷P1593)——约数和+分解质因数
    暑假考试题6:problem 题(Catlan + dp + 组合数)
    暑假考试题6:single 单(树上推理)
    暑假考试题5:tree 最小生成树(最小生成树+倍增)
    暑假考试题5:序列(分类讨论水题)
    暑假考试题5:工作 work(贪心+二分)
    暑假考试题4:砍树 cut(整除分块)
    4.8 作业
    面向对象编程
  • 原文地址:https://www.cnblogs.com/abdm-989/p/12069910.html
Copyright © 2020-2023  润新知