<jupyter_start><jupyter_text>K Nearest Neighbors using Iris<jupyter_code>from __future__ import print_function
import os
import numpy as np
import pandas as pd

data = pd.read_csv('./data/Iris_Data.csv')
data['species_pred'] = data['species']
species=data.species.unique()
species=pd.Series(species)
species=species.to_dict()
print(species)
species_inv = dict(zip(species.values(), species.keys()))
data.replace({"species_pred": species_inv}, inplace=True)
data.sample(10)
# Get a list of all the columns that don't contain the label
x_cols = [x for x in data.columns if (x != 'species' and x != 'species_pred')]

# Split the data into two dataframes
X_data = data[x_cols]
y_data = data['species_pred']
# print(y_data.tolist())
from sklearn.neighbors import KNeighborsRegressor

neigh = KNeighborsRegressor(n_neighbors=1,weights='distance')

knn = neigh.fit(X_data, y_data)
y_pred = knn.predict(X_data)
y_pred = y_pred.astype(int)
# print(y_pred.tolist())
# print(y_data.count())
# print(y_pred.size)
def accuracy(real, predict):
    return sum(y_data == y_pred) / float(real.shape[0])
print(accuracy(y_data, y_pred))
# data.sample(10)
data.replace({"species_pred": species}, inplace=True)
data[data['species'] != data['species_pred']]
data.sample(10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1451990632318501, 'fraction_numerical': 0.00624512099921936, 'mean_word_length': 7.165605095541402, 'pattern_counts': {'":': 2, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '380870', 'license': 'Apache-2.0', 'n_tokens_mistral': 496, 'n_tokens_neox': 463, 'n_words': 100, 'path': 'week-02/iris_data_regression.ipynb', 'repo_name': 'evertonaleixo/IntelAAcademy_MACHINE_LEARNING101', 'size': '1281'}