'Python RF Model Evaluation Metrics

I have run the random forest model in Python. I need to store the TP,FP,FN and TN data in a dataframe.

    cf_matrix_train = pd.DataFrame([{'Key':0,'TN':0,'FP':0,'FN':0,'TP':0}])

df = pd.DataFrame()


max_f = list(range(1,3))
max_d = list(range(10,12))



for fea in max_f:
    for dep in max_d:
        clf=RandomForestClassifier(random_state=1,n_estimators=100,max_features=fea, max_depth=dep)
        clf.fit(X_train,y_train)
        train_pred = clf.predict(X_train)
        cm_train = confusion_matrix(train_pred,y_train)
        cf_matrix_train['TN'] = cm_train[0][0]
        cf_matrix_train['FP'] = cm_train[1][0]
        cf_matrix_train['FN'] = cm_train[0][1]
        cf_matrix_train['TP'] = cm_train[1][1]
        cf_matrix_train.append(df)
        print(cm_train)

Though all the four model were generated, only the last model output saved to dataframe "cf_matrix_train".

Current Output:

Expected Output



Solution 1:[1]

The .append() operation in pandas is not done inplace. It returns a new dataframe. So you've to assign it to something. Further, you're doing the opposite.

Replace the line

cf_matrix_train.append(df)

with

df = df.append(cf_matrix_train)

Apart from this, you can also do something like this:

df = pd.DataFrame(columns=['Key', 'TN', 'FP', 'FN', 'TP'])

max_f = list(range(1, 3))
max_d = list(range(10, 12))

index = 0
for fea in max_f:
    for dep in max_d:
        clf = RandomForestClassifier(random_state=1, n_estimators=100,
                                     max_features=fea, max_depth=dep)
        clf.fit(X_train,y_train)
        train_pred = clf.predict(X_train)
        cm_train = confusion_matrix(train_pred, y_train)

        df.loc[index, 'Key'] = f"{fea}, {dep}"
        df.loc[index, 'TN'] = cm_train[0][0]
        df.loc[index, 'FP'] = cm_train[1][0]
        df.loc[index, 'FN'] = cm_train[0][1]
        df.loc[index, 'TP'] = cm_train[1][1]

        index += 1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1