SVM za regresiju¶

Sve pojmove i hiperparametre zadržavamo, ali će neki od njih imati drugačiju interpretaciju. Interpretacija širokog pojasa kod regresije je nešto drugačija u odnosu na klasifikaciju. Naime, u regresionom slučaju široki pojas je prostor tačaka koje se po y osi razlikuju od regresione krive za više od $\varepsilon$. Za mnogo više detalja pogledati $\href{http://ml.matf.bg.ac.rs/readings/ml.pdf}{link}$. Sada ćemo na primeru pokazati kako se metod potpornih vektora koristi za regresiju.

Skup podataka je California Housing, a cilj je predvideti cenu nekretnine.

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [2]:
from sklearn import model_selection
from sklearn import datasets
from sklearn import preprocessing
from sklearn import metrics
from sklearn import svm
In [3]:
data = datasets.fetch_california_housing()
X = data.data
y = data.target
In [4]:
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.33, random_state=7)
In [5]:
X_train.shape
Out[5]:
(13828, 8)
In [6]:
scaler = preprocessing.StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
In [7]:
model = svm.SVR(kernel='rbf', gamma=0.1, C = 2)
In [8]:
model.fit(X_train, y_train)
Out[8]:
SVR(C=2, gamma=0.1)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
SVR(C=2, gamma=0.1)
In [9]:
metrics.mean_squared_error(y_test, model.predict(X_test))
Out[9]:
0.34724233763674195
In [10]:
metrics.r2_score(y_test, model.predict(X_test))
Out[10]:
0.7426782223919458
In [ ]: