• 支持向量机(SVM)


    #线性支持向量机

    #代码:

    from sklearn import datasets
    x,y = datasets.make_classification(n_samples=100,n_features=2,n_redundant=0,n_classes=2,random_state=7816)
    x.shape,y.shape
    import matplotlib.pyplot as plt
    %matplotlib inline
    plt.scatter(x[:,0],x[:,1],c=y,s=100)
    plt.xlabel('x values')
    plt.ylabel('y values')
    import numpy as np
    x = x.astype(np.float32)
    y = y*2-1
    from sklearn import model_selection as ms
    x_train,x_test,y_train,y_test = ms.train_test_split(x,y,test_size=0.2,random_state=42)
    print(x)
    print(y)

    x = x.astype(np.float32)
    y = y*2-1
    from sklearn import model_selection as ms
    x_train,x_test,y_train,y_test = ms.train_test_split(x,y,test_size=0.2,random_state=42)
    import cv2
    svm = cv2.ml.SVM_create()
    svm.setKernel(cv2.ml.SVM_LINEAR)
    svm.train(x_train,cv2.ml.ROW_SAMPLE,y_train)
    _,y_pred = svm.predict(x_test)
    from sklearn import metrics
    metrics.accuracy_score(y_test,y_pred)
    def plot_decision_boundary(svm, X_test, y_test):
        # create a mesh to plot in
        h = 0.02  # step size in mesh
        x_min, x_max = X_test[:, 0].min() - 1, X_test[:, 0].max() + 1
        y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1
        xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                             np.arange(y_min, y_max, h))
       
        X_hypo = np.c_[xx.ravel().astype(np.float32),
                       yy.ravel().astype(np.float32)]
        _, zz = svm.predict(X_hypo)
        zz = zz.reshape(xx.shape)
       
        plt.contourf(xx, yy, zz, cmap=plt.cm.coolwarm, alpha=0.8)
        plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, s=200)
    plt.figure(figsize=(10, 6))
    plot_decision_boundary(svm, x_test, y_test)
    可视化决策边界结果:

     #非线性支持向量机:

    #代码:

    kernels = [cv2.ml.SVM_LINEAR,cv2.ml.SVM_INTER,cv2.ml.SVM_SIGMOID,cv2.ml.SVM_RBF]
    for idx,kernel in enumerate(kernels):
        svm = cv2.ml.SVM_create()
        svm.setKernel(kernel)
        svm.train(x_train,cv2.ml.ROW_SAMPLE,y_train)
        _,y_pred = svm.predict(x_test)
        accuracy = metrics.accuracy_score(y_test,y_pred)
       
        plt.subplot(2,2,idx+1)
        plot_decision_boundary(svm,x_test,y_test)
        plt.title('accuracy = %.2f' %accuracy)

  • 相关阅读:
    UWP开发必备:常用数据列表控件汇总比较
    CodeForces 372 A. Counting Kangaroos is Fun
    ubuntu 13.10 eclipse 菜单栏不可用的问题
    Codeforces Round #219(Div. 2)373 B. Making Sequences is Fun(二分+找规律)
    Git/Github使用方法小记
    Ubuntu 下jdk的安装
    VIM简明教程
    codeforces 371 C-Hamburgers
    codeforces 371A K-Periodic Array
    计算机网络中IP地址和MAC地址
  • 原文地址:https://www.cnblogs.com/shiheyuanfang/p/12268312.html
Copyright © 2020-2023  润新知