Sklearn SVR - Training a SVM Regression Model with Python

Python

Example of how to initialise and fit a support vector machine regression model along with how to make predictions on test data and evaluate the results.

Note: Reduce C value if model is overfitting. Change to kernel to 'rbf' or 'poly' for non-linear regression

 1|  from sklearn.svm import SVR
 2|  from sklearn.metrics import mean_squared_error, mean_absolute_error
 3|  
 4|  # initliase & fit model
 5|  model = SVR(C=1.5, kernel='linear')
 6|  model.fit(X_train, y_train)
 7|  
 8|  # make prediction for test data
 9|  y_pred = model.predict(X_test)
10|  
11|  # evaluate performance
12|  print('RMSE:',mean_squared_error(y_test, y_pred, squared = False))
13|  print('MAE:',mean_absolute_error(y_test, y_pred))
Did you find this snippet useful?

Sign up for free to to add this to your code library