Skip to content

Instantly share code, notes, and snippets.

@olinguyen
Created July 24, 2017 23:06
Show Gist options
  • Save olinguyen/7fd3bf7642ae952a7803579cb9561b5b to your computer and use it in GitHub Desktop.
Save olinguyen/7fd3bf7642ae952a7803579cb9561b5b to your computer and use it in GitHub Desktop.
sklearn and shogun's multiclass SVC produce different results (predictions and confidence values)
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
# import some data to play with
iris = datasets.load_iris()
num_samples = len(iris.data)
np.random.seed(seed=42)
idx = np.arange(num_samples)
np.random.shuffle(idx)
spl = int(num_samples * 0.70)
X_train = iris.data[idx][:spl, :2]
y_train = iris.target[idx][:spl]
X_test = iris.data[idx][spl:, :2]
y_test = iris.target[idx][spl:]
C = 1.0 # SVM regularization parameter
lsvc = svm.LinearSVC(C=C).fit(X_train, y_train)
sklearn_pred = lsvc.predict(X_test)
print("sklearn predictions:", sklearn_pred)
# Shogun
classifier = LibLinear()
strategy = MulticlassOneVsOneStrategy()
strategy = MulticlassOneVsRestStrategy()
mc_classifier = LinearMulticlassMachine(strategy, RealFeatures(X_train.T), classifier, MulticlassLabels(y_train.astype('d')))
mc_classifier.train()
y_pred = mc_classifier.apply_multiclass(RealFeatures(X_test.T))
print("Shogun predictions:", y_pred.get_labels())
print("sklearn confidence values:", lsvc.decision_function())
print("Shogun confidence values:", y_pred.get_values())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment