def test_fit_by_cross_validation(self):
        x = EncodedData(sparse.csr_matrix(
            np.array([[1, 0, 0], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 0, 0], [0, 1, 1], [1, 1, 1], [0, 1, 1]])),
            labels={"t1": [1, 0, 2, 0, 1, 0, 2, 0], "t2": [1, 0, 2, 0, 1, 0, 2, 0]})

        rfc = RandomForestClassifier()
        rfc.fit_by_cross_validation(x, number_of_splits=2, label=Label("t2"))
    def test_predict(self):
        x = np.array([[1, 0, 0], [0, 1, 1], [1, 1, 1], [0, 1, 1]])
        y = {"default": np.array([1, 0, 2, 0])}

        rfc = RandomForestClassifier()
        rfc.fit(EncodedData(x, y), Label("default"))

        test_x = np.array([[0, 1, 0], [1, 0, 0]])
        y = rfc.predict(EncodedData(test_x), Label("default"))["default"]

        self.assertTrue(len(y) == 2)
        self.assertTrue(y[0] in [0, 1, 2])
        self.assertTrue(y[1] in [0, 1, 2])
Exemple #3
0
    def test_load(self):
        x = np.array([[1, 0, 0], [0, 1, 1], [1, 1, 1], [0, 1, 1]])
        y = {"default": np.array([1, 0, 2, 0])}

        rfc = RandomForestClassifier()
        rfc.fit(EncodedData(x, y), "default")

        path = EnvironmentSettings.root_path / "test/tmp/rfc2/"
        PathBuilder.build(path)

        with open(path / "random_forest_classifier.pickle", "wb") as file:
            pickle.dump(rfc.get_model(), file)

        rfc2 = RandomForestClassifier()
        rfc2.load(path)

        self.assertTrue(isinstance(rfc2.get_model()["default"], RFC))

        shutil.rmtree(path)
    def test_store(self):
        x = np.array([[1, 0, 0], [0, 1, 1], [1, 1, 1], [0, 1, 1]])
        y = {"default": np.array([1, 0, 2, 0])}

        rfc = RandomForestClassifier()
        rfc.fit(EncodedData(x, y), Label("default"))

        path = EnvironmentSettings.root_path / "test/tmp/rfc/"

        rfc.store(path)
        self.assertTrue(os.path.isfile(path / "random_forest_classifier.pickle"))

        with open(path / "random_forest_classifier.pickle", "rb") as file:
            rfc2 = pickle.load(file)

        self.assertTrue(isinstance(rfc2, RFC))

        shutil.rmtree(path)
    def test_fit(self):
        x = np.array([[1, 0, 0], [0, 1, 1], [1, 1, 1], [0, 1, 1]])
        y = {"default": np.array([1, 0, 2, 0])}

        rfc = RandomForestClassifier()
        rfc.fit(EncodedData(sparse.csr_matrix(x), y), Label("default"))