Skip to content
Advertisement

create an array or dataframe using different variables from nested for loop in python

How do I create an array or dataframe to store seedN, clf.score(X_test, y_test),n_neighbors?

from sklearn.model_selection import train_test_split

for seedN in range(1,50,1):
    X_train, X_test, y_train, y_test = train_test_split(indicators,data2['target'], 
                                                        test_size=0.25, random_state=seedN)

    training_accuracy = []
    test_accuracy = []
    
    neighbors_settings = range(1, 70) # try n_neighbors from 1 to 50

    for n_neighbors in neighbors_settings:   
        clf = KNeighborsClassifier(n_neighbors=n_neighbors)  # build the model
        clf.fit(X_train, y_train)
        training_accuracy.append(clf.score(X_train, y_train)) # record training set accuracy
        test_accuracy.append(clf.score(X_test, y_test))   # record generalization accuracy

Advertisement

Answer

Create a temporary empty list to store the results :

tmp = []

For each fit, add a new list with the desired values :

for seedN in range(1, 50, 1):
    # your code
    for n_neighbors in neighbors_settings:
        # your code
        tmp.append([seedN, clf.score(X_test, y_test), n_neighbors]) 

Finally, create the dataframe with this temporary list :

df = pd.DataFrame(tmp, columns=["seedN", "score", "n_neighbors"])
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement