简体   繁体   English

仅使用列表的最后一个值进行循环迭代

[英]For loop iterating with only last value of the list

For-loop iterating but store with only last value of the list For-loop 迭代但仅存储列表的最后一个值

Here is the code:这是代码:

models = [KNN,NB,LR,SVM]
result = pd.DataFrame()
for i in models:
    i.fit(x_train, y_train)
    ypred = i.predict(x_test)
    model_valuation = result.append({'Model': i, 
                                  'Accuracy': accuracy_score(y_test, ypred),
                                    'Recall': recall_score(y_test, ypred),
                                 'Precision': precision_score(y_test, ypred),
                               'Specificity': recall_score(y_test, ypred, pos_label=0),
                                  'F1 score': f1_score(y_test, ypred)}, ignore_index=True)

It appends only the SVM which is the last value in the list models.它仅附加 SVM,它是列表模型中的最后一个值。

You need to assign result.append() to the dataframe result in order to append the new record您需要将result.append()分配给 dataframe result以便 append 新记录

Try this:尝试这个:

models = [KNN, NB, LR, SVM]
result = pd.DataFrame()
for i in models:
    i.fit(x_train, y_train)
    ypred = i.predict(x_test)
    result = result.append({'Model': i, 
                         'Accuracy': accuracy_score(y_test, ypred),
                           'Recall': recall_score(y_test, ypred),
                        'Precision': precision_score(y_test, ypred),
                      'Specificity': recall_score(y_test, ypred, pos_label=0),
                         'F1 score': f1_score(y_test, ypred)}, ignore_index=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM