Sem Spirit

Support Vector Regression in Python

The aim of this script is to create in Python the following bivariate SVR model (the observations are represented with blue dots and the predictions with the multicolored 3D surface) :

3D graph of the SVR model

3D graph of the SVR model

We start by importing the necessary packages :

import pandas as pd
import numpy as np
from matplotlib import cm
from matplotlib import pyplot
from matplotlib.mlab import griddata
from mpl_toolkits.mplot3d import Axes3D

Then we load the dataset

#loading the dataset
dataset = pd.read_csv('dataset.csv')

S = dataset.iloc[:,0:2].values
t = dataset.iloc[:,2].values

whose first 30 rows over 1000 are as follows :

Diamond price (C=dollars/carat) VS Paleonium concentration (A=g/m^3) and pressure (B=kg/cm^2)

Diamond price (dollars/carat) VS Paleonium concentration (g/m^3) and pressure (kg/cm^2)

The we proceed to feature scaling of the dataset :

#feature scaling
from sklearn.preprocessing import StandardScaler
sc_S = StandardScaler()
sc_t = StandardScaler()
S2 = sc_S.fit_transform(S)
t2 = sc_t.fit_transform(t)

Then we fit the regressor to the scaled dataset :

#fitting the SVR to the dataset
from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf'), t2)

We finish this script by displaying in a 3D space the observed and predicted Price along the z axis, where x and y axis correspond to Paleonium and Pressure.

#displaying the 3D graph
x = S[:, 0]
y = S[:, 1]
z = t
zp = sc_t.inverse_transform(regressor.predict(sc_S.transform(S))) #the predictions

xi = np.linspace(min(x), max(x))
yi = np.linspace(min(y), max(y))
X, Y = np.meshgrid(xi, yi)
ZP = griddata(x, y, zp, xi, yi)

fig = pyplot.figure()
ax = Axes3D(fig)
surf = ax.plot_surface(X, Y, ZP, rstride=1, cstride=1, facecolors=cm.jet(ZP/3200), linewidth=0, antialiased=True)
ax.scatter(x, y, z)
ax.set_zlim3d(np.min(z), np.max(z))
colorscale = cm.ScalarMappable(cmap=cm.jet)

The above code produces the following 3D graph where the predictions take the shape of a colorfaded 3D surface and the observations are represented with blue dots. The model is fitting relatively well the observations since the blue dots are wrapping the 3D surface very closely.

3D graph of the SVR model

3D graph of the SVR model