I'm not sure why
grid.fit(X,y) is correct, rather than
In this tutorial on RBF SVM Parameters, we are using
GridSearchCV to find the optimum hyperparameters for an SVM.
They have the following code:
# Dataset for decision function visualization: we only keep the first two # features in X and sub-sample the dataset to keep only 2 classes and # make it a binary classification problem. X_2d = X[:, :2] X_2d = X_2d[y > 0] y_2d = y[y > 0] y_2d -= 1
param_grid = dict(gamma=gamma_range, C=C_range) # GridSearchCV will search the parameter space for the best parameters to use, minimizing the score function cv = StratifiedShuffleSplit(n_splits=5, test_size=0.2, random_state=42) grid = GridSearchCV(SVC(), param_grid=param_grid, cv=cv) # ==================== CODE I'M INTERESTED IN ==================> # ===== SWITCH `grid.fit(X,y)` with grid.fit(X_2d, y_2d) ========> grid.fit(X, y) # ==================== ^^^^^^^^^^^^^^^^^^^ =============> print("The best parameters are %s with a score of %0.2f" % (grid.best_params_, grid.best_score_))
y_2d are a subset of
Just some information to show what
print(X.shape) #(150,4) print(y.shape) #(150,) print(X_2d.shape) #(100,2) print(y_2d.shape) #(100,) print(type(X)) #<class 'numpy.ndarray'> print(type(y)) #<class 'numpy.ndarray'> print(type(X_2d)) #<class 'numpy.ndarray'> print(type(y_2d)) #<class 'numpy.ndarray'>
Why does changing the code above to
grid.fit(X_2d, y_2d) not work? I'm not sure if it's taking a very long time, OR that it's incorrect. My Jupyter Notebook is just sitting, whereas
grid.fit(X, y) only takes seconds.
My original thought was that we would want to fit to the actual data set we're running on which are
y_2d, rather than