def train_svm(self):

        width = float(self.sigma.text())
        degree = int(self.degree.text())

        self.axes.clear()
        self.axes.grid(True)
        self.axes.plot(self.data.x1_pos, self.data.x2_pos, 'ro')
        self.axes.plot(self.data.x1_neg, self.data.x2_neg, 'bo')

        # train svm
        labels = self.data.get_labels()
        print type(labels)
        lab = BinaryLabels(labels)
        features = self.data.get_examples()
        train = RealFeatures(features)

        kernel_name = self.kernel_combo.currentText()
        print "current kernel is %s" % (kernel_name)

        if kernel_name == "LinearKernel":
            gk = LinearKernel(train, train)
            gk.set_normalizer(IdentityKernelNormalizer())
        elif kernel_name == "PolynomialKernel":
            gk = PolyKernel(train, train, degree, True)
            gk.set_normalizer(IdentityKernelNormalizer())
        elif kernel_name == "GaussianKernel":
            gk = GaussianKernel(train, train, width)

        cost = float(self.cost.text())

        print "cost", cost
        svm = LibSVM(cost, gk, lab)
        svm.train()
        svm.set_epsilon(1e-2)

        x, y, z = util.compute_output_plot_isolines(svm, gk, train)
        plt=self.axes.pcolor(x, y, z, shading='interp')
        CS=self.axes.contour(x, y, z, [-1,0,1], linewidths=1, colors='black', hold=True)
        #CS=self.axes.contour(x, y, z, linewidths=1, colors='black', hold=True)
        #CS=self.axes.contour(x, y, z, 5, linewidths=1, colors='black', hold=True)
        matplotlib.pyplot.clabel(CS, inline=1, fontsize=10)

        self.axes.set_xlim((-5,5))
        self.axes.set_ylim((-5,5))

        cmap = matplotlib.cm.jet
        norm = mpl.colors.Normalize(numpy.min(z), numpy.max(z))
        print CS.get_clim()
        if not self.cax:
            self.cax, kw = make_axes(self.axes)

# ColorbarBase derives from ScalarMappable and puts a colorbar
# in a specified axes, so it has everything needed for a
# standalone colorbar.  There are many more kwargs, but the
# following gives a basic continuous colorbar with ticks
# and labels.
        cb1 = mpl.colorbar.ColorbarBase(self.cax, cmap=cmap,
                                           norm=norm)
        self.canvas.draw()
Esempio n. 2
0
    def train_svm(self):

        width = float(self.sigma.text())
        degree = int(self.degree.text())

        self.axes.clear()
        self.axes.grid(True)
        self.axes.plot(self.data.x1_pos, self.data.x2_pos, 'ro')
        self.axes.plot(self.data.x1_neg, self.data.x2_neg, 'bo')

        # train svm
        labels = self.data.get_labels()
        print type(labels)
        lab = BinaryLabels(labels)
        features = self.data.get_examples()
        train = RealFeatures(features)

        kernel_name = self.kernel_combo.currentText()
        print "current kernel is %s" % (kernel_name)

        if kernel_name == "LinearKernel":
            gk = LinearKernel(train, train)
            gk.set_normalizer(IdentityKernelNormalizer())
        elif kernel_name == "PolynomialKernel":
            gk = PolyKernel(train, train, degree, True)
            gk.set_normalizer(IdentityKernelNormalizer())
        elif kernel_name == "GaussianKernel":
            gk = GaussianKernel(train, train, width)

        cost = float(self.cost.text())

        print "cost", cost
        svm = LibSVM(cost, gk, lab)
        svm.train()
        svm.set_epsilon(1e-2)

        x, y, z = util.compute_output_plot_isolines(svm, gk, train)
        plt=self.axes.pcolor(x, y, z)
        CS=self.axes.contour(x, y, z, [-1,0,1], linewidths=1, colors='black', hold=True)
        #CS=self.axes.contour(x, y, z, linewidths=1, colors='black', hold=True)
        #CS=self.axes.contour(x, y, z, 5, linewidths=1, colors='black', hold=True)
        matplotlib.pyplot.clabel(CS, inline=1, fontsize=10)

        self.axes.set_xlim((-5,5))
        self.axes.set_ylim((-5,5))

        cmap = matplotlib.cm.jet
        norm = matplotlib.colors.Normalize(numpy.min(z), numpy.max(z))
        print CS.get_clim()
        if not self.cax:
            self.cax, kw = make_axes(self.axes)

# ColorbarBase derives from ScalarMappable and puts a colorbar
# in a specified axes, so it has everything needed for a
# standalone colorbar.  There are many more kwargs, but the
# following gives a basic continuous colorbar with ticks
# and labels.
        cb1 = matplotlib.colorbar.ColorbarBase(self.cax, cmap=cmap,
                                           norm=norm)
        self.canvas.draw()
Esempio n. 3
0
util.set_title('SVM')
util.NUM_EXAMPLES = 200

width = 5

# positive examples
pos = util.get_realdata(True)
pylab.plot(pos[0, :], pos[1, :], "rs")

# negative examples
neg = util.get_realdata(False)
pylab.plot(neg[0, :], neg[1, :], "bo")

# train svm
labels = util.get_labels()
train = util.get_realfeatures(pos, neg)
gk = GaussianKernel(train, train, width)
svm = LibSVM(10.0, gk, labels)
svm.train()

x, y, z = util.compute_output_plot_isolines(svm, gk, train)
pylab.contour(x, y, z, linewidths=1, colors='black', hold=True)
pylab.axis('tight')
pylab.title('Binary SVM classification with Gaussian kernel')
pylab.xlabel('x')
pylab.ylabel('y')

pylab.connect('key_press_event', util.quit)
pylab.show()
Esempio n. 4
0
dense=util.get_realfeatures(pos, neg)
train=SparseRealFeatures()
train.obtain_from_simple(dense)
svm=SVMLin(C, train, labels)
svm.train()

lk=LinearKernel(dense, dense)
try:
	svmlight=LibSVM(C, lk, labels)
except NameError:
	print 'No SVMLight support available'
	import sys
	sys.exit(1)
svmlight.train()

x, y, z=util.compute_output_plot_isolines(svm, None, None, True, pos, neg)
x, y, zlight=util.compute_output_plot_isolines(svmlight, lk, dense, False, pos, neg)

c=pcolor(x, y, z, shading='interp')
contour(x, y, z, linewidths=1, colors='black', hold=True)
colorbar(c)
scatter(pos[0,:], pos[1,:],s=20, c='r')
scatter(neg[0,:], neg[1,:],s=20, c='b')
axis('tight')
connect('key_press_event', util.quit)

figure()
util.set_title('SVM Linear 2')
c=pcolor(x, y, zlight, shading='interp')
contour(x, y, zlight, linewidths=1, colors='black', hold=True)
colorbar(c)
Esempio n. 5
0
    pos = util.get_realdata(True)
    neg = util.get_realdata(False)
    traindatList[i] = concatenate((pos, neg), axis=1)
    trainfeatList[i] = util.get_realfeatures(pos, neg)
    trainlabsList[i] = util.get_labels(True)
    trainlabList[i] = util.get_labels()
    kernelList[i] = GaussianKernel(trainfeatList[i], trainfeatList[i], width)
    svmList[i] = LibSVM(10, kernelList[i], trainlabList[i])

for i in range(num_svms):
    print "Training svm nr. %d" % (i)
    currentSVM = svmList[i]
    currentSVM.train()
    print currentSVM.get_num_support_vectors()
    print "Done."
    x, y, z = util.compute_output_plot_isolines(currentSVM, kernelList[i],
                                                trainfeatList[i])
    subplot(num_svms / 2, 2, i + 1)
    pcolor(x, y, z)
    contour(x, y, z, linewidths=1, colors='black', hold=True)
    scatter(traindatList[i][0, :],
            traindatList[i][1, :],
            s=20,
            marker='o',
            c=trainlabsList[i],
            hold=True)
    axis('tight')

connect('key_press_event', util.quit)
show()
Esempio n. 6
0
import util

util.set_title('SVM')
util.NUM_EXAMPLES=200

width=5

# positive examples
pos=util.get_realdata(True)
plot(pos[0,:], pos[1,:], "r.")

# negative examples
neg=util.get_realdata(False)
plot(neg[0,:], neg[1,:], "b.")

# train svm
labels=util.get_labels()
train=util.get_realfeatures(pos, neg)
gk=GaussianKernel(train, train, width)
svm = LibSVM(10.0, gk, labels)
svm.train()

