from sklearn.svm import LinearSVR
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.datasets import make_regression
from sklearn.multioutput import MultiOutputRegressor
X, y = make_regression(n_samples=200, n_features=5, n_targets=2, shuffle=True, random_state=1)
svr = LinearSVR()
model = MultiOutputRegressor(svr)
kfold = KFold(n_splits=10, shuffle=True, random_state=1)
scores = cross_val_score(model, X, y, cv=kfold, scoring="r2")
print("R2: ", scores.mean())
Here, we are first using the make_regression() function to create the feature set and the target variables. Both X and y are ndarrays here. (How to use the make_regression() function in sklearn?)
X, y = make_regression(n_samples=200, n_features=5, n_targets=2, shuffle=True, random_state=1)
We are creating 200 samples or records with 5 features and 2 target variables.
svr = LinearSVR() model = MultiOutputRegressor(svr)
Now, we are initializing the linear SVR using the LinearSVR class and using the regressor to initialize the multioutput regressor.
kfold = KFold(n_splits=10, shuffle=True, random_state=1)
We are then initializing the k-fold cross-validation with 10 splits.
scores = cross_val_score(model, X, y, cv=kfold, scoring="r2")
print("R2: ", scores.mean())
Now, we are using the cross_val_score() function to estimate the performance of the model. We are using the r2 score here (What is the R-squared score in machine learning?)
The average r2 score will be like the following:
R2: 0.9979302094865631








































0 Comments