Ejemplo n.º 1
0
    def test_model_based(self):
        icp = InductiveClassifier(
            InverseProbability(SVMLearner(probability=True)), self.train,
            self.calibrate)
        pred = icp(self.test.x, 0.1)
        self.assertEqual(pred, ['Iris-setosa'])

        icp = InductiveClassifier(
            ProbabilityMargin(SVMLearner(probability=True)), self.train,
            self.calibrate)
        pred = icp(self.test.x, 0.1)
        self.assertEqual(pred, ['Iris-setosa'])
Ejemplo n.º 2
0
def main():
    import sip
    from PyQt4.QtGui import QApplication
    from Orange.classification import (LogisticRegressionLearner, SVMLearner,
                                       NuSVMLearner)

    app = QApplication([])
    w = OWCalibrationPlot()
    w.show()
    w.raise_()

    data = Orange.data.Table("ionosphere")
    results = Orange.evaluation.CrossValidation(data, [
        LogisticRegressionLearner(penalty="l2"),
        LogisticRegressionLearner(penalty="l1"),
        SVMLearner(probability=True),
        NuSVMLearner(probability=True)
    ],
                                                store_data=True)
    results.learner_names = ["LR l2", "LR l1", "SVM", "Nu SVM"]
    w.set_results(results)
    rval = app.exec_()

    sip.delete(w)
    del w
    app.processEvents()
    del app
    return rval
Ejemplo n.º 3
0
def main():
    import gc
    import sip
    from PyQt4.QtGui import QApplication
    from Orange.classification import (LogisticRegressionLearner, SVMLearner,
                                       NuSVMLearner)

    app = QApplication([])
    w = OWROCAnalysis()
    w.show()
    w.raise_()

#     data = Orange.data.Table("iris")
    data = Orange.data.Table("ionosphere")
    results = Orange.evaluation.CrossValidation(
        data,
        [LogisticRegressionLearner(),
         LogisticRegressionLearner(penalty="l1"),
         SVMLearner(probability=True),
         NuSVMLearner(probability=True)],
        k=5,
        store_data=True,
    )
    results.learner_names = ["Logistic", "Logistic (L1 reg.)", "SVM", "NuSVM"]
    w.set_results(results)

    rval = app.exec_()
    w.deleteLater()
    sip.delete(w)
    del w
    app.processEvents()
    sip.delete(app)
    del app
    gc.collect()
    return rval
    def test_raise_no_classifier_error(self):
        """
        Regression learner must raise error
        """
        w = self.widget

        # linear regression learner is regression - should raise
        learner = LinearRegressionLearner()
        self.send_signal(w.Inputs.learner, learner)
        self.assertTrue(w.Error.no_classifier.is_shown())

        # make it empty to test if error disappear
        self.send_signal(w.Inputs.learner, None)
        self.assertFalse(w.Error.no_classifier.is_shown())

        # test with some other learners
        learner = LogisticRegressionLearner()
        self.send_signal(w.Inputs.learner, learner)
        self.assertFalse(w.Error.no_classifier.is_shown())

        learner = TreeLearner()
        self.send_signal(w.Inputs.learner, learner)
        self.assertFalse(w.Error.no_classifier.is_shown())

        learner = RandomForestLearner()
        self.send_signal(w.Inputs.learner, learner)
        self.assertFalse(w.Error.no_classifier.is_shown())

        learner = SVMLearner()
        self.send_signal(w.Inputs.learner, learner)
        self.assertFalse(w.Error.no_classifier.is_shown())
Ejemplo n.º 5
0
 def test_SVM(self):
     n = int(0.7 * self.data.X.shape[0])
     learn = SVMLearner()
     clf = learn(self.data[:n])
     z = clf(self.data[n:])
     self.assertTrue(
         np.sum(z.reshape((-1, 1)) == self.data.Y[n:]) > 0.7 * len(z))
Ejemplo n.º 6
0
    def test_compute_colors(self):
        heart_disease = Table.from_file("heart_disease.tab")
        colors = compute_colors(heart_disease)
        self.assertTupleEqual(colors.shape, heart_disease.X.shape + (3,))

        # the way to add colors to attributes
        [owcolor.DiscAttrDesc(a) for a in heart_disease.domain.attributes]

        colors = compute_colors(heart_disease)
        self.assertTupleEqual(colors.shape, heart_disease.X.shape + (3,))

        titanic = Table("titanic")
        model = SVMLearner()(titanic)
        titanic_proc = model.data_to_model_domain(titanic)

        colors = compute_colors(titanic_proc)
        self.assertTupleEqual(colors.shape, titanic_proc.X.shape + (3,))
