How to transfer decision boundary learned from scaled data to the original data (scaled back data)?

Viewed 165

I fitted SVM on scaled input data and right now I am trying to find a way to transfer decision boundary learned from scaled data to the original data (non-scaled data). How should I do that?

I used the following to plot the decision boundary:

svc0.fit(Xs, y)
plot_decision_regions(X=Xs, y=y ,clf=svc0,legend=2)

enter image description here

then I just scaled back data (svc0 is still fitted on scaled data), but decision boundary looks weird:

Xs_scaledback=scaler.inverse_transform(Xs) 

plot_decision_regions(X=Xs_scaledback,y=y,clf=svc0,legend=2)

enter image description here

I need to show the decision boundary on original (scaled back) data. How can I fix this?!

1 Answers

Have you tried to combine your data scaling into the estimator?

Something like this

import numpy as np
import matplotlib.pyplot as plt

from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

from sklearn.datasets import load_iris

data_dict = load_iris()

X, y = data_dict['data'][:, :2], data_dict['target']

model = make_pipeline(StandardScaler(), SVC())
model.fit(X,y) # You should do a train test split

def plot_decision_boundary(pred_func, X, y):
    # Set min and max values and give it some padding
    x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
    y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
    h = 0.01
    # Generate a grid of points with distance h between them
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    # Predict the function value for the whole gid
    Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    # Plot the contour and training examples
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)
    
    
plot_decision_boundary(model.predict, X, y)

Partly adapted from https://scikit-learn.org/stable/auto_examples/ensemble/plot_voting_decision_regions.html

Related