0% found this document useful (0 votes)
528 views5 pages

Plotting Decision Regions - Mlxtend

The document discusses using the mlxtend library in Python to plot decision regions from classifiers. It provides examples plotting decision regions from an SVM classifier on the Iris dataset in 2D and 1D, highlighting test data points. Further examples show plotting decision regions from multiple classifiers on the Iris data and on a synthetic half-moons dataset.

Uploaded by

akhi016733
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
528 views5 pages

Plotting Decision Regions - Mlxtend

The document discusses using the mlxtend library in Python to plot decision regions from classifiers. It provides examples plotting decision regions from an SVM classifier on the Iris dataset in 2D and 1D, highlighting test data points. Further examples show plotting decision regions from multiple classifiers on the Iris data and on a synthetic half-moons dataset.

Uploaded by

akhi016733
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

12/8/21, 4:12 PM Plotting Decision Regions - mlxtend

from mlxtend.plotting import plot_decision_regions

import matplotlib.pyplot as plt

from sklearn import datasets

from sklearn.svm import SVC

# Loading some example data

iris = datasets.load_iris()

X = iris.data[:, [0, 2]]

y = iris.target

# Training a classifier

svm = SVC(C=0.5, kernel='linear')

svm.fit(X, y)

# Plotting decision regions

plot_decision_regions(X, y, clf=svm, legend=2)

# Adding axes annotations

plt.xlabel('sepal length [cm]')

plt.ylabel('petal length [cm]')

plt.title('SVM on Iris')

plt.show()

Example 2 - Decision regions in 1D

rasbt.github.io/mlxtend/user_guide/plotting/plot_decision_regions/ 2/25
12/8/21, 4:12 PM Plotting Decision Regions - mlxtend

from sklearn.linear_model import LogisticRegression

from sklearn.naive_bayes import GaussianNB

from sklearn.ensemble import RandomForestClassifier

from sklearn.svm import SVC

from sklearn import datasets

import numpy as np

# Initializing Classifiers

clf1 = LogisticRegression(random_state=1,

solver='newton-cg',

multi_class='multinomial')

clf2 = RandomForestClassifier(random_state=1, n_estimators=100)

clf3 = GaussianNB()

clf4 = SVC(gamma='auto')

# Loading some example data

iris = datasets.load_iris()

X = iris.data[:, [0,2]]

y = iris.target

import matplotlib.pyplot as plt

from mlxtend.plotting import plot_decision_regions

import matplotlib.gridspec as gridspec

import itertools

gs = gridspec.GridSpec(2, 2)

fig = plt.figure(figsize=(10,8))

labels = ['Logistic Regression', 'Random Forest', 'Naive Bayes', 'SVM']

for clf, lab, grd in zip([clf1, clf2, clf3, clf4],

labels,

itertools.product([0, 1], repeat=2)):

clf.fit(X, y)

ax = plt.subplot(gs[grd[0], grd[1]])

fig = plot_decision_regions(X=X, y=y, clf=clf, legend=2)

plt.title(lab)

plt.show()

rasbt.github.io/mlxtend/user_guide/plotting/plot_decision_regions/ 4/25
12/8/21, 4:12 PM Plotting Decision Regions - mlxtend

from mlxtend.plotting import plot_decision_regions

from mlxtend.preprocessing import shuffle_arrays_unison

import matplotlib.pyplot as plt

from sklearn import datasets

from sklearn.svm import SVC

# Loading some example data

iris = datasets.load_iris()

X, y = iris.data[:, [0,2]], iris.target

X, y = shuffle_arrays_unison(arrays=[X, y], random_seed=3)

X_train, y_train = X[:100], y[:100]

X_test, y_test = X[100:], y[100:]

# Training a classifier

svm = SVC(C=0.5, kernel='linear')

svm.fit(X_train, y_train)

# Plotting decision regions

plot_decision_regions(X, y, clf=svm, legend=2,

X_highlight=X_test)

# Adding axes annotations

plt.xlabel('sepal length [cm]')

plt.ylabel('petal length [cm]')

plt.title('SVM on Iris')

plt.show()

rasbt.github.io/mlxtend/user_guide/plotting/plot_decision_regions/ 6/25
12/8/21, 4:12 PM Plotting Decision Regions - mlxtend

gs = gridspec.GridSpec(2, 2)

fig = plt.figure(figsize=(10,8))

labels = ['Logistic Regression', 'Random Forest', 'Naive Bayes', 'SVM']

for clf, lab, grd in zip([clf1, clf2, clf3, clf4],

labels,

itertools.product([0, 1], repeat=2)):

clf.fit(X, y)

ax = plt.subplot(gs[grd[0], grd[1]])

fig = plot_decision_regions(X=X, y=y, clf=clf, legend=2)

plt.title(lab)

plt.show()

Half-Moons

rasbt.github.io/mlxtend/user_guide/plotting/plot_decision_regions/ 8/25
12/8/21, 4:12 PM Plotting Decision Regions - mlxtend

Concentric Circles

from sklearn.datasets import make_circles

X, y = make_circles(n_samples=1000, random_state=123, noise=0.1, factor=0.2)

gs = gridspec.GridSpec(2, 2)

fig = plt.figure(figsize=(10,8))

labels = ['Logistic Regression', 'Random Forest', 'Naive Bayes', 'SVM']

for clf, lab, grd in zip([clf1, clf2, clf3, clf4],

labels,

itertools.product([0, 1], repeat=2)):

clf.fit(X, y)

ax = plt.subplot(gs[grd[0], grd[1]])

fig = plot_decision_regions(X=X, y=y, clf=clf, legend=2)

plt.title(lab)

plt.show()

rasbt.github.io/mlxtend/user_guide/plotting/plot_decision_regions/ 10/25

You might also like