Code snippets for page Graph-based SFA (GSFA)ΒΆ

Download gsfa.py. Browse the code snippet index.

# -*- coding: utf-8 -*-
# Generated by codesnippet sphinx extension on 2020-12-16

import mdp
import numpy as np
np.random.seed(0)
import matplotlib
import matplotlib.pyplot as plt
from sklearn import (datasets, model_selection, metrics)
from sklearn.svm import SVC

data, label = datasets.load_breast_cancer(return_X_y=True)
data_train, data_test, label_train, label_test = \
model_selection.train_test_split(data, label, test_size=0.2)

SVM_clf_train = SVC(gamma='auto')
SVM_clf_train.fit(data_train, label_train)
SVM_prediction_train = SVM_clf_train.predict(data_train)
print("SVM train score: ",
    metrics.accuracy_score(label_train, SVM_prediction_train))
# Expected:
## SVM train score:  1.0

SVM_clf_test = SVC(gamma='auto')
SVM_clf_test.fit(data_train, label_train)
SVM_prediction_test = SVM_clf_test.predict(data_test)
print("SVM test score: ",
    metrics.accuracy_score(label_test, SVM_prediction_test))
# Expected:
## SVM test score:  0.5877192982456141

output_dim = 2
GSFA_n = mdp.nodes.GSFANode(output_dim=output_dim)
GSFA_n.train(data_train, train_mode=("classification", label_train, 1))
GSFA_n.stop_training()

GSFA_train = GSFA_n.execute(data_train)
GSFA_test = GSFA_n.execute(data_test)

colors = ['red', 'blue']
f = plt.figure(figsize=(15,6))
ax = f.add_subplot(121)
ax2 = f.add_subplot(122)
ax.scatter(GSFA_train[:, 0], GSFA_train[:, 1], c=label_train,
    cmap=matplotlib.colors.ListedColormap(colors))
ax2.scatter(GSFA_test[:, 0], GSFA_test[:, 1], c=label_test,
    cmap=matplotlib.colors.ListedColormap(colors))
ax.set_title("Train data in 2-D")
ax2.set_title("Test data in 2-D")
plt.show()

GSFA_clf = SVC(gamma='auto')
GSFA_clf.fit(GSFA_train, label_train)
GSFA_SVM_test = GSFA_clf.predict(GSFA_test)
print("GSFA dimension reduction + SVM score: ", metrics.accuracy_score(label_test, GSFA_SVM_test))
# Expected:
## GSFA dimension reduction + SVM score:  0.9649122807017544