In [1]:
from random import seed
from random import randrange
from csv import reader
from math import sqrt
import copy

In [58]:
# Load a CSV file
def load_csv(filename):
    dataset = list()
    with open(filename, 'r') as file:
        csv_reader = reader(file)
        for row in csv_reader:
            if not row:
                continue
            dataset.append(row)
    return dataset
        
# Convert string column to integer
def str_column_to_int(dataset, column):
    class_values = [row[column] for row in dataset]
    unique = set(class_values)
    lookup = dict()
    for i, value in enumerate(unique):
        lookup[value] = i
    for row in dataset:
        row[column] = lookup[row[column]]
    return lookup

# Convert string column to float
def str_column_to_float(dataset, column):
    
    column = column
    dataset_copy = copy.deepcopy(dataset)
    
    for row in dataset_copy:
        row[column] = float(row[column].strip())
    
    return dataset_copy

In [73]:
# Test the random forest algorithm
seed(1)

# load and prepare data
filename = '../../data_sets/sonar.all-data.csv'
dataset = load_csv(filename)

# convert string attributes to integers
for i in range(0, len(dataset[0])-1):
    dataset = str_column_to_float(dataset, i)

In [71]:
def gini_index(groups, class_values):
    """
    Calcular o indíce-Gini pela split dataset
    """
    gini = 0.0
    for class_value in class_values:
        for group in groups:
            size = len(group)
            if size == 0:
                continue
            proportion = [row[-1] for row in group].count(class_value) / float(size)
            gini += (proportion * (1.0 - proportion))
    return gini
 
# Select the best split point for a dataset
def get_split(dataset, n_features):
    valores_alvo = list(set(row[-1] for row in dataset)) # Lista único de valores-alvo 
    b_index, b_value, b_score, b_groups = 999, 999, 999, None
    
    # Faz uma lista de características únicos para usar
    features = list()
    while len(features) < n_features:
        index = randrange(len(dataset[0])-1)
        if index not in features:
            features.append(index)
    
    
    for index in features:
        for row in dataset:
            groups = test_split(index, row[index], dataset)
            gini = gini_index(groups, class_values)
            if gini < b_score:
                b_index, b_value, b_score, b_groups = index, row[index], gini, groups
    return {'index':b_index, 'value':b_value, 'groups':b_groups}
 
# Create a terminal node value
def to_terminal(group):
    """
    Voltar o valor alvo para uma grupo no fim de uma filial
    
    :param grupo: O conjuncto de registros em um lado de uma divisão
    
    :return: valor_de_alvo, Int 
    """
    
    outcomes = [row[-1] for row in group]
    valor_de_alvo = max(set(outcomes), key=outcomes.count)
    return valor_de_alvo
 
# Create child splits for a node or make terminal
def split(node, max_depth, min_size, n_features, depth):
    left, right = node['groups']
    del(node['groups'])
    
    # check for a no split
    if not left or not right:
        node['left'] = node['right'] = to_terminal(left + right)
        return
    
    # check for max depth
    if depth >= max_depth:
        node['left'], node['right'] = to_terminal(left), to_terminal(right)
        return
    
    # process left child
    if len(left) <= min_size:
        node['left'] = to_terminal(left)
    else:
        node['left'] = get_split(left, n_features)
        split(node['left'], max_depth, min_size, n_features, depth+1)

    # process right child
    if len(right) <= min_size:
        node['right'] = to_terminal(right)
    else:
        node['right'] = get_split(right, n_features)
        split(node['right'], max_depth, min_size, n_features, depth+1)

def test_split(indíce, valor, dados):
    """
    Dividir o dados sobre uma característica e o valor da caracaterística dele
    
    :param indíce: Int, o indíce da característica
    :param valor: Float, o valor do indíce por um registro
    :param dados: List, o conjuncto de dados
    """
    
    left, right = list(), list()
    for row in dataset:
        if row[index] < value:
            left.append(row)
        else:
            right.append(row)
    return left, right
        
# Build a decision tree
def build_tree(train, max_depth, min_size, n_features):
    root = get_split(train, n_features)
    split(root, max_depth, min_size, n_features, 1)
    return root

# Make a prediction with a decision tree
def predict(node, row):
    if row[node['index']] < node['value']:
        if isinstance(node['left'], dict):
            return predict(node['left'], row)
        else:
            return node['left']
    else:
        if isinstance(node['right'], dict):
            return predict(node['right'], row)
        else:
            return node['right']

In [99]:
n_features = len(dataset[0])-1
max_depth = 10
min_size = 1

dados_divisão = int(len(dataset)*.8)
trem = dataset[:dados_divisão]
teste = dataset[dados_divisão:]

árvore = build_tree(trem, max_depth, min_size, n_features)

