2

I am following this awesome tutorial about random forest: https://machinelearningmastery.com/implement-random-forest-scratch-python/

My problem is that I am getting the error: "TypeError: 'list' object cannot be interpreted as an integer" which is a pretty common error and also which is pretty self-explanatory. Moreover, it has been discussed: here1, here2, and here3

What drives me nuts is that this when I run the code proposed on my Spyder IDE, it gives me the error. However, when I run exactly the same code on Jupyter, the code runs with no problem. Am I missing something really simple? They are different IDEs but it should be the same thing. Both them are running under the same Anaconda environment:

  • Python 3.7.1
  • Spyder 3.3.3
  • Jupyter Notebook 5.7.8

As suggested in the comments, I restarted the Jupyter kernel and rerun my code. In the same way, it gives me results (the code run without error). The error is exclusively on the Spyder side.

My whole code:

# -*- coding: utf-8 -*-
"""
Created on Wed May 15 22:26:36 2019
@author:
Ideas based on: https://machinelearningmastery.com/implement-random-forest-scratch-python/
Random Forest from Scratch
"""

# Random Forest Algorithm
from random import seed
from random import randrange
from csv import reader
from math import sqrt


# 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)
    #print(dataset) 
    return dataset

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

# 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

# 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

# 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 based on an attribute and an attribute value
def test_split(index, value, dataset):
    left, right = list(), list()
    for row in dataset:
        if row[index] < value:
            left.append(row)
        else:
            right.append(row)
    return left, right

# Calculate the Gini index for a split dataset
def gini_index(groups, classes):
    # count all samples at split point
    n_instances = float(sum([len(group) for group in groups]))
    # sum weighted Gini index for each group
    gini = 0.0
    for group in groups:
        size = float(len(group))
        # avoid divide by zero
        if size == 0:
            continue
        score = 0.0
        # score the group based on the score for each class
        for class_val in classes:
            p = [row[-1] for row in group].count(class_val) / size
            score += p * p
        # weight the group score by its relative size
        gini += (1.0 - score) * (size / n_instances)
    return gini

# Select the best split point for a dataset
def get_split(dataset, n_features):
    class_values = list(set(row[-1] for row in dataset))
    b_index, b_value, b_score, b_groups = 999, 999, 999, None
    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):
    outcomes = [row[-1] for row in group]
    return max(set(outcomes), key=outcomes.count)

# 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)

# 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']

# 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)

# Test the random forest algorithm
seed(2)

# load and prepare data
filename = 'x'
dataset = load_csv(filename)

print("hereee1")

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

print("hereee2")

# convert class column to integers
str_column_to_int(dataset, len(dataset[0])-1)

print("hereee3")

# evaluate algorithm
n_folds = 5
max_depth = 10
min_size = 1
sample_size = 1.0
n_features = int(sqrt(len(dataset[0])-1))

print("hereee4")

for n_trees in [1, 5, 10]:
    print("hereee5")
    scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, n_features)
    print("hereee6")
    print('Trees: %d' % n_trees)
    print('Scores: %s' % scores)
    print('Mean Accuracy: %.3f%%' % (sum(scores)/float(len(scores))))

The full error is:

runfile('C:/Users/X/Desktop/Xx/Code/RF_from_scratch.py', wdir='C:/Users/X/Desktop/Xx/Code') hereee1 hereee2 hereee3 hereee4 hereee5 Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/X/Desktop/Xx/Code/RF_from_scratch.py', wdir='C:/Users/X/Desktop/Xx/Code')

File "D:\Anaconda\envs\envdata\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile execfile(filename, namespace)

File "D:\Anaconda\envs\envdata\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/X/Desktop/Xx/Code/RF_from_scratch.py", line 235, in scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, n_features)

File "C:/Users/X/Desktop/Xx/Code/RF_from_scratch.py", line 72, in evaluate_algorithm train_set = sum(train_set, [])

File "D:\Anaconda\envs\envdata\lib\site-packages\numpy\core\fromnumeric.py", line 2076, in sum initial=initial)

File "D:\Anaconda\envs\envdata\lib\site-packages\numpy\core\fromnumeric.py", line 86, in _wrapreduction return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

TypeError: 'list' object cannot be interpreted as an integer

7
  • Please paste the full traceback. Also, reset your Jupyter kernel and try again.
    – gmds
    May 16, 2019 at 4:33
  • In the traceback sum is the numpy version. Due to a from numpy import *, np.sum shadows the Python sum. sum(var, []) works with the Python sum, but np.sum(var, []) produces this error.
    – hpaulj
    May 16, 2019 at 5:59
  • For Python sum(var, []) does a list concatenates. numpy.sum attempts to do a numeric sum, interpreting the 2nd argument as an axis number.
    – hpaulj
    May 16, 2019 at 6:03
  • 1
    Please restart Spyder and try again too. That should fix your problem. May 16, 2019 at 9:24
  • @CarlosCordoba, your solution worked! Do you want to post as a solution and I will tag as correct or do you want me to post as my own solution? Thankssss
    – FFLS
    May 16, 2019 at 14:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.