Esempio n. 1
0
def bench_pymvpa(X, y, T, valid):
#
#       .. PyMVPA ..
#
    from mvpa.datasets import dataset_wizard
    from mvpa.clfs import knn as mvpa_knn
    start = datetime.now()
    data = dataset_wizard(X, y)
    mvpa_clf = mvpa_knn.kNN(k=n_neighbors)
    mvpa_clf.train(data)
    score = np.mean(mvpa_clf.predict(T) == valid)
    return score, datetime.now() - start
def bench_pymvpa(X, y, T, valid):
    #
    #       .. PyMVPA ..
    #
    from mvpa.datasets import dataset_wizard
    from mvpa.clfs import knn as mvpa_knn
    start = datetime.now()
    data = dataset_wizard(X, y)
    mvpa_clf = mvpa_knn.kNN(k=n_neighbors)
    mvpa_clf.train(data)
    score = np.mean(mvpa_clf.predict(T) == valid)
    return score, datetime.now() - start
Esempio n. 3
0
    def testKNNState(self):
        train = pureMultivariateSignal( 20, 3 )
        test = pureMultivariateSignal( 20, 3 )

        clf = kNN(k=10)
        clf.train(train)

        clf.states.enable('values')
        clf.states.enable('predictions')

        p = clf.predict(test.samples)

        self.failUnless(p == clf.predictions)
        self.failUnless(N.array(clf.values).shape == (80,2))
Esempio n. 4
0
    def test_knn_state(self):
        train = pure_multivariate_signal( 40, 3 )
        test = pure_multivariate_signal( 20, 3 )

        clf = kNN(k=10)
        clf.train(train)

        clf.ca.enable(['estimates', 'predictions', 'distances'])

        p = clf.predict(test.samples)

        self.failUnless(p == clf.ca.predictions)
        self.failUnless(np.array(clf.ca.estimates).shape == (80,2))
        self.failUnless(clf.ca.distances.shape == (80,160))

        self.failUnless(not clf.ca.distances.fa is train.sa)
        # Those are deep-copied now by default so they should not be the same
        self.failUnless(not (clf.ca.distances.fa['chunks'] is train.sa['chunks']))
        self.failUnless(not (clf.ca.distances.fa.chunks is train.sa.chunks))
Esempio n. 5
0
    def test_knn_state(self):
        train = pure_multivariate_signal( 40, 3 )
        test = pure_multivariate_signal( 20, 3 )

        clf = kNN(k=10)
        clf.train(train)

        clf.ca.enable(['estimates', 'predictions', 'distances'])

        p = clf.predict(test.samples)

        self.failUnless(p == clf.ca.predictions)
        self.failUnless(np.array(clf.ca.estimates).shape == (80,2))
        self.failUnless(clf.ca.distances.shape == (80,160))

        self.failUnless(not clf.ca.distances.fa is train.sa)
        # XXX if we revert to deepcopy for that state following test
        # should fail
        self.failUnless(clf.ca.distances.fa['chunks'] is train.sa['chunks'])
        self.failUnless(clf.ca.distances.fa.chunks is train.sa.chunks)
Esempio n. 6
0
    def test_multivariate(self):

        mv_perf = []
        uv_perf = []

        clf = kNN(k=10)
        for i in xrange(20):
            train = pure_multivariate_signal( 20, 3 )
            test = pure_multivariate_signal( 20, 3 )
            clf.train(train)
            p_mv = clf.predict( test.samples )
            mv_perf.append( np.mean(p_mv==test.targets) )

            clf.train(train[:, 0])
            p_uv = clf.predict(test[:, 0].samples)
            uv_perf.append( np.mean(p_uv==test.targets) )

        mean_mv_perf = np.mean(mv_perf)
        mean_uv_perf = np.mean(uv_perf)

        self.failUnless( mean_mv_perf > 0.9 )
        self.failUnless( mean_uv_perf < mean_mv_perf )
Esempio n. 7
0
    def testMultivariate(self):

        mv_perf = []
        uv_perf = []

        clf = kNN(k=10)
        for i in xrange(20):
            train = pureMultivariateSignal( 20, 3 )
            test = pureMultivariateSignal( 20, 3 )
            clf.train(train)
            p_mv = clf.predict( test.samples )
            mv_perf.append( N.mean(p_mv==test.labels) )

            clf.train(train.selectFeatures([0]))
            p_uv = clf.predict( test.selectFeatures([0]).samples )
            uv_perf.append( N.mean(p_uv==test.labels) )

        mean_mv_perf = N.mean(mv_perf)
        mean_uv_perf = N.mean(uv_perf)

        self.failUnless( mean_mv_perf > 0.9 )
        self.failUnless( mean_uv_perf < mean_mv_perf )
Esempio n. 8
0
    def test_multivariate(self):

        mv_perf = []
        uv_perf = []

        clf = kNN(k=10)
        for i in xrange(20):
            train = pure_multivariate_signal( 20, 3 )
            test = pure_multivariate_signal( 20, 3 )
            clf.train(train)
            p_mv = clf.predict( test.samples )
            mv_perf.append( np.mean(p_mv==test.targets) )

            clf.train(train[:, 0])
            p_uv = clf.predict(test[:, 0].samples)
            uv_perf.append( np.mean(p_uv==test.targets) )

        mean_mv_perf = np.mean(mv_perf)
        mean_uv_perf = np.mean(uv_perf)

        self.failUnless( mean_mv_perf > 0.9 )
        self.failUnless( mean_uv_perf < mean_mv_perf )
Esempio n. 9
0
        _lars_tags = ['lars', 'linear', 'regression', 'does_feature_selection']

        _lars = SKLLearnerAdapter(sklLARS(),
                                  tags=_lars_tags,
                                  descr='skl.LARS()')

        _lasso_lars = SKLLearnerAdapter(sklLassoLARS(),
                                        tags=_lars_tags,
                                        descr='skl.LassoLARS()')

        regrswh += [_lars, _lasso_lars]
        clfswh += [RegressionAsClassifier(_lars, descr="skl.LARS_C()"),
                   RegressionAsClassifier(_lasso_lars, descr="skl.LassoLARS_C()")]

# kNN
clfswh += kNN(k=5, descr="kNN(k=5)")
clfswh += kNN(k=5, voting='majority', descr="kNN(k=5, voting='majority')")

clfswh += \
    FeatureSelectionClassifier(
        kNN(),
        SensitivityBasedFeatureSelection(
           SMLRWeights(SMLR(lm=1.0, implementation="C"),
                       postproc=maxofabs_sample()),
           RangeElementSelector(mode='select')),
        descr="kNN on SMLR(lm=1) non-0")