x, y, z=util.compute_output_plot_isolines(svm, gk, train)
pcolor(x, y, z, shading='interp')
contour(x, y, z, linewidths=1, colors='black', hold=True)
axis('tight')

connect('key_press_event', util.quit)
show()

Esempio n. 7
0
import util

util.set_title('LDA')
util.DISTANCE = 0.5

gamma = 0.1

# positive examples
pos = util.get_realdata(True)
plot(pos[0, :], pos[1, :], "r.")

# negative examples
neg = util.get_realdata(False)
plot(neg[0, :], neg[1, :], "b.")

# train lda
labels = util.get_labels()
features = util.get_realfeatures(pos, neg)
lda = LDA(gamma, features, labels)
lda.train()

# compute output plot iso-lines
x, y, z = util.compute_output_plot_isolines(lda)

c = pcolor(x, y, z)
contour(x, y, z, linewidths=1, colors='black', hold=True)
colorbar(c)

connect('key_press_event', util.quit)
show()
Esempio n. 8
0
trainlabList = [None]*num_svms
trainlabsList = [None]*num_svms
kernelList = [None]*num_svms

for i in range(num_svms):
	pos=util.get_realdata(True)
	neg=util.get_realdata(False)
	traindatList[i] = concatenate((pos, neg), axis=1)
	trainfeatList[i] = util.get_realfeatures(pos, neg)
	trainlabsList[i] = util.get_labels(True)
	trainlabList[i] = util.get_labels()
	kernelList[i] = GaussianKernel(trainfeatList[i], trainfeatList[i], width)
	svmList[i] = LibSVM(10, kernelList[i], trainlabList[i])

for i in range(num_svms):
	print "Training svm nr. %d" % (i)
	currentSVM = svmList[i]
	currentSVM.train()
	print currentSVM.get_num_support_vectors()
	print "Done."
	x, y, z=util.compute_output_plot_isolines(
		currentSVM, kernelList[i], trainfeatList[i])
	subplot(num_svms/2, 2, i+1)
	pcolor(x, y, z, shading='interp')
	contour(x, y, z, linewidths=1, colors='black', hold=True)
	scatter(traindatList[i][0,:],traindatList[i][1,:], s=20, marker='o', c=trainlabsList[i], hold=True)
	axis('tight')

connect('key_press_event', util.quit)
show()
Esempio n. 9
0
util.set_title('KernelRidgeRegression')

width = 20

# positive examples
pos = util.get_realdata(True)
plot(pos[0, :], pos[1, :], "r.")

# negative examples
neg = util.get_realdata(False)
plot(neg[0, :], neg[1, :], "b.")

# train krr
labels = util.get_labels(type='regression')
train = util.get_realfeatures(pos, neg)
gk = GaussianKernel(train, train, width)
krr = KernelRidgeRegression()
krr.set_labels(labels)
krr.set_kernel(gk)
krr.set_tau(1e-3)
krr.train()

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

pcolor(x, y, z)
contour(x, y, z, linewidths=1, colors='black', hold=True)

connect('key_press_event', util.quit)
show()
width=20

# positive examples
pos=util.get_realdata(True)
plot(pos[0,:], pos[1,:], "r.")

# negative examples
neg=util.get_realdata(False)
plot(neg[0,:], neg[1,:], "b.")

# train svm
labels = util.get_labels(type='regression')
train = util.get_realfeatures(pos, neg)
gk=GaussianKernel(train, train, width)
krr = KernelRidgeRegression()
krr.set_labels(labels)
krr.set_kernel(gk)
krr.set_tau(1e-3)
krr.train()

# compute output plot iso-lines
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)

connect('key_press_event', util.quit)
show()

Esempio n. 11
0
util.set_title('LDA')
util.DISTANCE=0.5

gamma=0.1

# positive examples
pos=util.get_realdata(True)
plot(pos[0,:], pos[1,:], "r.")

# negative examples
neg=util.get_realdata(False)
plot(neg[0,:], neg[1,:], "b.")

# train lda
labels=util.get_labels()
features=util.get_realfeatures(pos, neg)
lda=LDA(gamma, features, labels)
lda.train()

# compute output plot iso-lines
x, y, z=util.compute_output_plot_isolines(lda)

c=pcolor(x, y, z, shading='interp')
contour(x, y, z, linewidths=1, colors='black', hold=True)
colorbar(c)

connect('key_press_event', util.quit)
show()