<jupyter_start><jupyter_text>K Nearest Neighbors ModelIn this notebook we will expirement with a knn model to get an optimal predicted repair time.  Imports<jupyter_code>from pandas import read_csv as pd_read_csv
from numpy import sqrt as np_sqrt
import matplotlib.pyplot as plt
import seaborn as sns
import pickle

from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, accuracy_score, confusion_matrix<jupyter_output><empty_output><jupyter_text>Fetching<jupyter_code>df = pd_read_csv("../Data/CSV/cleaned.csv", sep=';', low_memory=False)
print(f'No less than {df.shape[0]} rows and {df.shape[1]} columns!')<jupyter_output>No less than 252751 rows and 12 columns!<jupyter_text>Model<jupyter_code>model = KNeighborsClassifier(n_neighbors=20)

feature_names = ['prioriteit', 'oorzaakcode', 'geocode']
features = df[feature_names]
target_names = ['hersteltijd_15']
targets = df[target_names]

#: Split data into train set (80%) and test set (20%).
features_train, features_test, targets_train, targets_test = train_test_split(features, targets, random_state=10)
model.fit(features_train, targets_train.values.ravel())
print(f'{len(features_train)} elementen in de trainset en {len(features_test)} elementen in de testset')
#: We calculate the module score by using the test set, 
#: this is why splitting your data into a training and test set is important.
score_1 = model.score(features_test, targets_test)

# An alternative way to calculate the predicted value is by predicting the model's targets by using the features test as input
# and scoring it against the actual targets of the test set.
targets_model = model.predict(features_test)
score_2 = accuracy_score(targets_test, targets_model)
print(f'Scores (methode 1 = {score_1}) and (methode 2 = {score_2}) so the methods are equal is {score_1 == score_2}.')
print(f'With a mean error of: {np_sqrt(mean_squared_error(targets_test, targets_model))}')<jupyter_output>Scores (methode 1 = 0.3195226941824397) and (methode 2 = 0.3195226941824397) so the methods are equal is True.
With a mean error of: 51.15788735995665<jupyter_text>Displaying<jupyter_code>targets_model = model.predict(features_train)
conf = confusion_matrix(targets_train, targets_model)
sns.heatmap(conf)<jupyter_output><empty_output><jupyter_text>We see that the model predicts 0 at a good rate but the rest of the predictions are bad, but it predicts more values in contrast to our previous model.  Predicting<jupyter_code>subset_df = pd_read_csv("../Data/CSV/subset.csv", sep=';')
features = subset_df[feature_names]
targets = subset_df['hersteltijd_15']
subset_df.head()
targets_model = model.predict(features)
score = accuracy_score(targets, targets_model)
print(f'Score = {score}, predictions = {targets_model}, ground truth = {targets.values}.')<jupyter_output>Score = 0.5, predictions = [ 0  0  0  0 15  0  0  0  0  0], ground truth = [  0   0 180  45 150   0 195   0   0  45].<jupyter_text>With a score of 0.5 we would think that it does reasonable well but we see that a lot of the answers are using it's default answer (0).  Saving<jupyter_code>subset_df['hersteltijd_knn'] = targets_model
subset_df.to_csv('../Data/CSV/subset.csv', sep=';', index=False)<jupyter_output><empty_output><jupyter_text>Lastly we make a new column for the predicted results and save it so we can use it in the GUI later on.<jupyter_code>with open('../Data/Model/knn.pkl', 'wb') as file:
    pickle.dump(model, file)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09712837837837837, 'fraction_numerical': 0.032939189189189186, 'mean_word_length': 6.325773195876288, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '84493', 'license': 'MIT', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1166, 'n_words': 360, 'path': 'Notebooks/5_nearest_neighbors.ipynb', 'repo_name': 'WailAbou/Data-Driven-Business', 'size': '3552'}