accuracy(准确率)
概念:模型正确预测的样本数量与总样本数量的比例。
公式:accuracy = (tp + tn) / (tp + tn + fp + fn)
tp (true positives):正确预测为正例的样本数。即模型正确地将正例判定为正例。
tn (true negatives):正确预测为负例的样本数。即模型正确地将负例判定为负例。
fp (false positives):错误预测为正例的样本数。即模型错误地将负例判定为正例。
fn (false negatives):错误预测为负例的样本数。即模型错误地将正例判定为负例。
代码实现
from sklearn.metrics import accuracy_score
y_true = [0, 1, 1, 0, 1, 0]
y_pred = [0, 1, 0, 0, 1, 1]
accuracy = accuracy_score(y_true, y_pred)
print("accuracy:", accuracy)
precision(精确度)
概念:被模型正确分类为正例的样本数量与所有被模型分类为正例的样本数量的比例。
公式:precision = tp / (tp + fp)
代码实现
from sklearn.metrics import precision_score
precision = precision_score(y_true, y_pred)
print("precision:", precision)
recall(召回率)
概念:在所有实际正例中,模型正确识别的比例。
公式:recall = tp / (tp + fn)
代码实现
from sklearn.metrics import recall_score
recall = recall_score(y_true, y_pred)
print("recall:", recall)
f1-score
概念:综合了模型的精确度和召回率,是一个更全面的指标。
公式:f1 score = 2 * (precision * recall) / (precision + recall)
代码实现
from sklearn.metrics import f1_score
f1 = f1_score(y_true, y_pred)
print("f1-score:", f1)
time taken(花费时间)
这个指标通常不是用公式来计算的,而是通过代码中记录开始时间和结束时间,然后计算时间差来得出。
root mean-squared error (rmse)(均方根误差)
概念:衡量模型预测值与真实值之间的平均差异。是均方误差的平方根。
公式:rmse = sqrt(mse)
from sklearn.metrics import mean_squared_error
import numpy as np
y_true = np.array([3.0, 2.5, 4.8])
y_pred = np.array([2.8, 2.7, 4.5])
mse = mean_squared_error(y_true, y_pred)
rmse = np.sqrt(mse)
print("rmse:", rmse)
mean absolute error (mae)(平均绝对误差)
概念:衡量模型预测值与真实值之间的平均绝对差异。
公式:mae = (|y_true - y_pred|) / n
from sklearn.metrics import mean_absolute_error
mae = mean_absolute_error(y_true, y_pred)
print("mae:", mae)
log-loss/cross-entropy loss(对数损失/交叉熵损失)
概念:衡量模型在预测概率时的准确性。适用于二分类问题的交叉熵损失为对数损失。
公式:log-loss = - (y_true * log(y_pred) + (1 - y_true) * log(1 - y_pred))
代码实现
from sklearn.metrics import log_loss
y_true = [0, 1, 1, 0]
y_pred = [0.2, 0.8, 0.7, 0.3]
logloss = log_loss(y_true, y_pred)
print("log-loss:", logloss)
发表评论