text_plain_output_1.png
from matplotlib.colors import ListedColormap

from sklearn.neighbors import KNeighborsClassifier

import matplotlib.pyplot as plt

import numpy as np # linear algebra

correlations = df1.corr()
names = ['hair','feathers','eggs','milk','airborne','aquatic','predator','toothed','backbone','breathes','venomous','fins','legs','tail','domestic','catsize']
# plot correlation matrix
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(correlations, vmin=-1, vmax=1)
fig.colorbar(cax)
ticks = np.arange(0,15,1)
ax.set_xticks(ticks)
ax.set_yticks(ticks)
ax.set_xticklabels(names)
ax.set_yticklabels(names)
plt.show()

from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=5, p=2)
knn.fit(df1[['predator', 'toothed']], df1.backbone)

def plotMesh():
    h = 100
    cmap_light = ListedColormap(['#ffffb3', '#ff9999', '#d6d6f5', '#ccffdd'])
    colormap = np.array(['black', 'yellow', 'red', 'blue', 'green'])
    x_min, x_max = (df1.predator.min() - 1000, df1.toothed.max() + 1000)
    y_min, y_max = (df1.predator.min() - 1000, df1.toothed.max() + 1000)
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    Z = knn.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    fig = plt.figure()
    plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
    plt.scatter(df1.predator, df1.toothed, c=colormap[df1.backbone], s=120)
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.title('4-Class classification \n(k = %i)\n    Loan 1 - Yellow, Loan 2 - Red, Loan 3 - Blue, Loan 4 - green' % 5)
    ax = fig.add_subplot(111)
plotMesh()