Home > Software engineering >  Add to dataframe from the results in for loop? [index , value]
Add to dataframe from the results in for loop? [index , value]

Time:01-02

I want to add in a pipeline 3 ML models and then I wish to pass them for cross validation with 10 splits. Now I want to store the result in a df with index as model name and score that is received from the model and cross validation.

model_names = ['Lasso', 'Ridge', 'KNeighbors Regression']
models = [Lasso(), Ridge() ,KNeighborsRegressor(n_neighbors=2) ]
iterr=0
scores =  [ ]
for index, model in enumerate(models):
    model.fit(x_train, y_train)
    score1 = model.score(x_test, y_test) 
    scores.append(score)
print(scores)

but this only gives the value.

CodePudding user response:

If you just want to create a DataFrame from model_names and scores, how about:

pd.DataFrame(scores, index=model_names, columns=['score'])

resulting in:

                    score
Lasso                  12
Ridge                  12
KNeighbors Regression  12

CodePudding user response:

More question to this question. What should I do, if I want to perform a cross validation for 5 splits and I want to store the result in this form

lasso      cv1      0.786
lasso      cv2      0.789
lasso      cv3      0.788

......

  • Related