In [1]:
# Kod 1
 
import pandas as pd
from sklearn.datasets import load_breast_cancer
 
from sklearn.neighbors import KNeighborsClassifier
 
from sklearn.model_selection import train_test_split
  
breast_cancer = load_breast_cancer()
X = pd.DataFrame(breast_cancer["data"], 
                 columns = breast_cancer["feature_names"])
y = pd.Series(breast_cancer["target"])
 
X_train, X_test, y_train, y_test = train_test_split(X, y, 
                                                    test_size = 0.25, 
                                                    random_state = 42)
In [2]:
# Kod 2

knn = KNeighborsClassifier()
knn.fit(X = X_train, y = y_train)

train_score = knn.score(X = X_train, y = y_train)
test_score = knn.score(X = X_test, y = y_test)

train_score, test_score
Out[2]:
(0.9342723004694836, 0.965034965034965)
In [3]:
%%time
# Kod 3


from sklearn.model_selection import GridSearchCV

param_grid = {'n_neighbors': range(1, 101),
              'weights': ["uniform", "distance"],
              'p': [1, 2]
             }

knn_grid_search = GridSearchCV(estimator = KNeighborsClassifier(), param_grid = param_grid, cv = 5, iid = False)

knn_grid_search.fit(X = X_train, y = y_train)

grid_train_score = knn_grid_search.score(X = X_train, y = y_train)
grid_test_score = knn_grid_search.score(X = X_test, y = y_test)

print(grid_train_score, grid_test_score)
print(knn_grid_search.best_params_)
0.9436619718309859 0.972027972027972
{'n_neighbors': 6, 'p': 1, 'weights': 'uniform'}
CPU times: user 1min 8s, sys: 164 ms, total: 1min 9s
Wall time: 1min 9s
In [ ]: