Example #1
0
def pick(event):

    global data
    global X
    global Y
    global numpy_container
    if event.key == 'q':
        if len(X) == 0: return
        if not numpy_container:
            data = VectorDataSet(X)
        else:
            data = PyVectorDataSet(numpy.array(X))
        data.attachLabels(Labels(Y))
        X = []
        Y = []
        print 'done creating data.  close this window and use the decisionSurface function'
        pylab.disconnect(binding_id)
    if event.key == '1' or event.key == '2':
        if event.inaxes is not None:
            print 'data coords', event.xdata, event.ydata
            X.append([event.xdata, event.ydata])
            Y.append(event.key)
            pylab.plot([event.xdata], [event.ydata],
                       plotStr[int(event.key) - 1])
            pylab.draw()
Example #2
0
def pick(event) :

    global data
    global X
    global Y
    global numpy_container
    if event.key == 'q' :
        if len(X) == 0 : return
        if not numpy_container :
            data = VectorDataSet(X)
        else :
            data = PyVectorDataSet(numpy.array(X))
        data.attachLabels(Labels(Y))
        X = []
        Y = []
        print 'done creating data.  close this window and use the decisionSurface function'
        pylab.disconnect(binding_id)
    if event.key =='1' or event.key == '2' :
        if event.inaxes is not None:
            print 'data coords', event.xdata, event.ydata
            X.append([event.xdata, event.ydata])
            Y.append(event.key)
            pylab.plot([event.xdata], [event.ydata], 
                       plotStr[int(event.key) - 1])
            pylab.draw()
Example #3
0
    def adjacent(self, i, j):

        xi = numpy.array(self.data.getPattern(i))
        xj = numpy.array(self.data.getPattern(j))
        stepSize = 1.0 / (self.lineSampleSize + 1)
        lambdas = numpy.arange(0, 1, stepSize)
        X = []
        for l in lambdas[1:]:
            X.append((xi * l + xj * (1 - l)).tolist())
        testdata = VectorDataSet(X)

        for i in range(len(testdata)):
            f = self.decisionFunc(testdata, i)
            if f < 0:
                return False
        return True
Example #4
0
def decisionSurface(classifier, fileName=None, **args):

    global data
    global numpy_container
    classifier.train(data)

    numContours = 3
    if 'numContours' in args:
        numContours = args['numContours']
    title = None
    if 'title' in args:
        title = args['title']
    markersize = 5
    fontsize = 'medium'
    if 'markersize' in args:
        markersize = args['markersize']
    if 'fontsize' in args:
        fontsize = args['fontsize']
    contourFontsize = 10
    if 'contourFontsize' in args:
        contourFontsize = args['contourFontsize']
    showColorbar = False
    if 'showColorbar' in args:
        showColorbar = args['showColorbar']
    show = True
    if fileName is not None:
        show = False
    if 'show' in args:
        show = args['show']

    # setting up the grid
    delta = 0.01
    if 'delta' in args:
        delta = args['delta']

    x = arange(xmin, xmax, delta)
    y = arange(ymin, ymax, delta)

    Z = numpy.zeros((len(x), len(y)), numpy.float)
    gridX = numpy.zeros((len(x) * len(y), 2), numpy.float)
    n = 0
    for i in range(len(x)):
        for j in range(len(y)):
            gridX[n][0] = x[i]
            gridX[n][1] = y[j]
            n += 1

    if not numpy_container:
        gridData = VectorDataSet(gridX)
        gridData.attachKernel(data.kernel)
    else:
        gridData = PyVectorDataSet(gridX)
    results = classifier.test(gridData)

    n = 0
    for i in range(len(x)):
        for j in range(len(y)):
            Z[i][j] = results.decisionFunc[n]
            n += 1

    #pylab.figure()
    im = pylab.imshow(numpy.transpose(Z),
                      interpolation='bilinear',
                      origin='lower',
                      cmap=pylab.cm.gray,
                      extent=(xmin, xmax, ymin, ymax))

    if numContours == 1:
        C = pylab.contour(numpy.transpose(Z), [0],
                          origin='lower',
                          linewidths=(3),
                          colors='black',
                          extent=(xmin, xmax, ymin, ymax))
    elif numContours == 3:
        C = pylab.contour(numpy.transpose(Z), [-1, 0, 1],
                          origin='lower',
                          linewidths=(1, 3, 1),
                          colors='black',
                          extent=(xmin, xmax, ymin, ymax))
    else:
        C = pylab.contour(numpy.transpose(Z),
                          numContours,
                          origin='lower',
                          linewidths=2,
                          extent=(xmin, xmax, ymin, ymax))

    pylab.clabel(C, inline=1, fmt='%1.1f', fontsize=contourFontsize)

    # plot the data
    scatter(data, markersize=markersize)
    xticklabels = pylab.getp(pylab.gca(), 'xticklabels')
    yticklabels = pylab.getp(pylab.gca(), 'yticklabels')
    pylab.setp(xticklabels, fontsize=fontsize)
    pylab.setp(yticklabels, fontsize=fontsize)

    if title is not None:
        pylab.title(title, fontsize=fontsize)
    if showColorbar:
        pylab.colorbar(im)

    # colormap:
    pylab.hot()
    if fileName is not None:
        pylab.savefig(fileName)
    if show:
        pylab.show()
