0
$\begingroup$

I'm not sure why grid.fit(X,y) is correct, rather than grid.fit(X_2d, y_2d)

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_))

where X_2d and y_2d are a subset of X and y.


Just some information to show what X and y are:

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 X_2d and y_2d, rather than X and y

$\endgroup$
1
$\begingroup$

If you aren't sure whether the code works or not, you could turn on gridsearch output:

grid = GridSearchCV(SVC(), param_grid=param_grid, cv=cv, verbose=2)

I just run the script in your link - it worked fine for me. Are you sure you didn't change something while working on the code?

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.