<jupyter_start><jupyter_text>Train-test split using loop<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(churn.loc[:, churn.columns != 'Exited'], 
                                                    churn['Exited'], stratify=churn['Exited'], random_state = 66) 
from sklearn.neighbors import KNeighborsClassifier
training_accuracy = []
testing_accuracy = []
 # Try n neighbors from 1 to 20
neighbors_settings = range(1,20)
for i in neighbors_settings:
    # build the model
    knn = KNeighborsClassifier(n_neighbors = i)
    knn.fit(X_train, y_train)
    # Record training accuracy
    training_accuracy.append(knn.score(X_train, y_train))
    # Record testing accuracy
    testing_accuracy.append(knn.score(X_test, y_test))
plt.plot(neighbors_settings, training_accuracy , label ='training accuracy')
plt.plot(neighbors_settings, testing_accuracy , label ='testing accuracy')
plt.xlabel('n_neighbors')
plt.ylabel('Accuracy')
plt.legend()
knn = KNeighborsClassifier(n_neighbors = 9)
knn.fit(X_train, y_train)
knn.score(X_test, y_test)
print('Accuracy of knn classifer on train dataset : {:.2f}'.format(knn.score(X_train, y_train)))
print('Accuracy of knn classifer on test dataset : {:.2f}'.format(knn.score(X_test, y_test)))
predictions = knn.predict(X_train)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11462160176340926, 'fraction_numerical': 0.008082292432035268, 'mean_word_length': 5.1076233183856505, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '645180', 'license': 'Apache-2.0', 'n_tokens_mistral': 501, 'n_tokens_neox': 468, 'n_words': 95, 'path': 'Churn-knn.ipynb', 'repo_name': 'amruthamukundan/Churn-Modelling', 'size': '1361'}