def test_decision_tree_criterion_iris_dtc(self): iris = datasets.load_iris() X, y = iris.data, iris.target clr1 = DecisionTreeRegressor() clr1.fit(X, y) p1 = clr1.predict(X) clr2 = PiecewiseTreeRegressor(criterion='simple') clr2.fit(X, y) p2 = clr2.predict(X) self.assertEqual(p1[:10], p2[:10])
def test_decision_tree_criterion_iris_dtc(self): iris = datasets.load_iris() X, y = iris.data, iris.target clr1 = DecisionTreeRegressor() clr1.fit(X, y) p1 = clr1.predict(X) clr2 = PiecewiseTreeRegressor(criterion='mselin') clr2.fit(X, y) p2 = clr2.predict(X) self.assertEqual(p1.shape, p2.shape) self.assertTrue(hasattr(clr2, 'betas_')) self.assertTrue(hasattr(clr2, 'leaves_mapping_')) self.assertEqual(len(clr2.leaves_index_), clr2.tree_.n_leaves) self.assertEqual(len(clr2.leaves_mapping_), clr2.tree_.n_leaves) self.assertEqual(clr2.betas_.shape[1], X.shape[1] + 1) self.assertEqual(clr2.betas_.shape[0], clr2.tree_.n_leaves) sc1 = clr1.score(X, y) sc2 = clr2.score(X, y) self.assertGreater(sc1, sc2) mp = clr2._mapping_train(X) # pylint: disable=W0212 self.assertIsInstance(mp, dict) self.assertGreater(len(mp), 2)
def test_decision_tree_criterion_iris_dtc_traintest(self): iris = datasets.load_iris() X, y = iris.data, iris.target X_train, X_test, y_train, y_test = train_test_split(X, y) clr1 = DecisionTreeRegressor() clr1.fit(X_train, y_train) p1 = clr1.predict(X_train) clr2 = PiecewiseTreeRegressor(criterion='mselin') clr2.fit(X_train, y_train) p2 = clr2.predict(X_train) self.assertEqual(p1.shape, p2.shape) self.assertTrue(hasattr(clr2, 'betas_')) self.assertTrue(hasattr(clr2, 'leaves_mapping_')) self.assertEqual(len(clr2.leaves_index_), clr2.tree_.n_leaves) self.assertEqual(len(clr2.leaves_mapping_), clr2.tree_.n_leaves) self.assertEqual(clr2.betas_.shape[1], X.shape[1] + 1) self.assertEqual(clr2.betas_.shape[0], clr2.tree_.n_leaves) sc1 = clr1.score(X_test, y_test) sc2 = clr2.score(X_test, y_test) self.assertGreater(abs(sc1 - sc2), -0.1)