【问题标题】:Print results more meaningfully in Python code?在 Python 代码中更有意义地打印结果?
【发布时间】:2021-11-01 14:08:11
【问题描述】:

我正在对 Cgamma 的不同值的 10 折数据集执行 SVM

from sklearn.datasets import load_digits, load_iris, load_breast_cancer, load_wine
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.utils import shuffle
from sklearn import preprocessing
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import accuracy_score, zero_one_loss, confusion_matrix
import pandas as pd
import numpy as np

z = pd.read_csv('/home/user/iris.csv', header=0)

X = z.iloc[:, :-1]
y = z.iloc[:, -1:]

X = np.array(X)
y = np.array(y)

# Performing standard scaling
scaler = preprocessing.MinMaxScaler()
X_scaled = scaler.fit_transform(X)

c = [0.1, 0.5]
gamma_values = [1e-1, 1]
     
for z in c:
    for v in gamma_values: 
        # Defining the SVM with 'rbf' kernel
        svc = SVC(kernel='rbf',C=z, gamma=v, random_state=50)

        skf = StratifiedKFold(n_splits=10, shuffle=True)
        acc_score = []
        #skf.get_n_splits(X, y)

        for train_index, test_index in skf.split(X, y):
            X_train, X_test = X_scaled[train_index], X_scaled[test_index]
            y_train, y_test = y[train_index], y[test_index]

            # Training the model
            svc.fit(X_train, np.ravel(y_train))

            # Prediction on test dataste
            y_pred = svc.predict(X_test)

            # Obtaining the accuracy scores of the model
            score = accuracy_score(y_test, y_pred)
            acc_score.append(score)
        print(np.array(acc_score))
        #print the accuracy score for each of the C values
        print('Mean accuracy score: %0.3f' % np.array(acc_score).mean())

导致如下输出

[0.52 0.6  0.49 0.6  0.55 0.6  0.5  0.51 0.63 0.54]
Mean accuracy score: 0.554
[0.51 0.45 0.54 0.42 0.53 0.45 0.52 0.48 0.5  0.39]
Mean accuracy score: 0.479
[0.73 0.76 0.7  0.64 0.61 0.68 0.71 0.61 0.71 0.71]
Mean accuracy score: 0.686
[0.76 0.6  0.66 0.61 0.67 0.66 0.69 0.74 0.63 0.65]
Mean accuracy score: 0.667

但是,我想更有意义地打印结果,如下所示:

[0.52 0.6  0.49 0.6  0.55 0.6  0.5  0.51 0.63 0.54]
Mean accuracy score for (C=0.1,gamma=0.1): 0.554
[0.51 0.45 0.54 0.42 0.53 0.45 0.52 0.48 0.5  0.39]
Mean accuracy score (C=0.1, gamma = 1): 0.479
[0.73 0.76 0.7  0.64 0.61 0.68 0.71 0.61 0.71 0.71]
Mean accuracy score (C=0.5, gamma = 0.1): 0.686
[0.76 0.6  0.66 0.61 0.67 0.66 0.69 0.74 0.63 0.65]
Mean accuracy score (C=0.5, gamma = 1): 0.667

如何在现有代码中更有意义地打印结果?

【问题讨论】:

    标签: python python-3.x machine-learning scikit-learn printing


    【解决方案1】:

    你可以使用fancy output formatting:

    print(f'Mean accuracy score (C={z:.1f}, gamma={v:.1f}): {np.array(acc_score).mean()}')
    

    【讨论】:

      【解决方案2】:

      试试这个:

      # (1)
      print('Mean accuracy score (C=%0.1f, gamma=%0.1f): %0.3f' % (z, v, np.array(acc_score).mean()))
      
      # (2)
      print("Mean accuracy score (C={}, gamma={}): {}".format(z, v, np.array(acc_score).mean()))
      
      # (3)
      print("Mean accuracy score (C="+str(z)+", gamma="+str(v)+"): "+str(np.array(acc_score).mean()))
      
      
      

      输出:

      Mean accuracy score (C=0.1, gamma=0.1): 0.554
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-06
        • 2016-03-08
        • 1970-01-01
        • 2013-03-31
        • 2019-03-11
        • 2022-01-16
        • 1970-01-01
        • 2022-12-05
        相关资源
        最近更新 更多