# This notebook carries out calculations for the example Table 5.1 of Chapter 5 when the adjusted credit score
# for the second loan is 140 instead of 30 and the income for the eighth loan is 60 rather than 95.
# Early printings of the book do not use this example. As indicated in the Errata they mistakenly calculate
# the misclassified loans with reference to the outer edges of the path rather than with reference to the middle of the path.
from sklearn.svm import LinearSVC
from sklearn.svm import SVC
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
Income = pd.DataFrame([30, 55, 63, 35, 28, 140, 100, 60, 64, 63])
Credit = pd.DataFrame([40, 140, 30, 80, 100, 30, 30, 90, 120, 150])
Loan = pd.DataFrame([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
plt.figure(figsize=(10,8))
plt.scatter(Income, Credit, c=Loan)
plt.xlabel('Income')
plt.ylabel('Adjusted Credit Score')
axes = plt.gca()
axes.set_xlim([0,160])
axes.set_ylim([0,None])
plt.show()
X = np.asarray(pd.concat([Income, Credit],axis=1))
y = np.asarray(Loan).ravel()
clf = SVC(kernel='linear',C=0.005,tol=1e-5)
clf.fit(X,y)
w = clf.coef_[0]
b = -clf.intercept_[0]
print(w)
print(b)
x1 = np.linspace(0,160,100)
w1 = w[0]
w2 = w[1]
bu = b+1
bd = b-1
y1 = (bu-w1*x1)/w2
y2 = (bd-w1*x1)/w2
y0 = (b-w1*x1)/w2
plt.figure(figsize=(10,8))
plt.scatter(Income, Credit, c=Loan)
plt.plot(x1,y1,'--',color='skyblue')
plt.plot(x1,y2,'--',color='skyblue')
plt.plot(x1,y0,'-')
plt.xlabel('Income')
plt.ylabel('Adjusted Credit Score')
axes = plt.gca()
axes.set_xlim([0,160])
axes.set_ylim([0,None])
plt.show()
for C in [0.01, 0.002, 0.001, 0.0005, 0.0003, 0.0002, 0.0001]:
clf = SVC(kernel='linear',C=C/2,tol=1e-6)
clf.fit(X,y)
S = clf.score(X,y)
w = clf.coef_[0]
b = -clf.intercept_[0]
P = 2/np.sqrt(w[0]**2+w[1]**2)
print("C = %6.4f, w1 = %6.4f, w2 = %6.4f, b = %5.2f, Loan Misclassified = %3.0f%%, Width = %5.1f" %(C,w[0],w[1],b,100*(1-S),P))
clf = SVC(kernel='linear',C=0.00005,tol=1e-5)
clf.fit(X,y)
w = clf.coef_[0]
b = -clf.intercept_[0]
print(w)
print(b)
x1 = np.linspace(-20,180,100)
w1 = w[0]
w2 = w[1]
bu = b+1
bd = b-1
y1 = (bu-w1*x1)/w2
y2 = (bd-w1*x1)/w2
y = (b-w1*x1)/w2
plt.figure(figsize=(10,8))
plt.scatter(Income, Credit, c=Loan)
plt.plot(x1,y1,'--',color='skyblue')
plt.plot(x1,y2,'--',color='skyblue')
plt.plot(x1,y,'-')
plt.xlabel('Income')
plt.ylabel('Adjusted Credit Score')
axes = plt.gca()
axes.set_xlim([-20,180])
axes.set_ylim([0,None])
plt.show()