If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Here's a Python code example using scikit-learn to implement Support Vector Regression (SVR):
pythonCopy code
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVR
# Example data
x = np.array([1, 2, 3, 4, 5]).reshape(-1, 1) # Reshape to a column vector
y = np.array([2, 3, 4, 2, 3])
# Create and fit the SVR model
model = SVR(kernel='linear', C=1.0) # Linear kernel with C=1.0 (regularization parameter)
model.fit(x, y)
# Predict using the model
x_new = np.array([6, 7, 8]).reshape(-1, 1)
predictions = model.predict(x_new)
# Visualize the SVR Model
plt.scatter(x, y, color='blue', label='Data Points')
plt.plot(x, model.predict(x), color='red', label='SVR Model')
plt.scatter(x_new, predictions, color='green', label='Predictions')
plt.xlabel('Independent Variable (x)')
plt.ylabel('Dependent Variable (y)')
plt.title('Support Vector Regression (SVR) using scikit-learn')
plt.legend()
plt.show()
print("Predictions on new data:", predictions)
In this implementation, we used scikit-learn's SVR
class to create and train the SVR model. The process is as follows:
We imported the necessary libraries, including numpy
, matplotlib.pyplot
, and SVR
from sklearn.svm
.
We defined the example data x
and y
.
We created an instance of SVR
as model
with a linear kernel and regularization parameter C
set to 1.0.
We fitted the model with the data using model.fit(x, y)
.
We then made predictions on new data x_new
using the model.
Finally, we used matplotlib
to visualize the SVR model's curve, data points, and predictions.
In this example, we used the linear kernel (kernel='linear'
) to perform linear regression with SVR. However, you can also try other kernels like the radial basis function (RBF) kernel (kernel='rbf'
), polynomial kernel (kernel='poly'
), or sigmoid kernel (kernel='sigmoid'
). The choice of kernel and hyperparameters will depend on the nature of the data and the complexity of the underlying relationship between variables. It's essential to tune these hyperparameters appropriately for the best model performance.
Comments: 0