<jupyter_start><jupyter_text><NAME> CB.SC.15DAS20032<jupyter_code>from datascience import *
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
ckd_main =Table.read_table('../ckd.csv').relabeled('Blood Glucose Random','Glucose')

ckd = ckd_main
ckd
def standard_units(x):
    return (x - np.mean(x))/np.std(x)

def distance(point1, point2):
    return np.sqrt(np.sum((point1 - point2)**2))

def all_distances(training, point):
    attributes = training.drop('Class')
    def distance_from_point(row):
        return distance(point, np.array(row))
    return attributes.apply(distance_from_point)

def table_with_distances(training, point):
    return training.with_column('Distance', all_distances(training, point))

def closest(training, point, k):
    with_dists = table_with_distances(training, point)
    sorted_by_distance = with_dists.sort('Distance')
    topk = sorted_by_distance.take(np.arange(k))
    return topk

def majority(topkclasses):
    ones = topkclasses.where('Class', are.equal_to(1)).num_rows
    zeros = topkclasses.where('Class', are.equal_to(0)).num_rows
    if ones > zeros:
        return 1
    else:
        return 0

def classify(training, p, k):
    closestk = closest(training, p, k)
    topkclasses = closestk.select('Class')
    return majority(topkclasses)

ckd=Table().with_columns('Hemoglobin', standard_units(ckd.column('Hemoglobin')),'Glucose',standard_units(ckd.column('Glucose')),'White Blood Cell Count',standard_units(ckd.column('White Blood Cell Count')),'Class',ckd.column('Class'))

ckd
def show_closest(point):

    HemoGl = ckd.drop('White Blood Cell Count', 'Color')
    t = closest(HemoGl, point, 1)
    x_closest = t.row(0).item(1)
    y_closest = t.row(0).item(2)
    ckd.scatter('Hemoglobin', 'Glucose', group='Color')
    plt.scatter(point.item(0), point.item(1), color='red', s=30)
    plt.plot(make_array(point.item(0), x_closest), make_array(point.item(1), y_closest), color='k', lw=2);
color_table=Table().with_columns('Class',make_array(1,0),'Color',make_array('darkblue','gold'))

ckd=ckd.join('Class',color_table)

ckd.scatter('Hemoglobin','Glucose', group = 'Color')
alice=make_array(0,1.5)
show_closest(alice)
alice=make_array(0,0.97)

show_closest(alice)
ckd.scatter('White Blood Cell Count','Glucose',group='Color')
shuffled_ckd=ckd.sample(with_replacement=False)
training=shuffled_ckd.take(np.arange(79))
testing=shuffled_ckd.take(np.arange(79,158))
training.scatter('White Blood Cell Count','Glucose',group='Color')
plt.xlim(-2,6)
plt.ylim(-2,6);
ckd = ckd_main
ckd.row(0)
ckd.row(0).item(3)
ckd=Table().with_columns('Hemoglobin',standard_units(ckd.column('Hemoglobin')),'Glucose',standard_units(ckd.column('Glucose')),'Class',ckd.column('Class'))

color_table=Table().with_columns('Class',make_array(1,0),'Color',make_array('darkblue','gold'))

ckd=ckd.join('Class',color_table)

ckd
alice=make_array(0,1.1)
ckd.scatter('Hemoglobin','Glucose',group='Color')
plt.scatter(alice.item(0),alice.item(1),color='red',s=30)
ckd_attributes=ckd.select('Hemoglobin','Glucose')

ckd_attributes
ckd_attributes.row(3)
np.array(ckd_attributes.row(3))
patient3=np.array(ckd_attributes.row(3))
alice,patient3
distance=np.sqrt(np.sum((alice-patient3)**2))
distance
def distance(point1,point2):
    return np.sqrt(np.sum((point1-point2)**2))

distance(alice,patient3)
t=ckd_attributes.take(np.arange(5))

t
def max_abs(row):
    return np.max(np.abs(np.array(row)))
    
max_abs(t.row(4))
t.apply(max_abs)
def distance_from_alice(row):    
    return distance(alice,np.array(row))
    
distance_from_alice(ckd_attributes.row(3))
distances=ckd_attributes.apply(distance_from_alice)

ckd_with_distances=ckd.with_column('Distance from Alice',distances)

ckd_with_distances
sorted_by_distance=ckd_with_distances.sort('Distance from Alice')

sorted_by_distance
alice_5_nearest_neighbors=sorted_by_distance.take(np.arange(5))
alice_5_nearest_neighbors<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16163410301953818, 'fraction_numerical': 0.020045673686881503, 'mean_word_length': 7.044897959183674, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, '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': '161341', 'license': 'MIT', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1503, 'n_words': 200, 'path': 'FODS/FODS-Final-Submission/NearNeigh.ipynb', 'repo_name': 'orectique/CourseWork-Sem3', 'size': '3954'}