<jupyter_start><jupyter_text>使用 kNN 进行分类<jupyter_code>from playML.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2)
from playML.kNN import KNNClassifier
my_knn_clf = KNNClassifier(k = 4)
my_knn_clf.fit(x_train, y_train)
y_predict = my_knn_clf.predict(x_test)
y_predict
y_test
sum (y_test == y_predict)
sum (y_test == y_predict) / len(y_test)
my_knn_clf.score(x_test, y_test)<jupyter_output><empty_output><jupyter_text>超参数<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2)
knn_clf = KNeighborsClassifier(n_neighbors = 3)
knn_clf.fit(x_train, y_train)
y_predict = knn_clf.predict(x_test)
from sklearn.metrics import accuracy_score
accuracy_score(y_test, y_predict )
knn_clf.score(x_test, y_test)
best_score = 0.0
best_k = -1

for k in range(1,11):
    knn_clf = KNeighborsClassifier(n_neighbors = k)
    knn_clf.fit(x_train, y_train)
    score = knn_clf.score(x_train, y_train)

    if score >= best_score:
        best_k = k
        best_score = score
    
print("best_k = ", best_k)
print("best_score = ", best_score)


KNeighborsClassifier?
best_score = 0.0
best_k = -1
best_method = ""

for method in ["uniform", "distance"]:
    for k in range(1,11):
        knn_clf = KNeighborsClassifier(n_neighbors = k, weights = method)
        knn_clf.fit(x_train, y_train)
        score = knn_clf.score(x_test, y_test)

        if score >= best_score:
            best_k = k
            best_score = score
            best_method = method
    
print("best_k = ", best_k)
print("best_score = ", best_score)
print("best_method = ", best_method)

%%time
best_score = 0.0
best_k = -1
best_p = -1

for k in range(1,11):
    for p in range(1,11):
        knn_clf = KNeighborsClassifier(n_neighbors = k,weights = "distance", p = p)
        knn_clf.fit(x_train, y_train)
        score = knn_clf.score(x_test, y_test)

        if score >= best_score:
            best_k = k
            best_score = score
            best_p = p

print("best_k = ", best_k)
print("best_score = ", best_score)
print("best_p = ", p)
param_grid = [
    {
        "weights":["uniform"],
        "n_neighbors":[i for i in range(1, 11)]
    },
     {
        "weights":["distance"],
        "n_neighbors":[i for i in range(1, 11)],
         "p":[i for i in range(1, 11)]
    }
    
]
knn_clf = KNeighborsClassifier()
from sklearn.model_selection import GridSearchCV
grid_search = GridSearchCV(knn_clf, param_grid)
%%time
grid_search.fit(x_train, y_train) # 80% 
grid_search?
grid_search.best_estimator_
grid_search.best_score_
grid_search.best_params_
knn_clf.fit(x_train, y_train)
knn_clf.score(x_test, y_test) #测试集的准确度 0.2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11481218993621545, 'fraction_numerical': 0.014528703047484054, 'mean_word_length': 3.81740614334471, 'pattern_counts': {'":': 5, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '290463', 'license': 'MIT', 'n_tokens_mistral': 1192, 'n_tokens_neox': 1150, 'n_words': 218, 'path': 'phase/04-hyper-param.ipynb', 'repo_name': 'pipo-chen/python-machine-learning', 'size': '2822'}