Reputation: 70
i have a code to print like this :
print('Logistic Regression')
print('Data Train :', logreg.score(X,y))
print('Data Test :', logreg.score(X_test,y_test))
print('KNN')
print('Data Train :', knn.score(X,y))
print('Data Test :', knn.score(X_test,y_test))
and the output is :
Logistic Regression
Data Train : 0.7171072127066912
Data Test :
0.7219339324136186
KNN
Data Train : 0.9711521365495169
Data Test : 0.8530565751170738
how can i make it a dataframe like this :
Logistic Regression | KNN | |
---|---|---|
Data Train | 0.7171072127066912 | 0.9711521365495169 |
Data Test | 0.7219339324136186 | 0.8530565751170738 |
Upvotes: 0
Views: 98
Reputation: 535
You can achieve that easily with list comprehension.
models = [logreg, knn]
model_names = ['Logistic Regression', 'KNN']
pd.DataFrame(
data=[
[model.score(a, b) for (a,b) in [(X, y), (X_test, y_test)]]
for model in models
], index=['Data Train', 'Data Test'], columns=model_names)
And when you want to include more models, you just update your models
and model_names
variables as;
models = [logreg, knn, dt, rf, xgb]
model_names = ['Logistic Regression', 'KNN', 'Decision Tree', 'Random Forest', 'XGBoost']
And keep the latter part exactly the same.
Upvotes: 1
Reputation: 4875
If those are the only 2 models, you can use this:
import pandas as pd
df = pd.DataFrame({"Logistic Regression":[logreg.score(X,y), logreg.score(X_test,y_test)], "KNN":[knn.score(X,y), knn.score(X_test,y_test)]}, index=['Data Train', 'Data Test'])
Upvotes: 1