In [102]:
indíce_teste = 8

print(predict(árvore, teste[indíce_teste]))
print(teste[indíce_teste][-1])


R
M

In [96]:
x['right']
x


Out[96]:
{'index': 57,
 'left': {'index': 53,
  'left': {'index': 20,
   'left': {'index': 57, 'left': 'M', 'right': 'M', 'value': 0.002},
   'right': {'index': 54,
    'left': {'index': 59,
     'left': {'index': 51,
      'left': {'index': 30,
       'left': {'index': 1, 'left': 'M', 'right': 'M', 'value': 0.0446},
       'right': {'index': 28,
        'left': {'index': 19, 'left': 'M', 'right': 'M', 'value': 0.7383},
        'right': {'index': 25,
         'left': {'index': 51, 'left': 'M', 'right': 'M', 'value': 0.0331},
         'right': {'index': 9, 'left': 'M', 'right': 'R', 'value': 0.0242},
         'value': 0.1653},
        'value': 0.0716},
       'value': 0.1307},
      'right': {'index': 7, 'left': 'M', 'right': 'M', 'value': 0.2018},
      'value': 0.0432},
     'right': {'index': 9,
      'left': {'index': 3, 'left': 'M', 'right': 'M', 'value': 0.0844},
      'right': {'index': 34, 'left': 'M', 'right': 'M', 'value': 0.6355},
      'value': 0.2028},
     'value': 0.0198},
    'right': {'index': 55,
     'left': {'index': 13, 'left': 'M', 'right': 'M', 'value': 0.2689},
     'right': {'index': 55, 'left': 'M', 'right': 'M', 'value': 0.0152},
     'value': 0.0152},
    'value': 0.027},
   'value': 0.1268},
  'right': {'index': 32, 'left': 'M', 'right': 'M', 'value': 0.3052},
  'value': 0.029},
 'right': {'index': 8,
  'left': {'index': 58, 'left': 'M', 'right': 'M', 'value': 0.0154},
  'right': {'index': 0, 'left': 'M', 'right': 'M', 'value': 0.0201},
  'value': 0.1206},
 'value': 0.0201}

In [ ]:
# Calculate accuracy percentage
def accuracy_metric(actual, predicted):
    correct = 0
    for i in range(len(actual)):
        if actual[i] == predicted[i]:
            correct += 1
    return correct / float(len(actual)) * 100.0
 
# Evaluate an algorithm using a cross validation split
def evaluate_algorithm(dataset, algorithm, n_folds, *args):
    folds = cross_validation_split(dataset, n_folds)
    scores = list()
    for fold in folds:
        train_set = list(folds)
        train_set.remove(fold)
        train_set = sum(train_set, [])
        test_set = list()
        for row in fold:
            row_copy = list(row)
            test_set.append(row_copy)
            row_copy[-1] = None
        predicted = algorithm(train_set, test_set, *args)
        actual = [row[-1] for row in fold]
        accuracy = accuracy_metric(actual, predicted)
        scores.append(accuracy)
    return scores

# Split a dataset into k folds
def cross_validation_split(dataset, n_folds):
    dataset_split = list()
    dataset_copy = list(dataset)
    fold_size = int(len(dataset) / n_folds)
    for i in range(n_folds):
        fold = list()
        while len(fold) < fold_size:
            index = randrange(len(dataset_copy))
            fold.append(dataset_copy.pop(index))
        dataset_split.append(fold)
    return dataset_split

In [ ]:
# evaluate algorithm
n_folds = 5
max_depth = 10
min_size = 1
sample_size = 1.0
n_features = int(sqrt(len(dataset[0])-1))
for n_trees in [1, 5, 10]:
    scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, n_features)
    print('Trees: %d' % n_trees)
    print('Scores: %s' % scores)
    print('Mean Accuracy: %.3f%%' % (sum(scores)/float(len(scores))))

In [ ]:
# Create a random subsample from the dataset with replacement
def subsample(dataset, ratio):
    sample = list()
    n_sample = round(len(dataset) * ratio)
    while len(sample) < n_sample:
        index = randrange(len(dataset))
        sample.append(dataset[index])
    return sample
 
# Make a prediction with a list of bagged trees
def bagging_predict(trees, row):
    predictions = [predict(tree, row) for tree in trees]
    return max(set(predictions), key=predictions.count)
 
# Random Forest Algorithm
def random_forest(train, test, max_depth, min_size, sample_size, n_trees, n_features):
    trees = list()
    for i in range(n_trees):
        sample = subsample(train, sample_size)
        tree = build_tree(sample, max_depth, min_size, n_features)
        trees.append(tree)
    predictions = [bagging_predict(trees, row) for row in test]
    return(predictions)