Example #5
0
def decisionSurface(classifier, fileName = None, **args) :

    global data
    global numpy_container
    classifier.train(data)

    numContours = 3
    if 'numContours' in args :
        numContours = args['numContours']
    title = None
    if 'title' in args :
        title = args['title']
    markersize=5
    fontsize = 'medium'
    if 'markersize' in args :
        markersize = args['markersize']
    if 'fontsize' in args :
        fontsize = args['fontsize']
    contourFontsize = 10
    if 'contourFontsize' in args :
        contourFontsize = args['contourFontsize']    
    showColorbar = False
    if 'showColorbar' in args :
        showColorbar = args['showColorbar']
    show = True
    if fileName is not None :
        show = False
    if 'show' in args :
        show = args['show']

    # setting up the grid
    delta = 0.01
    if 'delta' in args :
        delta = args['delta']
        

    x = arange(xmin, xmax, delta)
    y = arange(ymin, ymax, delta)

    Z = numpy.zeros((len(x), len(y)), numpy.float)
    gridX = numpy.zeros((len(x) *len(y), 2), numpy.float)
    n = 0
    for i in range(len(x)) :
	for j in range(len(y)) :
	    gridX[n][0] = x[i]
	    gridX[n][1] = y[j]
	    n += 1

    if not numpy_container :
        gridData = VectorDataSet(gridX)
        gridData.attachKernel(data.kernel)
    else :
        gridData = PyVectorDataSet(gridX)
    results = classifier.test(gridData)

    n = 0
    for i in range(len(x)) :
	for j in range(len(y)) :
	    Z[i][j] = results.decisionFunc[n]
	    n += 1
    
    #pylab.figure()
    im = pylab.imshow(numpy.transpose(Z), 
                      interpolation='bilinear', origin='lower',
                      cmap=pylab.cm.gray, extent=(xmin,xmax,ymin,ymax) )

    if numContours == 1 :
        C = pylab.contour(numpy.transpose(Z),
                          [0],
                          origin='lower',
                          linewidths=(3),
                          colors = 'black',
                          extent=(xmin,xmax,ymin,ymax))
    elif numContours == 3 :
        C = pylab.contour(numpy.transpose(Z),
                          [-1,0,1],
                          origin='lower',
                          linewidths=(1,3,1),
                          colors = 'black',
                          extent=(xmin,xmax,ymin,ymax))
    else :
        C = pylab.contour(numpy.transpose(Z),
                          numContours,
                          origin='lower',
                          linewidths=2,
                          extent=(xmin,xmax,ymin,ymax))

    pylab.clabel(C,
                 inline=1,
                 fmt='%1.1f',
                 fontsize=contourFontsize)
    
    # plot the data
    scatter(data, markersize=markersize)
    xticklabels = pylab.getp(pylab.gca(), 'xticklabels')
    yticklabels = pylab.getp(pylab.gca(), 'yticklabels')
    pylab.setp(xticklabels, fontsize=fontsize)
    pylab.setp(yticklabels, fontsize=fontsize)

    if title is not None :
        pylab.title(title, fontsize=fontsize)
    if showColorbar :
        pylab.colorbar(im)

    # colormap:
    pylab.hot()
    if fileName is not None :
        pylab.savefig(fileName)
    if show :
        pylab.show()