Home > Mobile >  How to create a list of tuples using a for loop
How to create a list of tuples using a for loop

Time:09-26

I've populated results with test_score, but how can I add train_score to results so that it's a list of 10 tuples like: (train_score, test_score)?

import numpy as np
import pandas as pd
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier

results = []

for i in list(range(1, 11)):
    
    clf = DecisionTreeClassifier(max_depth = i, random_state=0).fit(X_train, y_train)
    
    test_score = clf.score(X_test, y_test)
    train_score = clf.score(X_train, y_train)
    
    results.append(test_score)
    
results

CodePudding user response:

You can do something like:

...
results.append((train_score, test_score))
...

CodePudding user response:

Just create a tuple instance before

t = (test_score, train_score)
results.append(t)

CodePudding user response:

Append the tuple to your list :
results.append((train_score,test_score))

Also, note than for i in list(range(1, 11)) is unnecessary in a for loop.

for i in range(1, 11) is sufficient since it returns a range object which is iterable.

  • Related