Esempio n. 1
0
    def test_few_compiled(self):
        num_features = 20
        num_examples = 1000
        num_classes = 4

        X1 = np.random.normal(size=(num_examples, num_features))
        X1 = X1.astype(np.float32)
        y1 = np.random.randint(0, num_classes, size=num_examples)

        X2 = np.random.normal(size=(num_examples, num_features))
        X2 = X2.astype(np.float32)
        y2 = np.random.randint(0, num_classes, size=num_examples)

        rf1 = ensemble.RandomForestClassifier()
        rf1.fit(X1, y1)

        rf2 = ensemble.RandomForestClassifier()
        rf2.fit(X2, y2)

        rf1_compiled = CompiledClassifierPredictor(rf1)
        rf2_compiled = CompiledClassifierPredictor(rf2)

        assert_array_almost_equal(rf1.predict(X1),
                                  rf1_compiled.predict(X1),
                                  decimal=10)
        assert_array_almost_equal(rf2.predict(X2),
                                  rf2_compiled.predict(X2),
                                  decimal=10)
Esempio n. 2
0
    def test_predictions_with_non_contiguous_input(self):
        num_features = 100
        num_examples = 100
        num_classes = 4

        X_non_contiguous = np.random.normal(size=(num_features,
                                                  num_examples)).T
        X_non_contiguous = X_non_contiguous.astype(np.float32)
        self.assertFalse(X_non_contiguous.flags['C_CONTIGUOUS'])

        y = np.random.randint(0, num_classes, size=num_examples)

        rf = ensemble.RandomForestClassifier()
        rf.fit(X_non_contiguous, y)
        rf_compiled = CompiledClassifierPredictor(rf)

        try:
            rf_compiled.predict(X_non_contiguous)
        except ValueError as e:
            self.fail("predict(X) raised ValueError")

        X_contiguous = np.ascontiguousarray(X_non_contiguous)
        self.assertTrue(X_contiguous.flags['C_CONTIGUOUS'])
        assert_array_equal(rf_compiled.predict(X_non_contiguous),
                           rf_compiled.predict(X_contiguous))
Esempio n. 3
0
    def test_float32_and_float_64_predictions_are_equal(self):
        num_features = 100
        num_examples = 100
        num_classes = 4

        X = np.random.normal(size=(num_features, num_examples))
        X_32 = X.astype(np.float32)
        X_64 = X.astype(np.float64)
        y = np.random.randint(0, num_classes, size=num_examples)

        # fit on X_32
        rf = ensemble.RandomForestClassifier()
        rf.fit(X_32, y)
        rf = CompiledClassifierPredictor(rf)

        assert_array_equal(rf.predict(X_32), rf.predict(X_64))

        # fit on X_64
        rf = ensemble.RandomForestClassifier()
        rf.fit(X_64, y)
        rf = CompiledClassifierPredictor(rf)

        assert_array_equal(rf.predict(X_32), rf.predict(X_64))
Esempio n. 4
0
    def test_many_trees(self):
        num_features = 20
        num_examples = 1000
        num_classes = 4

        X1 = np.random.normal(size=(num_examples, num_features))
        X1 = X1.astype(np.float32)
        y1 = np.random.randint(0, num_classes, size=num_examples)

        rf1 = ensemble.RandomForestClassifier(n_estimators=500, max_depth=2)
        rf1.fit(X1, y1)

        rf1_compiled = CompiledClassifierPredictor(rf1)
        assert_array_almost_equal(rf1.predict(X1),
                                  rf1_compiled.predict(X1),
                                  decimal=10)