<jupyter_start><jupyter_text>Chapter: Support Vector Machines & Kernel Methods Topic: SVM Binary Classification<jupyter_code># read data
import numpy as np

data = np.loadtxt('toyDataset.csv', delimiter=',')
X = data[:, [0, 1]]; y = data[:, 2]
# scale model inputs
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler() 
X_scaled = scaler.fit_transform(X) 
# fit SVM model
from sklearn.svm import SVC # for large datasets LinearSVC class is preferable

model = SVC(kernel='linear', C=100)
model.fit(X_scaled, y)
# get details of support vectors
print('# of support vectors:', len(model.support_))
# plot SVM boundaries
import matplotlib.pyplot as plt

plt.figure()
plt.scatter(X_scaled[:, 0], X_scaled[:, 1], c=y, cmap=plt.cm.Paired, edgecolors='k')
plt.xlabel('X1'), plt.ylabel('X2')

# get axis limits
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()

# create grid to evaluate model
xx = np.linspace(xlim[0], xlim[1], 100)
yy = np.linspace(ylim[0], ylim[1], 100)
YY, XX = np.meshgrid(yy, xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = model.decision_function(xy).reshape(XX.shape)

# plot decision boundary and supporting planes
ax.contour(XX, YY, Z, levels=[-1, 0, 1], alpha=0.5, linestyles=['--', '-', '--'], colors=['green', 'red', 'green'])

# highlight support vectors
ax.scatter(model.support_vectors_[:, 0], model.support_vectors_[:, 1], s=200, linewidth=2, alpha=0.25)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1686166551961459, 'fraction_numerical': 0.02339986235375086, 'mean_word_length': 5.669724770642202, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '172130', 'license': 'Apache-2.0', 'n_tokens_mistral': 574, 'n_tokens_neox': 545, 'n_words': 142, 'path': 'Chapter_SupportVectorMachines/SVM_BinaryClassification.ipynb', 'repo_name': 'ML-PSE/Machine_Learning_for_PSE', 'size': '1453'}