1from sklearn import svm 2import random 3import numpy as np 4import math 5 6from pylab import scatter,figure, clf, plot, xlabel, ylabel, xlim, ylim, title, grid, axes, show,semilogx, semilogy 7import matplotlib.pyplot as plt 8from matplotlib.ticker import MaxNLocator 9from matplotlib.colors import BoundaryNorm,ListedColormap 10 11# Generation of data to train the SVM classifier 12# 100 vectors are generated. Vector have dimension 2 so can be represented as points 13NBVECS = 100 14VECDIM = 2 15 16# A cluster of point is generated around the origin. 17ballRadius = 0.5 18x = ballRadius * np.random.randn(NBVECS,2) 19 20# An annulus of point is generated around the central cluster. 21angle = 2.0*math.pi * np.random.randn(1,NBVECS) 22radius = 3.0+0.1*np.random.randn(1,NBVECS) 23 24xa = np.zeros((NBVECS,2)) 25xa[:,0]=radius*np.cos(angle) 26xa[:,1]=radius*np.sin(angle) 27 28# All points are concatenated 29X_train=np.concatenate((x,xa)) 30 31# First points (central cluster) are corresponding to class 0 32# OTher points (annulus) are corresponding to class 1 33Y_train=np.concatenate((np.zeros(NBVECS),np.ones(NBVECS))) 34 35# Some bounds are computed for the graphical representation 36x_min = X_train[:, 0].min() 37x_max = X_train[:, 0].max() 38y_min = X_train[:, 1].min() 39y_max = X_train[:, 1].max() 40 41# Training is done with a polynomial SVM 42clf = svm.SVC(kernel='poly',gamma='auto', coef0=1.1) 43clf.fit(X_train, Y_train) 44 45# The classifier is tested with a first point inside first class 46test1=np.array([0.4,0.1]) 47test1=test1.reshape(1,-1) 48 49predicted1 = clf.predict(test1) 50# Predicted class should be 0 51print(predicted1) 52 53# Second test is made with a point inside the second class (in the annulus) 54test2=np.array([x_max,0]).reshape(1,-1) 55 56predicted2 = clf.predict(test2) 57# Predicted class should be 1 58print(predicted2) 59 60# The parameters of the trained classifier are printed to be used 61# in CMSIS-DSP 62supportShape = clf.support_vectors_.shape 63 64nbSupportVectors=supportShape[0] 65vectorDimensions=supportShape[1] 66 67print("nbSupportVectors = %d" % nbSupportVectors) 68print("vectorDimensions = %d" % vectorDimensions) 69print("degree = %d" % clf.degree) 70print("coef0 = %f" % clf.coef0) 71print("gamma = %f" % clf._gamma) 72 73print("intercept = %f" % clf.intercept_) 74 75dualCoefs=clf.dual_coef_ 76dualCoefs=dualCoefs.reshape(nbSupportVectors) 77supportVectors=clf.support_vectors_ 78supportVectors = supportVectors.reshape(nbSupportVectors*VECDIM) 79 80print("Dual Coefs") 81print(dualCoefs) 82 83print("Support Vectors") 84print(supportVectors) 85 86# Graphical representation to display the cluster of points 87# and the SVM boundary 88r=plt.figure() 89plt.axis('off') 90XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j] 91Z = clf.decision_function(np.c_[XX.ravel(), YY.ravel()]) 92 93# Put the result into a color plot 94Z = Z.reshape(XX.shape) 95 96levels = MaxNLocator(nbins=15).tick_values(Z.min(), Z.max()) 97 98#cmap = plt.get_cmap('gray') 99newcolors = ['#FFFFFF','#FFFFFF'] 100cmap = ListedColormap(newcolors) 101norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) 102 103plt.pcolormesh(XX, YY, Z > 0, cmap=cmap,norm=norm) 104plt.contour(XX, YY, Z, colors=['k', 'k', 'k'], 105 linestyles=['--', '-', '--'], levels=[-.5, 0, .5]) 106 107scatter(x[:,0],x[:,1],s=1.0,color='#FF6B00') 108scatter(xa[:,0],xa[:,1],s=1.0,color='#95D600') 109 110# The test points are displayed in red. 111scatter(test1[:,0],test1[:,1],s=6.0,color='Red') 112scatter(test2[:,0],test2[:,1],s=6.0,color='Red') 113#r.savefig('fig1.jpeg') 114#plt.close(r) 115show() 116 117 118#r=plt.figure() 119#plt.axis('off') 120#scatter(x[:,0],x[:,1],s=1.0,color='#FF6B00') 121#scatter(xa[:,0],xa[:,1],s=1.0,color='#95D600') 122#r.savefig('fig2.jpeg') 123#plt.close(r)