clfswh += \
    FeatureSelectionClassifier(
        kNN(),
        SensitivityBasedFeatureSelection(
Esempio n. 10
0
#pymvpa stuff				
f_handle = open("classdatafile.txt", 'r')
f_handle2 = open("classidfile.txt", 'r')
f_handle3 = open("predictdata.txt", 'r')
features = genfromtxt(f_handle, dtype = float)
classes = genfromtxt(f_handle2, dtype = int)
predictdata = genfromtxt(f_handle3, dtype = float)
predictdata = np.expand_dims(predictdata, axis=0)
print predictdata
print np.shape(features), features.ndim, features.dtype
print np.shape(classes), classes.ndim, classes.dtype
print np.shape(predictdata), predictdata.ndim, predictdata.dtype
f_handle.close()
f_handle2.close()
f_handle3.close()

training = Dataset(samples=features,labels=classes)
clf = kNN(k=2)
print "clf = ", clf
clf.train(training)
#print np.mean(clf.predict(training.samples) == training.labels)
classID =  clf.predict(predictdata)
print "classID = ", classID
#print clf.trained_labels
if classID[0] == 1: print "Image is of class: GRASS"
if classID[0] == 2: print "Image is of class: DIRT/GRAVEL"
if classID[0] == 3: print "Image is of class: CEMENT/ASPHALT"


Esempio n. 11
0
from mvpa.datasets import Dataset

#pymvpa stuff
f_handle = open("classdatafile.txt", 'r')
f_handle2 = open("classidfile.txt", 'r')
f_handle3 = open("predictdata.txt", 'r')
features = genfromtxt(f_handle, dtype=float)
classes = genfromtxt(f_handle2, dtype=int)
predictdata = genfromtxt(f_handle3, dtype=float)
predictdata = np.expand_dims(predictdata, axis=0)
print predictdata
print np.shape(features), features.ndim, features.dtype
print np.shape(classes), classes.ndim, classes.dtype
print np.shape(predictdata), predictdata.ndim, predictdata.dtype
f_handle.close()
f_handle2.close()
f_handle3.close()

training = Dataset(samples=features, labels=classes)
clf = kNN(k=2)
print "clf = ", clf
clf.train(training)
#print np.mean(clf.predict(training.samples) == training.labels)
classID = clf.predict(predictdata)
print "classID = ", classID
#print clf.trained_labels
if classID[0] == 1: print "Image is of class: GRASS"
if classID[0] == 2: print "Image is of class: DIRT/GRAVEL"
if classID[0] == 3:
    print "Image is of class: CEMENT/ASPHALT"
Esempio n. 12
0
## Still fails unittests battery although overhauled otherwise.
## # enet from R via RPy2
## if externals.exists('elasticnet'):
##     from mvpa.clfs.enet import ENET
##     clfswh += RegressionAsClassifier(ENET(),
##                                      descr="RegressionAsClassifier(ENET())")
##     regrswh += ENET(descr="ENET()")

# glmnet from R via RPy
if externals.exists('glmnet'):
    from mvpa.clfs.glmnet import GLMNET_C, GLMNET_R
    clfswh += GLMNET_C(descr="GLMNET_C()")
    regrswh += GLMNET_R(descr="GLMNET_R()")

# kNN
clfswh += kNN(k=5, descr="kNN(k=5)")
clfswh += kNN(k=5, voting='majority', descr="kNN(k=5, voting='majority')")

clfswh += \
    FeatureSelectionClassifier(
        kNN(),
        SensitivityBasedFeatureSelection(
           SMLRWeights(SMLR(lm=1.0, implementation="C"),
                       postproc=maxofabs_sample()),
           RangeElementSelector(mode='select')),
        descr="kNN on SMLR(lm=1) non-0")

clfswh += \
    FeatureSelectionClassifier(
        kNN(),
        SensitivityBasedFeatureSelection(
Esempio n. 13
0
from mvpa.clfs.knn import kNN
from mvpa.misc.plot import *

mvpa.seed(0)  # to reproduce the plot

dataset_kwargs = dict(nfeatures=2, nchunks=10, snr=2, nlabels=4, means=[[0, 1], [1, 0], [1, 1], [0, 0]])

dataset_train = normal_feature_dataset(**dataset_kwargs)
dataset_plot = normal_feature_dataset(**dataset_kwargs)


# make a new figure
pl.figure(figsize=(9, 9))

for i, k in enumerate((1, 3, 9, 20)):
    knn = kNN(k)

    print "Processing kNN(%i) problem..." % k
    pl.subplot(2, 2, i + 1)

    """
    """

    knn.train(dataset_train)

    plot_decision_boundary_2d(dataset_plot, clf=knn, maps="targets")

if cfg.getboolean("examples", "interactive", True):
    # show all the cool figures
    pl.show()
Esempio n. 14
0
rgb_histo = np.asarray(rgb_histo)

id_index = 15
lbp_predictdata = lbp[[id_index]]
i3_histo_predictdata = lbp[[id_index]]

print
#print predictdata
print classID[id_index]
#print "len lbp:", len(lbp)
#print "shape:", lbp.shape

#mvpa
lbp_training = Dataset(samples=lbp,labels=classID)
i3_histo_training = Dataset(samples=lbp,labels=classID)
clf = kNN(k=1, voting='majority')
print "clf = ", clf
clf.train(lbp_training)
lbp_predicted_classID =  clf.predict(lbp_predictdata)
clf.train(i3_histo_training)
i3_histo_predicted_classID =  clf.predict(i3_histo_predictdata)



print "lbp_predicted_classID: ", lbp_predicted_classID 
print "i3_histo__predicted_classID :", i3_histo_predicted_classID
#if predicted_classID[0]  == 1.0: print "Image is of class: GRASS"
#if predicted_classID[0]  == 2.0: print "Image is of class: DIRT/GRAVEL"
#if predicted_classID[0]  == 3.0: print "Image is of class: CEMENT/ASPHALT"

#mlpy
Esempio n. 15
0
mvpa.seed(0)  # to reproduce the plot

dataset_kwargs = dict(nfeatures=2,
                      nchunks=10,
                      snr=2,
                      nlabels=4,
                      means=[[0, 1], [1, 0], [1, 1], [0, 0]])

dataset_train = normal_feature_dataset(**dataset_kwargs)
dataset_plot = normal_feature_dataset(**dataset_kwargs)

# make a new figure
pl.figure(figsize=(9, 9))

for i, k in enumerate((1, 3, 9, 20)):
    knn = kNN(k)

    print "Processing kNN(%i) problem..." % k
    pl.subplot(2, 2, i + 1)
    """
    """

    knn.train(dataset_train)

    plot_decision_boundary_2d(dataset_plot, clf=knn, maps='targets')

if cfg.getboolean('examples', 'interactive', True):
    # show all the cool figures
    pl.show()