image_output_1.png
from matplotlib.colors import ListedColormap

from sklearn.datasets import make_classification, make_blobs,make_gaussian_quantiles, make_circles,make_moons

from sklearn.decomposition import PCA

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

from sklearn.svm import SVC

import matplotlib.pyplot as plt

import numpy as np # linear algebra

n_sam = 250
n_fea = 8
n_clas = 2
df = make_classification(n_samples=n_sam, n_features=n_fea, n_classes=n_clas, random_state=10)
df2 = make_blobs(n_samples=n_sam, centers=2, random_state=10)
df3 = make_circles(n_samples=n_sam, random_state=10)
df4 = make_moons(n_samples=n_sam, random_state=10)

from sklearn.decomposition import PCA
pca = PCA(n_components=2)
df1_PCA = pca.fit_transform(df[0])
y = df[1]
df1 = (df1_PCA, y)

datasets = [df1, df2, df3, df4]

from sklearn.svm import SVC
classifiers = [SVC(kernel='linear', C=0.025), SVC(C=100, kernel='poly', degree=4), SVC(gamma=2, C=1)]
names = ['Linear SVM', 'Polinomial SVM', 'RBF SVM']
h = 0.02

from matplotlib.colors import ListedColormap
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
figure = plt.figure(figsize=(15, 9))
i = 1
for ds_cnt, ds in enumerate(datasets):
    X, y = ds
    X = StandardScaler().fit_transform(X)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42)
    x_min, x_max = (X[:, 0].min() - 0.5, X[:, 0].max() + 0.5)
    y_min, y_max = (X[:, 1].min() - 0.5, X[:, 1].max() + 0.5)
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    cm = plt.cm.RdBu
    cm_bright = ListedColormap(['#FF0000', '#0000FF'])
    ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
    if ds_cnt == 0:
        ax.set_title('Input data')
    ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright, edgecolors='k')
    ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6, edgecolors='k')
    ax.set_xlim(xx.min(), xx.max())
    ax.set_ylim(yy.min(), yy.max())
    ax.set_xticks(())
    ax.set_yticks(())
    i += 1
    for name, clf in zip(names, classifiers):
        ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
        clf.fit(X_train, y_train)
        score = clf.score(X_test, y_test)
        if hasattr(clf, 'decision_function'):
            Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
        else:
            Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
        Z = Z.reshape(xx.shape)
        ax.contourf(xx, yy, Z, cmap=cm, alpha=0.8)
        ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright, edgecolors='k')
        ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, edgecolors='k', alpha=0.6)
        ax.set_xlim(xx.min(), xx.max())
        ax.set_ylim(yy.min(), yy.max())
        ax.set_xticks(())
        ax.set_yticks(())
        if ds_cnt == 0:
            ax.set_title(name)
        ax.text(xx.max() - 0.3, yy.min() + 0.3, ('%.2f' % score).lstrip('0'), size=15, horizontalalignment='right')
        i += 1
plt.tight_layout()
plt.show()