1. Instalasi Library

Pastikan kamu sudah menginstal scikit-learn dan matplotlib:

pip install scikit-learn matplotlib

2. SVR Linear Sederhana

Step-by-Step:

a) Import Library

import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVR

b) Membuat Data Dummy

# Membuat data sederhana
X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y = np.array([2, 2.8, 3.5, 5, 5.1])

c) Membuat Model SVR

# Membuat model SVR linear
svr_linear = SVR(kernel='linear', C=1000, epsilon=0.1)

# Melatih model
svr_linear.fit(X, y)

d) Visualisasi Hasil

# Prediksi
y_pred = svr_linear.predict(X)

# Plot hasil
plt.scatter(X, y, color='blue', label='Data Asli')
plt.plot(X, y_pred, color='red', label='SVR Linear Prediction')
plt.xlabel('x')
plt.ylabel('y')
plt.title('SVR Linear')
plt.legend()
plt.grid(True)
plt.show()

3. SVR RBF (Non-Linear)

Step-by-Step:

a) Membuat Data Non-Linear

# Data non-linear
X = np.sort(5 * np.random.rand(40, 1), axis=0)
y = np.sin(X).ravel() + np.random.randn(40) * 0.1

b) Membuat Model SVR RBF

# Membuat model SVR dengan kernel RBF
svr_rbf = SVR(kernel='rbf', C=100, gamma=0.5, epsilon=0.1)

# Melatih model
svr_rbf.fit(X, y)

c) Visualisasi Hasil

# Prediksi
X_test = np.linspace(0, 5, 100).reshape(-1, 1)
y_pred_rbf = svr_rbf.predict(X_test)

# Plot hasil
plt.scatter(X, y, color='blue', label='Data Asli')
plt.plot(X_test, y_pred_rbf, color='green', label='SVR RBF Prediction')
plt.xlabel('x')
plt.ylabel('y')
plt.title('SVR RBF')
plt.legend()
plt.grid(True)
plt.show()

Penjelasan Singkat Parameter

  • kernel: Jenis kernel (‘linear’, ‘rbf’, ‘poly’, dll.)
  • C: Trade-off antara kesalahan kecil dan margin besar (semakin besar C, semakin keras penalti terhadap error)
  • epsilon: Margin toleransi kesalahan (ϵ\epsilon-tube)
  • gamma: Untuk kernel non-linear seperti RBF, mengatur jarak pengaruh antar titik.

Catatan Tambahan

  • Untuk dataset non-linear, kernel 'rbf' sering memberikan hasil lebih baik daripada kernel 'linear'.
  • Perlu melakukan tuning terhadap C, epsilon, dan gamma untuk mendapatkan performa optimal.
Leave a Reply 0

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