Ejemplo n.º 7
0
def results_for_preview(data_name=""):
    from Orange.data import Table
    from Orange.evaluation import CrossValidation
    from Orange.classification import \
        LogisticRegressionLearner, SVMLearner, NuSVMLearner

    data = Table(data_name or "heart_disease")
    results = CrossValidation(data, [
        LogisticRegressionLearner(penalty="l2"),
        LogisticRegressionLearner(penalty="l1"),
        SVMLearner(probability=True),
        NuSVMLearner(probability=True)
    ],
                              store_data=True)
    results.learner_names = ["LR l2", "LR l1", "SVM", "Nu SVM"]
    return results
Ejemplo n.º 8
0
    def test_reprs(self):
        lr = LogisticRegressionLearner(tol=0.0002)
        m = MajorityLearner()
        nb = NaiveBayesLearner()
        rf = RandomForestLearner(bootstrap=False, n_jobs=3)
        st = SimpleTreeLearner(seed=1, bootstrap=True)
        sm = SoftmaxRegressionLearner()
        svm = SVMLearner(shrinking=False)
        lsvm = LinearSVMLearner(tol=0.022, dual=False)
        nsvm = NuSVMLearner(tol=0.003, cache_size=190)
        osvm = OneClassSVMLearner(degree=2)
        tl = TreeLearner(max_depth=3, min_samples_split=1)
        knn = KNNLearner(n_neighbors=4)
        el = EllipticEnvelopeLearner(store_precision=False)
        srf = SimpleRandomForestLearner(n_estimators=20)

        learners = [lr, m, nb, rf, st, sm, svm,
                    lsvm, nsvm, osvm, tl, knn, el, srf]

        for l in learners:
            repr_str = repr(l)
            new_l = eval(repr_str)
            self.assertEqual(repr(new_l), repr_str)
Ejemplo n.º 9
0
 def test_SVM(self):
     learn = SVMLearner()
     cv = CrossValidation(k=2)
     res = cv(self.data, [learn])
     self.assertGreater(CA(res)[0], 0.9)
Ejemplo n.º 10
0
 def test_SVM(self):
     learn = SVMLearner()
     res = Orange.evaluation.CrossValidation(self.data, [learn], k=2)
     self.assertGreater(Orange.evaluation.CA(res)[0], 0.9)
Ejemplo n.º 11
0
from Orange.classification import LogisticRegressionLearner
from Orange.classification import NaiveBayesLearner
from Orange.classification import TreeLearner
from Orange.classification import RandomForestLearner
from Orange.classification import KNNLearner
from Orange.classification import SVMLearner

### create models ###

models = [
    LogisticRegressionLearner(),
    NaiveBayesLearner(),
    TreeLearner(),
    RandomForestLearner(),
    KNNLearner(),
    SVMLearner(),
]

### read train data ###

train = Table.from_file('train.csv')
# move `sex` from X to Y (from attributes/features to class_var/target)
domain = Domain(train.domain.attributes[1:], train.domain.attributes[0])
train = train.transform(domain)

print('\n=== train.X ===')
print(train.X)
print('\n=== train.Y ===')
print(train.Y)

### read predict data ###
Ejemplo n.º 12
0
plt.scatter(train_disc_pts2[:, 0], train_disc_pts2[:, 1], c='b')
bound_ang = np.arange(0, 2 * np.pi, 0.01)
plt.plot(2 * np.cos(bound_ang), 2 * np.sin(bound_ang))
plt.xlim(-4, 4)
plt.ylim(-4, 4)
plt.show()

train_disc_pts = np.append(train_disc_pts1, train_disc_pts2, axis=0)
train_disc_pt_labels = np.append(np.zeros(75), np.ones(75))
train_disc_data_domain = Domain.from_numpy(train_disc_pts, train_disc_pt_labels)
train_disc_data_tab = Table.from_numpy(train_disc_data_domain, train_disc_pts,
train_disc_pt_labels)


print("###########TASK 5###################")
non_linear_learner = SVMLearner()
eval_results = CrossValidation(train_disc_data_tab, [non_linear_learner], k=10)
#Accuracy of cross validation: 0.960
#AUC: 0.959
print("Accuracy of cross validation: {:.3f}".format(scoring.CA(eval_results)[0]))
print("AUC: {:.3f}".format(scoring.AUC(eval_results)[0]))


print("###########EXERCISE 1###############")
non_linear_learner = SVMLearner()
eval_results = non_linear_learner(train_disc_data_tab)

print("DOMAIN: %s \nVARIABLES: %s \nATTRIBUTES: %s \nCLASS_VAR: %s" 
      % (train_disc_data_tab.domain, train_disc_data_tab.domain.variables, train_disc_data_tab.domain.attributes, 
         train_disc_data_tab.domain.class_var))
print(len(train_disc_data_tab))