# Path: mla/ensemble/random_forest.py
# class RandomForestClassifier(RandomForest):
#     def __init__(self, n_estimators=10, max_features=None, min_samples_split=10, max_depth=None, criterion="entropy"):
#         super(RandomForestClassifier, self).__init__(
#             n_estimators=n_estimators,
#             max_features=max_features,
#             min_samples_split=min_samples_split,
#             max_depth=max_depth,
#             criterion=criterion,
#         )
# 
#         if criterion == "entropy":
#             self.criterion = information_gain
#         else:
#             raise ValueError()
# 
#         # Initialize empty trees
#         for _ in range(self.n_estimators):
#             self.trees.append(Tree(criterion=self.criterion))
# 
#     def _predict(self, X=None):
#         y_shape = np.unique(self.y).shape[0]
#         predictions = np.zeros((X.shape[0], y_shape))
# 
#         for i in range(X.shape[0]):
#             row_pred = np.zeros(y_shape)
#             for tree in self.trees:
#                 row_pred += tree.predict_row(X[i, :])
# 
#             row_pred /= self.n_estimators
#             predictions[i, :] = row_pred
#         return predictions
# 
# class RandomForestRegressor(RandomForest):
#     def __init__(self, n_estimators=10, max_features=None, min_samples_split=10, max_depth=None, criterion="mse"):
#         super(RandomForestRegressor, self).__init__(
#             n_estimators=n_estimators,
#             max_features=max_features,
#             min_samples_split=min_samples_split,
#             max_depth=max_depth,
#         )
# 
#         if criterion == "mse":
#             self.criterion = mse_criterion
#         else:
#             raise ValueError()
# 
#         # Initialize empty regression trees
#         for _ in range(self.n_estimators):
#             self.trees.append(Tree(regression=True, criterion=self.criterion))
# 
#     def _predict(self, X=None):
#         predictions = np.zeros((X.shape[0], self.n_estimators))
#         for i, tree in enumerate(self.trees):
#             predictions[:, i] = tree.predict(X)
#         return predictions.mean(axis=1)
# 
# Path: mla/metrics/metrics.py
# def mean_squared_error(actual, predicted):
#     return np.mean(squared_error(actual, predicted))


import logging
import numpy as np
from sklearn.datasets import make_classification
from sklearn.datasets import make_regression
from sklearn.metrics import roc_auc_score, accuracy_score
    from sklearn.model_selection import train_test_split
    from sklearn.cross_validation import train_test_split
from mla.ensemble.random_forest import RandomForestClassifier, RandomForestRegressor
from mla.metrics.metrics import mean_squared_error


logging.basicConfig(level=logging.DEBUG)


def classification():
    # Generate a random binary classification problem.
    X, y = make_classification(
        n_samples=500, n_features=10, n_informative=10, random_state=1111, n_classes=2, class_sep=2.5, n_redundant=0
    )

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=1111)

    model = RandomForestClassifier(n_estimators=10, max_depth=4)
    model.fit(X_train, y_train)

    predictions_prob = model.predict(X_test)[:, 1]
    predictions = np.argmax(model.predict(X_test), axis=1)
    #print(predictions.shape)
    print("classification, roc auc score: %s" % roc_auc_score(y_test, predictions_prob))
    print("classification, accuracy score: %s" % accuracy_score(y_test, predictions))


def regression():
    # Generate a random regression problem
    X, y = make_regression(
        n_samples=500, n_features=5, n_informative=5, n_targets=1, noise=0.05, random_state=1111, bias=0.5
    )
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1111)


# Path: mla/ensemble/random_forest.py
# class RandomForestClassifier(RandomForest):
#     def __init__(self, n_estimators=10, max_features=None, min_samples_split=10, max_depth=None, criterion="entropy"):
#         super(RandomForestClassifier, self).__init__(
#             n_estimators=n_estimators,
#             max_features=max_features,
#             min_samples_split=min_samples_split,
#             max_depth=max_depth,
#             criterion=criterion,
#         )
# 
#         if criterion == "entropy":
#             self.criterion = information_gain
#         else:
#             raise ValueError()
# 
#         # Initialize empty trees
#         for _ in range(self.n_estimators):
#             self.trees.append(Tree(criterion=self.criterion))
# 
#     def _predict(self, X=None):
#         y_shape = np.unique(self.y).shape[0]
#         predictions = np.zeros((X.shape[0], y_shape))
# 
#         for i in range(X.shape[0]):
#             row_pred = np.zeros(y_shape)
#             for tree in self.trees:
#                 row_pred += tree.predict_row(X[i, :])
# 
#             row_pred /= self.n_estimators
#             predictions[i, :] = row_pred
#         return predictions
# 
# class RandomForestRegressor(RandomForest):
#     def __init__(self, n_estimators=10, max_features=None, min_samples_split=10, max_depth=None, criterion="mse"):
#         super(RandomForestRegressor, self).__init__(
#             n_estimators=n_estimators,
#             max_features=max_features,
#             min_samples_split=min_samples_split,
#             max_depth=max_depth,
#         )
# 
#         if criterion == "mse":
#             self.criterion = mse_criterion
#         else:
#             raise ValueError()
# 
#         # Initialize empty regression trees
#         for _ in range(self.n_estimators):
#             self.trees.append(Tree(regression=True, criterion=self.criterion))
# 
#     def _predict(self, X=None):
#         predictions = np.zeros((X.shape[0], self.n_estimators))
#         for i, tree in enumerate(self.trees):
#             predictions[:, i] = tree.predict(X)
#         return predictions.mean(axis=1)
# 
# Path: mla/metrics/metrics.py
# def mean_squared_error(actual, predicted):
#     return np.mean(squared_error(actual, predicted))


# Path: examples/random_forest.py
import logging
import numpy as np
from sklearn.datasets import make_classification
from sklearn.datasets import make_regression
from sklearn.metrics import roc_auc_score, accuracy_score
    from sklearn.model_selection import train_test_split
    from sklearn.cross_validation import train_test_split
from mla.ensemble.random_forest import RandomForestClassifier, RandomForestRegressor
from mla.metrics.metrics import mean_squared_error



logging.basicConfig(level=logging.DEBUG)


def classification():
    # Generate a random binary classification problem.
    X, y = make_classification(
        n_samples=500, n_features=10, n_informative=10, random_state=1111, n_classes=2, class_sep=2.5, n_redundant=0
    )

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=1111)

    model = RandomForestClassifier(n_estimators=10, max_depth=4)
    model.fit(X_train, y_train)

    predictions_prob = model.predict(X_test)[:, 1]
    predictions = np.argmax(model.predict(X_test), axis=1)
    #print(predictions.shape)
    print("classification, roc auc score: %s" % roc_auc_score(y_test, predictions_prob))
    print("classification, accuracy score: %s" % accuracy_score(y_test, predictions))


def regression():
    # Generate a random regression problem
    X, y = make_regression(
        n_samples=500, n_features=5, n_informative=5, n_targets=1, noise=0.05, random_state=1111, bias=0.5
    )
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1111)


    model = RandomForestRegressor(n_estimators=50, max_depth=10, max_features=3)