Change language

ML | Nonlinear SVM

In

These classes are clearly not linearly separable. Below is a nonlinear SVM contour plot that successfully classified the IRIS dataset using RBF core.

The above figure shows the classification of the three classes of the IRIS dataset.

  1. From sklearn, we imported the SVM library.
  2. We created 3 non-linear SVM’s (RBF kernel based).
  3. Each SVM was fed with 1 class kept positive and other 2 as negative. Say, SVM1 had labels corresponding to class 1 only else all were made 0. Same for SVM2 and SVM3 respectively.
  4. Plot the contour plot of each SVM.
  5. Plot the data points.

Below is a Python implementation for the same.

import numpy as np 

import pandas as pd 

import matplotlib.pyplot as plt 

from matplotlib import style

from sklearn.svm import SVC 

 

style.use ( ’fivethirtyeight’ )

  
# create grid

def make_meshgrid (x , y, h = . 02 ):

x_min, x_max = x. min () - 1 , x. max () + 1

y_min, y_max = y. min () - 1 , y. max () + 1

xx, yy = np.meshgrid (np.arange (x_min, x_max, h), np.arange (y_min, y_max, h))

return xx, yy

 
# build outlines

def plot_contours (ax, clf, xx, yy, * * params):

  Z = clf.predict (np.c_ [xx.ravel (), yy.ravel ()])

Z = Z.reshape (xx.shape)

out = ax.contourf (xx, yy, Z, * * params)

  return out

  

color = [ ’r’ , ’ b’ , ’g’ , ’ k’ ]

 

iris = pd.read_csv ( " iris-data. txt " ). values ​​

  

 

features = iris [ 0 : 150 , 2 : 4 ]

level1 = np.zeros ( 150 )

level2 = np.zeros ( 150 )

level3 = np.zeros ( 150 )

 
# level1 contains 1 for class1 and 0 for all others.
# level2 contains 1 for class2 and 0 for all others.
# level3 contains 1 for class3 and 0 for all others.

for i in range ( 150 ):

if i" = 0 and i " 50 :

  lev el1 [i] = 1

elif i" = 50 and i " 100 :

level2 [i] = 1

elif i" = 100 and i " 150 :

level3 [i] = 1

  # create 3 svm with rbf cores

svm1 = SVC (kernel = ’rbf’ )

svm2 = SVC (kernel = ’ rbf’ )

svm3 = SVC (kernel = ’rbf’ )

 
# fits every svm
svm1.fit (features, level1)
svm2.fit (features, level2)
svm3.fit (features, level3)

 < / p>

fig, ax = plt.subplots ()

X0, X1 = iris [:, 2 ], iris [:, 3 ]

xx, yy = make_meshgrid (X0, X1)

  
# build outlines

plot_contours (ax, svm1, xx, yy, cmap = plt.get_cmap ( ’ hot’ ), alpha = 0.8 )

plot_contours (ax, svm2, xx, yy, cmap = plt.get_c map ( ’hot’ ), alpha = 0.3 )

plot_contours (ax, svm3, xx, yy, cmap = plt.get_cmap ( ’hot’ ), alpha = 0.5 )

 

color = [ ’r’ , ’b’ , ’ g’ , ’k’ ]

  

for i in range ( len ( iris)):

plt.scatter (iris [i] [ 2 ], iris [i] [ 3 ], s = 30 , c = color [ int (iris [ i] [ 4 ])])

plt.show ()

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically