Skip to content

Commit

Permalink
Updated python graphical examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed May 29, 2012
1 parent 80e75c8 commit fb4d081
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -86,4 +86,5 @@ configure.log
/examples/undocumented/libshogun/*
!/examples/undocumented/libshogun/*.cpp
examples/undocumented/python_modular/*
!examples/undocumented/python_modular/graphical/
!examples/undocumented/python_modular/*.py
Expand Up @@ -98,7 +98,7 @@ def train_svm(self):
# train svm
labels = self.data.get_labels()
print type(labels)
lab = Labels(labels)
lab = BinaryLabels(labels)
features = self.data.get_examples()
train = RealFeatures(features)

Expand Down
Expand Up @@ -108,7 +108,7 @@ def train_svm(self):
# train svm
labels = self.data.get_labels()
print type(labels)
lab = Labels(labels)
lab = BinaryLabels(labels)
features = self.data.get_examples()
train = RealFeatures(features)

Expand Down
Expand Up @@ -101,7 +101,7 @@ def train_svm(self):
# train svm
labels = self.data.get_labels()
print type(labels)
lab = Labels(labels)
lab = RegressionLabels(labels)
features = self.data.get_examples()
train = RealFeatures(features)

Expand All @@ -126,7 +126,7 @@ def train_svm(self):
svm.set_epsilon(1e-2)

x=numpy.linspace(-5.0,5.0,100)
y=svm.apply(RealFeatures(numpy.array([x]))).get_labels()
y=svm.apply(RealFeatures(numpy.array([x]))).get_confidences()

self.axes.plot(x,y,'r-')

Expand Down
Expand Up @@ -19,7 +19,7 @@
plot(neg[0,:], neg[1,:], "b.")

# train svm
labels = util.get_labels()
labels = util.get_labels(type='regression')
train = util.get_realfeatures(pos, neg)
gk=GaussianKernel(train, train, width)
krr = KernelRidgeRegression()
Expand All @@ -29,7 +29,7 @@
krr.train()

# compute output plot iso-lines
x, y, z=util.compute_output_plot_isolines(krr, gk, train)
x, y, z=util.compute_output_plot_isolines(krr, gk, train, regression=True)

pcolor(x, y, z, shading='interp')
contour(x, y, z, linewidths=1, colors='black', hold=True)
Expand Down
Expand Up @@ -11,7 +11,7 @@
width=1

feat=RealFeatures(X)
lab=Labels(Y.flatten())
lab=RegressionLabels(Y.flatten())
gk=GaussianKernel(feat, feat, width)
krr=KernelRidgeRegression()
krr.set_labels(lab)
Expand All @@ -23,7 +23,7 @@
plot(X[0], krr.apply().get_labels(), hold=True, label='train output')

XE, YE=util.compute_output_plot_isolines_sine(krr, gk, feat)
YE200=krr.apply(200)
YE200=krr.apply_one(200)

plot(XE[0], YE, hold=True, label='test output')
plot([XE[0,200]], [YE200], '+', hold=True)
Expand Down
Expand Up @@ -11,7 +11,7 @@

from scipy import linalg
from shogun.Classifier import QDA
from shogun.Features import RealFeatures, Labels
from shogun.Features import RealFeatures, MulticlassLabels

# colormap
cmap = mpl.colors.LinearSegmentedColormap('color_classes',
Expand Down Expand Up @@ -102,7 +102,7 @@ def plot_regions(qda):

X, y = gen_data()

labels = Labels(y)
labels = MulticlassLabels(y)
features = RealFeatures(X.T)
qda = QDA(features, labels, 1e-4, True)
qda.train()
Expand Down
4 changes: 2 additions & 2 deletions examples/undocumented/python_modular/graphical/qda.py
@@ -1,5 +1,5 @@
from shogun.Features import RealFeatures
from shogun.Features import Labels
from shogun.Features import MulticlassLabels
from shogun.Classifier import QDA
from pylab import pcolor, contour, colorbar, connect, show, plot, axis

Expand All @@ -25,7 +25,7 @@
plot(x_neg, y_neg, 'ro');

# train qda
labels = Labels( np.concatenate([np.zeros(N), np.ones(N)]) )
labels = MulticlassLabels( np.concatenate([np.zeros(N), np.ones(N)]) )
pos = np.array([x_pos, y_pos])
neg = np.array([x_neg, y_neg])
features = RealFeatures( np.array(np.concatenate([pos, neg], 1)) )
Expand Down
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import matplotlib.pyplot as plt

from shogun.Features import Labels, RealFeatures
from shogun.Features import RegressionLabels, RealFeatures
from shogun.Regression import LeastAngleRegression, LinearRidgeRegression, LeastSquaresRegression

# we compare LASSO with ordinary least-squares (OLE)
Expand Down Expand Up @@ -43,7 +43,7 @@

# train LASSO
LeastAngleRegression = LeastAngleRegression()
LeastAngleRegression.set_labels(Labels(y))
LeastAngleRegression.set_labels(RegressionLabels(y))
LeastAngleRegression.train(RealFeatures(X.T))

# train ordinary LSR
Expand All @@ -52,7 +52,7 @@
lsr.train()
else:
lsr = LeastSquaresRegression()
lsr.set_labels(Labels(y))
lsr.set_labels(RegressionLabels(y))
lsr.train(RealFeatures(X.T))

# gather LASSO path
Expand Down
4 changes: 2 additions & 2 deletions examples/undocumented/python_modular/graphical/svr_sinc.py
Expand Up @@ -12,14 +12,14 @@
epsilon=0.01

feat = RealFeatures(X)
lab = Labels(Y.flatten())
lab = RegressionLabels(Y.flatten())
gk=GaussianKernel(feat,feat, width)
#svr = SVRLight(C, epsilon, gk, lab)
svr = LibSVR(C, epsilon, gk, lab)
svr.train()

plot(X, Y, '.', label='train data')
plot(X[0], svr.apply().get_labels(), hold=True, label='train output')
plot(X[0], svr.apply().get_confidences(), hold=True, label='train output')

XE, YE=util.compute_output_plot_isolines_sine(svr, gk, feat)
plot(XE[0], YE, hold=True, label='test output')
Expand Down
20 changes: 14 additions & 6 deletions examples/undocumented/python_modular/graphical/util.py
Expand Up @@ -4,7 +4,7 @@
from numpy import ones, array, double, meshgrid, reshape, linspace, \
concatenate, ravel, pi, sinc
from numpy.random import randn, rand
from shogun.Features import Labels, RealFeatures, SparseRealFeatures
from shogun.Features import BinaryLabels, RegressionLabels, RealFeatures, SparseRealFeatures

QUITKEY='q'
NUM_EXAMPLES=100
Expand Down Expand Up @@ -42,17 +42,21 @@ def get_realfeatures(pos, neg):
return RealFeatures(features)


def get_labels(raw=False):
def get_labels(raw=False, type='binary'):
data = concatenate(array(
(-ones(NUM_EXAMPLES, dtype=double), ones(NUM_EXAMPLES, dtype=double))
))
if raw:
return data
else:
return Labels(data)
if type == 'binary':
return BinaryLabels(data)
if type == 'regression':
return RegressionLabels(data)
return None


def compute_output_plot_isolines(classifier, kernel=None, train=None, sparse=False, pos=None, neg=None):
def compute_output_plot_isolines(classifier, kernel=None, train=None, sparse=False, pos=None, neg=None, regression=False):
size=100
if pos is not None and neg is not None:
x1_max=max(1.2*pos[0,:])
Expand All @@ -79,7 +83,11 @@ def compute_output_plot_isolines(classifier, kernel=None, train=None, sparse=Fal
else:
classifier.set_features(test)

labels=classifier.apply().get_labels()
labels = None
if regression:
labels=classifier.apply().get_labels()
else:
labels=classifier.apply().get_confidences()
z=labels.reshape((size, size))

return x, y, z
Expand All @@ -98,6 +106,6 @@ def compute_output_plot_isolines_sine(classifier, kernel, train):
x.sort()
test=RealFeatures(x)
kernel.init(train, test)
y=classifier.apply().get_labels()
y=classifier.apply().get_confidences()

return x, y

0 comments on commit fb4d081

Please sign in to comment.