# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

from sklearn import svm,datasets
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsClassifier

iris = datasets.load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X,y)

# +
clf = KNeighborsClassifier()               #Finding Best K - No. of Neighbors
grid = {"n_neighbors" : [3,5,7,9,11]}               #Dictionary with parameter name and list of values to test on

gsCV = GridSearchCV(clf,grid)
gsCV.fit(X_train,y_train)
# -

gsCV.best_estimator_                              #Shows n_neighbors=7 ---> Best K

gsCV.cv_results_                                #To see all the results

# Grid Search for SVM

# +
clf1 = svm.SVC()

grid = {"C" : [1e2,1e3,5e3,1e4,5e4,1e5],             #100,1000,5000,10000,50000,100000
        "gamma" : [1e-3,5e-4,1e-4,5e-3]}             # 1 / 1000, 50000, 10000, 5000

gsCV = GridSearchCV(clf1,grid)                   #Same Code
gsCV.fit(X_train,y_train)
# -

gsCV.best_estimator_                          #C=100, gamma=0.05 = 5e-3



StarcoderdataJupyterScriptsDedupFiltered