#! /usr/bin/env python3 # def handcrafted_classify_svm_rbf ( ): #*****************************************************************************80 # ## handcrafted_classify_svm_rbf() classifies handcrafted data using SVM with RBF. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 August 2023 # # Author: # # Andreas Mueller, Sarah Guido. # Modifications by John Burkardt. # # Reference: # # Andreas Mueller, Sarah Guido, # Introduction to Machine Learning with Python, # OReilly, 2017, # ISBN: 978-1-449-36941-5 # from sklearn.svm import LinearSVC from sklearn.svm import SVC import matplotlib.pyplot as plt import mglearn import numpy as np import platform import sklearn print ( '' ) print ( 'handcrafted_classify_svm_rbf():' ) print ( ' Python version: ' + platform.python_version ( ) ) print ( ' scikit-learn version: '+ sklearn.__version__ ) print ( ' Classify data from the handcrafted dataset.' ) print ( ' Use the support vector classifier with the RBF kernel.' ) print ( ' Then vary the C and GAMMA parameters.' ) print ( '' ) # # Generate the dataset. # print ( ' Generate the handcrafted dataset, (X, y).' ) X, y = mglearn.tools.make_handcrafted_dataset ( ) # # Fit the support vector classifier. # svm = SVC ( kernel = 'rbf', C = 10, gamma = 0.1 ).fit ( X, y ) # # Plot the data, boundary, and highlight the support vectors. # plt.clf ( ) mglearn.plots.plot_2d_separator ( svm, X, eps = 0.5 ) mglearn.discrete_scatter ( X[:,0], X[:,1], y ) sv = svm.support_vectors_ sv_labels = svm.dual_coef_.ravel() > 0 mglearn.discrete_scatter ( sv[:,0], sv[:,1], sv_labels, s = 15, markeredgewidth = 3 ) plt.xlabel ( 'Feature 0' ) plt.ylabel ( 'Feature 1' ) plt.title ( 'RBF support vectors for handcrafted data' ) filename = 'handcrafted_classify_svm_rbf_default.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.close ( ) # # Plot variations of C and Gamma # plt.clf ( ) fig, axes = plt.subplots ( 3, 3, figsize = ( 15, 10 ) ) for ax, C in zip ( axes, [ -1.0, 0.0, 3.0 ] ): for a, gamma in zip ( ax, range ( -1, 2 ) ): mglearn.plots.plot_svm ( log_C = C, log_gamma = gamma, ax = a ) axes[0,0].legend ( [ "class 0", "class 1", "sv class 0", "sv class 1" ], ncol = 4, loc = ( 0.9, 1.2 ) ) filename = 'handcrafted_classify_svm_rbf_variations.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.close ( ) # # Terminate. # print ( '' ) print ( 'handcrafted_classify_svm_rbf():' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 August 2019 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) handcrafted_classify_svm_rbf ( ) timestamp ( )