<jupyter_start><jupyter_text>Introduction to Data Science - kNN  Imports<jupyter_code>import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sklearn import neighbors, datasets
from matplotlib.colors import ListedColormap
from pandas import DataFrame
%matplotlib inline
plt.style.use('dark_background')
from matplotlib import animation, rc
from IPython.display import HTML<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>df = pd.read_table('data/anaconda.dat')
df['class'] = [0 if x == 'F' else 1 for x in df['gender']]
df.head()
df.tail()<jupyter_output><empty_output><jupyter_text>Plot It<jupyter_code>colors = ['red', 'blue']
ax = df.plot.scatter(x='snout_length', y='weight', c='class', cmap=ListedColormap(colors), \
                     figsize=(14, 8), grid = True, legend = True, title='Anaconda Snout-Vent Length vs Weight')
ax.set_xlabel('Snout-Vent Length')
ax.set_ylabel('Weight')
ax.get_figure().subplots_adjust(bottom=0.25)
plt.show()<jupyter_output><empty_output><jupyter_text>Plotting Decision Boundaries<jupyter_code>cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])

data = np.array([df['snout_length'], df['weight'], df['class']]).T
np.random.shuffle(data)
test_size = 4

X = data[:-test_size, 0:2]
y = np.array(data[:-test_size, 2])

h = 1
# Generate mesh values
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
# fig, ax = plt.subplots(nrows=2, ncols=4)
fig = plt.figure(figsize=(24, 44))

cnt = 1
for num_neigh in range(5, 35, 4):
    clf = neighbors.KNeighborsClassifier(num_neigh, weights='uniform')
    clf.fit(X, y)

    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

    Z = Z.reshape(xx.shape)
    plt.subplot(4, 2, cnt)
#     plt.figure(2, 4, cnt)
    plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
    
    
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold,
                edgecolor='k', s=20)
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.title("Anaconda classification (k = {})".format(num_neigh))
    cnt += 1

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17498914459400783, 'fraction_numerical': 0.02301346070343031, 'mean_word_length': 4.605839416058394, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '417986', 'license': 'MIT', 'n_tokens_mistral': 956, 'n_tokens_neox': 914, 'n_words': 188, 'path': 'Introduction to Data Science - kNN.ipynb', 'repo_name': 'willshiao/acm-ds-workshop', 'size': '2303'}