def main(): np.random.seed(42) X_train = np.array([ [-3.0], [-1.0], [0.0], [1.0], [2.0], [4.0], ]) Y_train = np.cos(X_train) + np.random.randn(X_train.shape[0], 1) * 0.2 num_test = 10000 X_test = np.linspace(-5, 5, num_test) X_test = X_test.reshape((num_test, 1)) Y_test = np.cos(X_test) num_trees = 100 depth_max = 5 size_min_leaf = 2 ratio_sampling = 0.8 replace_samples = False num_features = 1 split_random_location = True trees = trees_generic_trees.get_generic_trees( X_train, Y_train, num_trees, depth_max, size_min_leaf, ratio_sampling, replace_samples, num_features, split_random_location ) mu, sigma = trees_common.predict_by_trees(X_test, trees) utils_plotting.plot_gp_via_distribution(X_train, Y_train, X_test, mu, sigma, Y_test, path_save=PATH_SAVE, str_postfix='cos')
def compute_posteriors(self, X: np.ndarray, trees: constants.TYPING_LIST) -> np.ndarray: """ It returns posterior mean and standard deviation functions over `X`. :param X: inputs to test. Shape: (l, d). :type X: numpy.ndarray :param trees: list of trees. :type trees: list :returns: posterior mean and standard deviation functions over `X`. Shape: ((l, ), (l, )). :rtype: (numpy.ndarray, numpy.ndarray) :raises: AssertionError """ assert isinstance(X, np.ndarray) assert isinstance(trees, list) assert len(X.shape) == 2 assert X.shape[1] == self.num_dim pred_mean, pred_std = trees_common.predict_by_trees(X, trees) pred_mean = np.squeeze(pred_mean, axis=1) pred_std = np.squeeze(pred_std, axis=1) return pred_mean, pred_std
def main(): time_start = time.time() np.random.seed(42) X_train = np.array([ [-3.0], [-1.0], [0.0], [1.0], [2.0], [4.0], ]) Y_train = np.cos(X_train) + np.random.randn(X_train.shape[0], 1) * 0.2 num_test = 10000 X_test = np.linspace(-5, 5, num_test) X_test = X_test.reshape((num_test, 1)) Y_test = np.cos(X_test) num_trees = 100 depth_max = 5 size_min_leaf = 2 num_features = 1 trees = trees_random_forest.get_random_forest( X_train, Y_train, num_trees, depth_max, size_min_leaf, num_features ) mu, sigma = trees_common.predict_by_trees(X_test, trees) time_end = time.time() print('time consumed: {:.4f}'.format(time_end - time_start)) utils_plotting.plot_gp_via_distribution(X_train, Y_train, X_test, mu, sigma, Y_test, path_save=PATH_SAVE, str_postfix='cos')
def _predict_by_trees(num_test): X_test = np.linspace(-5, 5, num_test) X_test = X_test.reshape((num_test, 1)) mu, sigma = trees_common.predict_by_trees(X_test, trees) return mu, sigma
def test_predict_by_trees(): np.random.seed(42) X = np.reshape(np.arange(0, 40), (10, 4)) Y = np.random.randn(10, 1) depth_max = 4 size_min_leaf = 2 num_features = 2 split_random_location = True node_1 = package_target._split(X, Y, num_features, split_random_location) package_target.split(node_1, depth_max, size_min_leaf, num_features, split_random_location, 1) node_2 = package_target._split(X, Y, num_features, split_random_location) package_target.split(node_2, depth_max, size_min_leaf, num_features, split_random_location, 1) node_3 = package_target._split(X, Y, num_features, split_random_location) package_target.split(node_3, depth_max, size_min_leaf, num_features, split_random_location, 1) list_trees = [node_1, node_2, node_3] with pytest.raises(AssertionError) as error: package_target.predict_by_trees(np.array([4.0, 2.0, 3.0, 1.0]), node_1) with pytest.raises(AssertionError) as error: package_target.predict_by_trees(np.array([4.0, 2.0, 3.0, 1.0]), 'abc') with pytest.raises(AssertionError) as error: package_target.predict_by_trees(np.array([4.0, 2.0, 3.0, 1.0]), list_trees) with pytest.raises(AssertionError) as error: package_target.predict_by_trees('abc', list_trees) means, stds = package_target.predict_by_trees(X, list_trees) print(means) print(stds) means_truth = np.array([ [0.33710618], [0.1254467], [0.68947573], [0.9866563], [0.16257799], [0.16258346], [0.85148454], [0.55084716], [0.30721653], [0.30721653], ]) stds_truth = np.array([ [0.29842457], [0.33330409], [0.44398864], [0.72536523], [0.74232577], [0.74232284], [0.83388663], [0.5615399], [0.64331582], [0.64331582], ]) assert isinstance(means, np.ndarray) assert isinstance(stds, np.ndarray) assert len(means.shape) == 2 assert len(stds.shape) == 2 assert means.shape[0] == stds.shape[0] == X.shape[0] assert means.shape[1] == stds.shape[1] == 1 assert np.all(np.abs(means - means_truth) < TEST_EPSILON) assert np.all(np.abs(stds - stds_truth) < TEST_EPSILON) X = np.random.randn(1000, 4) means, stds = package_target.predict_by_trees(X, list_trees) assert isinstance(means, np.ndarray) assert isinstance(stds, np.ndarray) assert len(means.shape) == 2 assert len(stds.shape) == 2 assert means.shape[0] == stds.shape[0] == X.shape[0] assert means.shape[1] == stds.shape[1] == 1