0


I'm using the git repository https://github.com/aimacode/aima-python to do a simple random forest classification final project. The functions that give the error are

def cross_validation(learner, dataset, size=None, k=10, trials=1):
    """
    Do k-fold cross_validate and return their mean.
    That is, keep out 1/k of the examples for testing on each of k runs.
    Shuffle the examples first; if trials > 1, average over several shuffles.
    Returns Training error, Validation error
    """
    k = k or len(dataset.examples)
    if trials > 1:
        trial_errT = 0
        trial_errV = 0
        for t in range(trials):
            errT, errV = cross_validation(learner, dataset, size, k, trials)
            trial_errT += errT
            trial_errV += errV
        return trial_errT / trials, trial_errV / trials
    else:
        fold_errT = 0
        fold_errV = 0
        n = len(dataset.examples)
        examples = dataset.examples
        random.shuffle(dataset.examples)
        for fold in range(k):
            train_data, val_data = train_test_split(dataset, fold * (n // k), (fold + 1) * (n // k))
            dataset.examples = train_data
            h = learner(dataset, size)
            fold_errT += err_ratio(h, dataset, train_data)
            fold_errV += err_ratio(h, dataset, val_data)
            # reverting back to original once test is completed
            dataset.examples = examples
        return fold_errT / k, fold_errV / k

def RandomForest(dataset, n=5):
    """An ensemble of Decision Trees trained using bagging and feature bagging."""

    def data_bagging(dataset, m=0):
        """Sample m examples with replacement"""
        n = len(dataset.examples)
        return weighted_sample_with_replacement(m or n, dataset.examples, [1] * n)

    def feature_bagging(dataset, p=0.7):
        """Feature bagging with probability p to retain an attribute"""
        inputs = [i for i in dataset.inputs if probability(p)]
        return inputs or dataset.inputs

    def predict(example):
        print([predictor(example) for predictor in predictors])
        return mode(predictor(example) for predictor in predictors)

    predictors = [DecisionTreeLearner(DataSet(examples=data_bagging(dataset), attrs=dataset.attrs,
                                              attr_names=dataset.attr_names, target=dataset.target,
                                              inputs=feature_bagging(dataset))) for _ in range(n)]

    return predict

in learning.py of the above repository.
I tried to do

rf_learner = RandomForest(training_dataset)

training_error, validation_error = cross_validation(learner=rf_learner, dataset=training_dataset, size=5) # use the default trials
print('training error for cv:\n', training_error)
print('validation error for cv: \n', validation_error)

but it keeps telling me

Traceback (most recent call last):
  File "C:\Users\jlmAurora\Desktop\ISTA 450 final project\code\final_project_classification.py", line 109, in <module>
    training_error, validation_error = cross_validation(learner=rf_learner, dataset=training_dataset, size=5) # delete trial=10 here
  File "C:\Users\jlmAurora\Desktop\ISTA 450 final project\code\learning.py", line 303, in cross_validation
    h = learner(dataset, size)
TypeError: RandomForest.<locals>.predict() takes 1 positional argument but 2 were given

I feel really confused because these are nested functions instead of class methods, then there shouldn't be this error which often occurs when you forgot to specify self.
Could anyone help? Thanks a lot!

6
  • You may look for the definition of RandomForest(). You are missing what this is here.
    – tyson.wu
    Apr 25, 2022 at 22:30
  • So I specified the dataset parameter and kept n as default. I think that's what RandomForest takes
    – Limeng
    Apr 25, 2022 at 22:49
  • where does that RandomForest() class came from? Did you implement your own? I am referring to the line rf_learner = RandomForest(training_dataset).
    – tyson.wu
    Apr 25, 2022 at 23:15
  • RandomForest() full code is shown in the first snippet of code, and it's written by someone else. I'm supposed to just take and use it.
    – Limeng
    Apr 25, 2022 at 23:45
  • 1
    RandomForest returns predict which you assign to rf_learner = RandomForest(training_dataset) and later you send as cross_validation(learner=rf_learner, ...) and finally it is executed as h = learner(dataset, size) - so it meas h = predict(dataset, size) but original function is defined as def predict(example): and it expects only one value but you run it with two values.
    – furas
    Apr 26, 2022 at 0:48

0

Your Answer

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