在Python中以List格式获取输出

时间:2017-11-17 14:22:30

标签: python list

我有以下Python代码:(来自Python机器学习简介和' http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.StratifiedKFold.html#sklearn.model_selection.StratifiedKFold.split')

from sklearn.datasets import load_iris
iris = load_iris()
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
from sklearn.model_selection import StratifiedKFold
for n_splits in [2, 3, 5]:    
    skf = StratifiedKFold(n_splits=n_splits, shuffle=False, random_state=0)
    X, y = iris.data, iris.target
    for train_index, test_index in skf.split(X, y):
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]    
        logreg_fit = LogisticRegression().fit(X_train, y_train)
        logreg_scores = logreg_fit.score(X_test, y_test)
        print('{}-fold STRATIFIED Cross-validation scores: {:.3f}'.format(
            n_splits, logreg_scores))

这个输出是:

2-fold STRATIFIED Cross-validation scores: 0.947
2-fold STRATIFIED Cross-validation scores: 0.947
3-fold STRATIFIED Cross-validation scores: 0.961
3-fold STRATIFIED Cross-validation scores: 0.922
3-fold STRATIFIED Cross-validation scores: 0.958
5-fold STRATIFIED Cross-validation scores: 1.000
5-fold STRATIFIED Cross-validation scores: 0.967
5-fold STRATIFIED Cross-validation scores: 0.933
5-fold STRATIFIED Cross-validation scores: 0.900
5-fold STRATIFIED Cross-validation scores: 1.000

我想在每个折叠的列表表格中输出,即 像这样:

2-fold STRATIFIED Cross-validation scores: [ 0.947  0.947]
3-fold STRATIFIED Cross-validation scores: [ 0.961  0.922 0.958]
5-fold STRATIFIED Cross-validation scores: [ 1.000 0.967 0.933 0.900 1.000]

如果有人可以帮助我,我真的很感激。非常感谢。

2 个答案:

答案 0 :(得分:0)

这应该有帮助,它基本上将结果存储在列表中,然后在内部循环完成后打印它。

from sklearn.datasets import load_iris
iris = load_iris()
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
from sklearn.model_selection import StratifiedKFold
for n_splits in [2, 3, 5]:    
    skf = StratifiedKFold(n_splits=n_splits, shuffle=False, random_state=0)
    X, y = iris.data, iris.target
    res = []
    for train_index, test_index in skf.split(X, y):
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]    
        logreg_fit = LogisticRegression().fit(X_train, y_train)
        logreg_scores = logreg_fit.score(X_test, y_test)
        res.append(logreg_scores)
     print('{}-fold STRATIFIED Cross-validation scores: {}'.format(
        n_splits, res))

答案 1 :(得分:0)

可能有一种方法可以在格式列表中执行此操作,但这里有一个替代方法:

    print('{}-fold STRATIFIED Cross-validation scores: {}'.format(
        n_splits, ["{:.3f"}.format(member) for member in logreg_scores] ))

这将创建一个新的字符串列表,其中每个成员都是一个3dp浮点字符串。

通过将其上移到上面的行中,您可以更轻松地阅读:

    formatted_logreg_scores = ["{:.3f"}.format(member) for member in logreg_scores] 
    print('{}-fold STRATIFIED Cross-validation scores: {}'.format(
           n_splits, formatted_logreg_scores))