<jupyter_start><jupyter_text>Choosing value of K for KNN  1. Elbow Method<jupyter_code>test_error_rates = []

for k in range(1,30):
    knn_model = KNeighborsClassifier(n_neighbors = k)
    knn_model.fit(scaled_X_train, y_train)

    y_pred_test = knn_model.predict(scaled_X_test)
    ## calculate the test_Error
    test_error = 1 - accuracy_score(y_test, y_pred_test)

    test_error_rates.append(test_error)

test_error_rates
plt.plot(range(1,30), test_error_rates)
plt.ylabel('Error Rate')
plt.xlabel('K Neighbors')<jupyter_output><empty_output><jupyter_text>I think we should go for K = 7 as it had lowerror rate at start and train the model<jupyter_code>knn_model.get_params().keys()<jupyter_output><empty_output><jupyter_text>2. Grid Search CV<jupyter_code>## GridSearch CV using Pipeline
operations = [('scaler',scaler), ('knn',knn_model)]
from sklearn.pipeline import Pipeline
pipe = Pipeline(operations)
from sklearn.model_selection import GridSearchCV
k_values = list(range(1,21))
k_values
## for knn it has done seperate syntax for performing GridSearchCV
param_grid = {'knn__n_neighbors': k_values, 'knn__metric': ['euclidean']}
full_cv_classifier = GridSearchCV(pipe, param_grid, cv =5, scoring = 'accuracy')
full_cv_classifier.fit(X_train, y_train)
full_cv_classifier.best_estimator_.get_params()
full_pred = full_cv_classifier.predict(X_test)
print(classification_report(y_test, full_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.90      0.90      0.90       136
           1       0.92      0.91      0.92       164

    accuracy                           0.91       300
   macro avg       0.91      0.91      0.91       300
weighted avg       0.91      0.91      0.91       300
{'fraction_non_alphanumeric': 0.09405654933641085, 'fraction_numerical': 0.04096941719561454, 'mean_word_length': 3.7247956403269753, 'pattern_counts': {'":': 0, '<': 12, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '470291', 'license': 'MIT', 'n_tokens_mistral': 664, 'n_tokens_neox': 616, 'n_words': 133, 'path': 'KNN/SimpleKNNImplementation.ipynb', 'repo_name': 'AkashBorgalli/Linear-Regression', 'size': '1733'}