从分类器的准确度结果计算标准误差?
Posted
技术标签:
【中文标题】从分类器的准确度结果计算标准误差?【英文标题】:Calculating standard errors from accuracy results of a classifier? 【发布时间】:2021-11-02 06:18:53 【问题描述】:下面的代码打印超过 10 倍的准确度分数,如下所示
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/datasets/iris_dataset.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)
# Defining the SVM with 'rbf' kernel
svc = SVC(kernel='rbf',random_state=50)
#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.10, shuffle=True)
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(acc_score)
代码的输出(acc_score)如下所示:
[1.0, 0.9333333333333333, 1.0, 0.9333333333333333, 0.9333333333333333, 1.0, 0.9333333333333333, 0.9333333333333333, 1.0, 1.0]
据此,如何在 Python 中使用 NumPy
和 sklearn
计算这些准确度结果的 standard error
和 average accuracy
?我希望打印这些准确度分数的标准误差以及平均准确度
【问题讨论】:
请发送绘制这些结果的代码,以便我的帮助更有用 @gtomer : 已添加代码以便对您更有帮助 【参考方案1】:计算平均值:
print ('ACC mean:', '0:0.2f'.format(np.mean(acc_score)))
计算标准误:
print ('ACC std:', '0:0.2f'.format(np.std(acc_score)))
【讨论】:
谢谢!如何计算上述准确度分数的标准误差? 我已经添加了stdev计算 我认为 np.std 计算标准偏差而不是标准误差。标准误和标准差完全不同【参考方案2】:要计算平均值的标准误差(或测量的标准误差),可以使用scipy:
import scipy
from scipy import stats
acc_score = [1.0, 0.9333333333333333, 1.0, 0.9333333333333333, 0.9333333333333333, 1.0, 0.9333333333333333, 0.9333333333333333, 1.0, 1.0]
print('ACC std:', '0:0.2f'.format(scipy.stats.sem(acc_score)))
【讨论】:
以上是关于从分类器的准确度结果计算标准误差?的主要内容,如果未能解决你的问题,请参考以下文章