<jupyter_start><jupyter_text>Machine Learning Lab \4: Support Vector Machine Classifier Textbook is available @ [https://www.github.com/a-mhamdi/isetbz](https://www.github.com/a-mhamdi/isetbz)--- Support vector machine or **SVM** for short is also known as a discriminative classifier. The concepts are relatively simple. Using a hyperplane with the largest possible margin, the classifier separates data points. The **SVM** classifier finds an optimal hyperplane which allows classifying new data points much more accurately compared to other classifiers such as logistic regression, knn, etc.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Load the dataset `make_blobs` from the `datasets` module of the `sklearn` library.<jupyter_code>from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=50, centers=2, random_state=0, cluster_std=0.60)

xfit = np.linspace(-1, 3.5)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')

# Draw three lines that couple separate the data
for m, b, d in [(1, 0.65, 0.33), (0.5, 1.6, 0.55), (-0.2, 2.9, 0.2)]:
    yfit = m * xfit + b
    plt.plot(xfit, yfit, '-k')
    plt.fill_between(xfit, yfit - d, yfit + d, edgecolor='none', color='#AAAAAA', alpha=0.4)

plt.xlim(-1, 3.5);
plt.grid()<jupyter_output><empty_output><jupyter_text>Import the **Support Vector Machine Classifier**.<jupyter_code>from sklearn.svm import SVC<jupyter_output><empty_output><jupyter_text>A full description of the available methods can be retrieved from the official website of [scikit-learn](https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html).|Syntax|Description||---------:|:------------||`decision_function(X)`| Evaluates the decision function for the samples in X.||`fit(X, y[, sample_weight])`| Fit the SVM model according to the given training data.||`get_params([deep])`| Get parameters for this estimator.||`predict(X)`| Perform classification on samples in X.||`score(X, y[, sample_weight])`| Return the mean accuracy on the given test data and labels.||`set_params(**params)`| Set the parameters of this estimator.| We will choose a linear kernel as the distribution of the points can be linearly separated.<jupyter_code>clf = SVC(kernel='linear')<jupyter_output><empty_output><jupyter_text>Fit to the data the **SVM** classifier, denoted here by `clf`.<jupyter_code>clf.fit(X, y)<jupyter_output><empty_output><jupyter_text>Define a function `plot_svc_decision_function` that will plot **SVM** decision boundaries.<jupyter_code>def plot_svc_decision_function(clf, ax=None):
    """Plot the decision function for a 2D SVC"""
    if ax is None:
        ax = plt.gca()
    x = np.linspace(plt.xlim()[0], plt.xlim()[1], 30)
    y = np.linspace(plt.ylim()[0], plt.ylim()[1], 30)
    Y, X = np.meshgrid(y, x)
    P = np.zeros_like(X)
    for i, xi in enumerate(x):
        for j, yj in enumerate(y):
            P[i, j] = clf.decision_function([[xi, yj]])
    # plot the margins
    ax.contour(X, Y, P, colors='k', levels=[-1, 0, 1], alpha=0.5, linestyles=['--', '-', '--'])
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')
plot_svc_decision_function(clf)
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=200, facecolors='none');
plt.grid()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15342796738145575, 'fraction_numerical': 0.019027484143763214, 'mean_word_length': 5.088235294117647, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '209584', 'license': 'MIT', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1146, 'n_words': 329, 'path': 'Python/ml/clf-svm.ipynb', 'repo_name': 'a-mhamdi/cosnip', 'size': '3311'}