import pandas as pd

print('\nKNN\n---')
d = {'X1': [ 0,   2,   0,   0,  -1,   1 ],
     'X2': [ 3,   0,   1,   1,   0,   1 ],
     'X3': [ 0,   0,   3,   2,   1,   1 ],
     'Y':  ['R', 'R', 'R', 'G', 'G', 'R']}
df = pd.DataFrame(data = d)
df = df.assign(dist = (df.X1**2 + df.X2**2 + df.X3**2)**(0.5))
df = df.sort_values(by='dist')
print(df)
print('K=1 =>', df.head(1).Y.to_numpy()[0])
print('K=3 =>', df.head(3).groupby('Y').count().sort_values(by='dist', # arbitrary
    ascending=False).index.values[0])

print('\nCollege.csv\n-----------')
df = pd.read_csv('College.csv')
print(df)

import matplotlib.pyplot as plt
import pandas as pd

print('\nKNN\n---')
d = {'X1': [ 0,   2,   0,   0,  -1,   1 ],
     'X2': [ 3,   0,   1,   1,   0,   1 ],
     'X3': [ 0,   0,   3,   2,   1,   1 ],
     'Y':  ['R', 'R', 'R', 'G', 'G', 'R']}
df = pd.DataFrame(data = d)
df = df.assign(dist = (df.X1**2 + df.X2**2 + df.X3**2)**(0.5))
df = df.sort_values(by='dist')
print(df)
print('K=1 =>', df.head(1).Y.to_numpy()[0])
print('K=3 =>', df.head(3).groupby('Y').count().sort_values(by='dist', # arbitrary
    ascending=False).index.values[0])

print('\nCollege.csv\n-----------')
df = pd.read_csv('College.csv')
df.rename(columns={'Unnamed: 0': 'Name'}, inplace=True)
df.set_index('Name', inplace=True)
print(df.describe())
fig = plt.figure()
gs = fig.add_gridspec(10, 10)
for r in range(10):
    for c in range(10):
        axes = fig.add_subplot(gs[r, c])
        axes.xaxis.set_visible(False)
        axes.yaxis.set_visible(False)
        if r == c:
            axes.annotate(df.columns.values[r], (0.5, 0.5),
                xycoords='axes fraction', ha='center', va='center')
        else:
            df.plot.scatter(x=r, y=c, ax=axes)
plt.show()

Add scatterplot matrix for college.csv.
Add scatterplot matrix for college.csv.

+ import matplotlib.pyplot as plt
  import pandas as pd
  
  print('\nKNN\n---')
  d = {'X1': [ 0,   2,   0,   0,  -1,   1 ],
       'X2': [ 3,   0,   1,   1,   0,   1 ],
       'X3': [ 0,   0,   3,   2,   1,   1 ],
       'Y':  ['R', 'R', 'R', 'G', 'G', 'R']}
  df = pd.DataFrame(data = d)
  df = df.assign(dist = (df.X1**2 + df.X2**2 + df.X3**2)**(0.5))
  df = df.sort_values(by='dist')
  print(df)
  print('K=1 =>', df.head(1).Y.to_numpy()[0])
  print('K=3 =>', df.head(3).groupby('Y').count().sort_values(by='dist', # arbitrary
      ascending=False).index.values[0])
  
  print('\nCollege.csv\n-----------')
  df = pd.read_csv('College.csv')
- print(df)
+ df.rename(columns={'Unnamed: 0': 'Name'}, inplace=True)
+ df.set_index('Name', inplace=True)
+ print(df.describe())
+ fig = plt.figure()
+ gs = fig.add_gridspec(10, 10)
+ for r in range(10):
+     for c in range(10):
+         axes = fig.add_subplot(gs[r, c])
+         axes.xaxis.set_visible(False)
+         axes.yaxis.set_visible(False)
+         if r == c:
+             axes.annotate(df.columns.values[r], (0.5, 0.5),
+                 xycoords='axes fraction', ha='center', va='center')
+         else:
+             df.plot.scatter(x=r, y=c, ax=axes)
+ plt.show()
  
Add scatterplot matrix for college.csv.
## Code Before:
import pandas as pd

print('\nKNN\n---')
d = {'X1': [ 0,   2,   0,   0,  -1,   1 ],
     'X2': [ 3,   0,   1,   1,   0,   1 ],
     'X3': [ 0,   0,   3,   2,   1,   1 ],
     'Y':  ['R', 'R', 'R', 'G', 'G', 'R']}
df = pd.DataFrame(data = d)
df = df.assign(dist = (df.X1**2 + df.X2**2 + df.X3**2)**(0.5))
df = df.sort_values(by='dist')
print(df)
print('K=1 =>', df.head(1).Y.to_numpy()[0])
print('K=3 =>', df.head(3).groupby('Y').count().sort_values(by='dist', # arbitrary
    ascending=False).index.values[0])

print('\nCollege.csv\n-----------')
df = pd.read_csv('College.csv')
print(df)

## Instruction:
Add scatterplot matrix for college.csv.
## Code After:
import matplotlib.pyplot as plt
import pandas as pd

print('\nKNN\n---')
d = {'X1': [ 0,   2,   0,   0,  -1,   1 ],
     'X2': [ 3,   0,   1,   1,   0,   1 ],
     'X3': [ 0,   0,   3,   2,   1,   1 ],
     'Y':  ['R', 'R', 'R', 'G', 'G', 'R']}
df = pd.DataFrame(data = d)
df = df.assign(dist = (df.X1**2 + df.X2**2 + df.X3**2)**(0.5))
df = df.sort_values(by='dist')
print(df)
print('K=1 =>', df.head(1).Y.to_numpy()[0])
print('K=3 =>', df.head(3).groupby('Y').count().sort_values(by='dist', # arbitrary
    ascending=False).index.values[0])

print('\nCollege.csv\n-----------')
df = pd.read_csv('College.csv')
df.rename(columns={'Unnamed: 0': 'Name'}, inplace=True)
df.set_index('Name', inplace=True)
print(df.describe())
fig = plt.figure()
gs = fig.add_gridspec(10, 10)
for r in range(10):
    for c in range(10):
        axes = fig.add_subplot(gs[r, c])
        axes.xaxis.set_visible(False)
        axes.yaxis.set_visible(False)
        if r == c:
            axes.annotate(df.columns.values[r], (0.5, 0.5),
                xycoords='axes fraction', ha='center', va='center')
        else:
            df.plot.scatter(x=r, y=c, ax=axes)
plt.show()
