3

The problem with the code below is that df is not appended by new DataFrame. When I print the shape it is still (1,6). How can I fix it?

columns = ['name', 'precision', 'recall', 'gmean', 'f1', 'mse']
df_SMOTE = pd.DataFrame(columns=columns )
df_ENN = pd.DataFrame(columns=columns )
df_Ensemble = pd.DataFrame(columns=columns )

for name, model in zip(names, [rfc, knc, lr, svc, dtc, xgbc, cbc, lgbc]):
    for X, y, df in [(X_smote, y_smote, df_SMOTE), (X_enn, y_enn, df_ENN), (X_smote, y_smote, df_Ensemble)]:
        learner = Learner(model, X, y)
        learner()
        precision, recall, gmean, f1, mse = learner.get_metrics()
        df = pd.concat([df, pd.DataFrame({'name': [name], 'precision': [precision], 'recall': [recall], 'gmean': [gmean], 'f1': [f1], 'mse': [mse]})], ignore_index=True)
        
        print(df.shape)

1 Answer 1

1

It would be faster if you collected each individual dataframe in a list and then did the concat after. Sort of like this:

columns = ["name", "precision", "recall", "gmean", "f1", "mse"]
dfs = []
for i in range(100):
    dfs.append(
        pd.DataFrame(
            data=[
                (f"n{i}", f"p{i}", f"r{i}", f"g{i}", f"f{i}", f"m{i}")
                for i in np.random.randint(0, 10000, size=(5,))
            ],
            columns=columns,
        )
    )
df = pd.concat(dfs, ignore_index=True)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.