Tensorflow linear model using kernel method in Python

Hey there everyone, Today we will learn how to create a linear model using kernel method in Python with Tensorflow. Also, we would be doing this using Tensorflow. Please make sure you know what is SVM and kernel methods and have some idea about Tensorflow then it would be easier to follow this article.
So, let’s get started.

Learn: How to train Tensorflow models in Python?

Creating a linear model using the kernel method in Python

So, first lets import TensorFlow and check its version as we will be implementing our linear model using TensorFlow.

import tensorflow as tf

This should give us output as:


Now, let’s import the other python packages that we will be needing

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.svm import SVC

We now need a dataset to work on. So, here we would be using the Iris dataset.
To know more about Iris dataset refer https://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html

Loading and creating our dataset

my_dataset = datasets.load_iris()
#taking first two features
X = my_dataset.data[:, :2][my_dataset.target != 1]
Y = my_dataset.target[my_dataset.target != 1]

Creating a Linear model using L2 regularization

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1, activation='linear', kernel_regularizer=tf.keras.regularizers.l2()))

Using Linear Kernel and training our  dataset

clf = SVC(kernel='linear')
clf.fit(X, Y)


SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)

Now, we need to plot the decision boundary. So, we here we will assign a color to each point in the mesh grid (x_min, x_max) x (y_min, y_max).

x_min = X[:, 0].min() - 1
x_max = X[:, 0].max() + 1
y_min = X[:, 1].min() - 1
y_max = X[:, 1].max() + 1

plt.scatter(X[:, 0], X[:, 1], c=Y, zorder=10, cmap=plt.cm.Blues,
                edgecolors='face', s=10)

xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.05),
                         np.arange(y_min, y_max, 0.05))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])

Put the result into a color plot

Z = Z.reshape(xx.shape)
plt.pcolormesh(xx, yy, Z > 0, cmap=plt.cm.Paired)
cs = plt.contourf(xx, yy, Z,  alpha=0.8)

Displaying the plotted data

plt.title('linear model display')


Tensorflow linear model using kernel method in Python

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *