Implementing Custom Randomsearchcv: 'Red' 'Blue'
Implementing Custom Randomsearchcv: 'Red' 'Blue'
# del X_train,X_test
#1.generate 10 unique values(uniform random distribution) in the given range "param_range" and
store them as "params"
# ex: if param_range = (1, 50), we need to generate 10 random numbers in range 1 to 50
#2.devide numbers ranging from 0 to len(X_train) into groups= folds
# ex: folds=3, and len(x_train)=100, we can devide numbers from 0 to 100 into 3 groups
group 1: 0-33, group 2:34-66, group 3: 67-100
#3.for each hyperparameter that we generated in step 1:
# and using the above groups we have created in step 2 you will do cross-validation as follows
# first we will keep group 1+group 2 i.e. 0-66 as train data and group 3: 67-100 as test data,
and find train and
test accuracies
# second we will keep group 1+group 3 i.e. 0-33, 67-100 as train data and group 2: 34-66 as
test data, and find
train and test accuracies
# third we will keep group 2+group 3 i.e. 34-100 as train data and group 1: 0-33 as test data,
and find train and
test accuracies
# based on the 'folds' value we will do the same procedure
# find the mean of train accuracies of above 3 steps and store in a list "train_scores"
# find the mean of test accuracies of above 3 steps and store in a list "test_scores"
#4. return both "train_scores" and "test_scores"
#5. call function RandomSearchCV(x_train,y_train,classifier, param_range, folds) and store the returned
values into "train_score", and "cv_scores"
#6. plot hyper-parameter vs accuracy plot as shown in reference notebook and choose the best
hyperparameter
#7. plot the decision boundaries for the model initialized with the best hyperparameter, as shown in
the last cell of reference notebook
neigh=KNeighborsClassifier()
params={'n_neighbors':sorted(random.sample(range(1,50),10))}
folds=3
trainscore,cv_scores=RandomSearchCV(X_train,y_train,neigh,params,folds)
plt.plot(params['n_neighbors'],trainscore, label='train curve')
plt.plot(params['n_neighbors'],cv_scores, label='test curve')
plt.title('Hyper-parameter VS accuracy plot')
plt.grid()
plt.legend()
plt.show()
plt.figure()
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
# Plot also the training points
plt.scatter(X1, X2, c=y, cmap=cmap_bold)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("2-Class classification (k = %i)" % (clf.n_neighbors))
plt.show()
In [ ]: