- 均方误差 (Mean Squared Error, MSE):
- 解释:MSE度量的是预测值与实际值之间的平均平方误差。它反映了预测值的平均偏离程度, 数值越小越好。
- 公式:
M S E = 1 n ∑ i = 1 n ( y i − y ^ i ) 2 \mathrm{MSE}=\frac{1}{n} \sum_{i=1}^n\left(y_i-\hat{y}_i\right)^2 MSE=n1i=1∑n(yi−y^i)2
def mse(y_true, y_pred):
n = len(y_true)
return sum((yt - yp) ** 2 for yt, yp in zip(y_true, y_pred)) / n
- 均方根误差(Root Mean Squared Error, RMSE):
- 解释:RMSE是MSE的平方根, 具有与原始数据相同的量纲, 更容易解释。
- 公式:
R M S E = M S E \mathrm{RMSE}=\sqrt{\mathrm{MSE}} RMSE=MSE
def rmse(y_true, y_pred):
return math.sqrt(mse(y_true, y_pred))
- 平均绝对误差 (Mean Absolute Error, MAE):
- 解释:MAE度量的是预测值与实际值之间的平均绝对误差。它反映了预测值的平均偏离程度, 但不考虑误差的方向。
- 公式:
M A E = 1 n ∑ i = 1 n ∣ y i − y ^ i ∣ \mathrm{MAE}=\frac{1}{n} \sum_{i=1}^n\left|y_i-\hat{y}_i\right| MAE=n1i=1∑n∣yi−y^i∣
def mae(y_true, y_pred):
n = len(y_true)
return sum(abs(yt - yp) for yt, yp in zip(y_true, y_pred)) / n
4. R 2 \mathrm{R}^2 R2 评分 (Coefficient of Determination, R 2 \mathrm{R}^2 R2 ):
- 解释: R 2 \mathrm{R}^2 R2 度量的是模型解释方差的比例, 取值范围为 0 到 1 , 值越大表示模型的解释能力越强。它表示的是模型解释的方差比例。
- 公式:
R 2 = 1 − ∑ i = 1 n ( y i − y ^ i ) 2 ∑ i = 1 n ( y i − y ˉ ) 2 R^2=1-\frac{\sum_{i=1}^n\left(y_i-\hat{y}_i\right)^2}{\sum_{i=1}^n\left(y_i-\bar{y}\right)^2} R2=1−∑i=1n(yi−yˉ)2∑i=1n(yi−y^i)2
其中, y ˉ \bar{y} yˉ 是实际值的均值。
def r2_score(y_true, y_pred):
mean_y_true = sum(y_true) / len(y_true)
ss_tot = sum((yt - mean_y_true) ** 2 for yt in y_true)
ss_res = sum((yt - yp) ** 2 for yt, yp in zip(y_true, y_pred))
return 1 - (ss_res / ss_tot)
分类问题的常用指标
- 准确率 (Accuracy):
- 解释:准确率是所有正确预测的样本占总样本数的比例。它衡量的是模型整体的准确性。
- 公式:
Accuracy = T P + T N T P + T N + F P + F N \text { Accuracy }=\frac{\mathrm{TP}+\mathrm{TN}}{\mathrm{TP}+\mathrm{TN}+\mathrm{FP}+\mathrm{FN}} Accuracy =TP+TN+FP+FNTP+TN
def accuracy(y_true, y_pred):
tp = sum(1 for yt, yp in zip(y_true, y_pred) if yt == yp and yt == 1) # 真正类
tn = sum(1 for yt, yp in zip(y_true, y_pred) if yt == yp and yt == 0) # 真负类
fp = sum(1 for yt, yp in zip(y_true, y_pred) if yt != yp and yt == 0) # 假正类
fn = sum(1 for yt, yp in zip(y_true, y_pred) if yt != yp and yt == 1) # 假负类
return (tp + tn) / (tp + tn + fp + fn)
- 精确率 (Precision):
- 解释:精确率是所有被预测为正类的样本中实际为正类的比例。它衡量的是正类预测的准确性。
- 公式:
Precision = T P T P + F P \text { Precision }=\frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FP}} Precision =TP+FPTP
def precision(y_true, y_pred):
tp = sum(1 for yt, yp in zip(y_true, y_pred) if yt == yp and yt == 1) # 真正类
fp = sum(1 for yt, yp in zip(y_true, y_pred) if yt != yp and yt == 0) # 假正类
return tp / (tp + fp)
- 召回率 (Recall):
- 解释:召回率是所有实际为正类的样本中被正确预测为正类的比例。它衡量的是模型发现正类样本的能力。
- 公式:
Recall = T P T P + F N \text { Recall }=\frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FN}} Recall =TP+FNTP
def recall(y_true, y_pred):
tp = sum(1 for yt, yp in zip(y_true, y_pred) if yt == yp and yt == 1) # 真正类
fn = sum(1 for yt, yp in zip(y_true, y_pred) if yt != yp and yt == 1) # 假负类
return tp / (tp + fn)
- F1分数 (F1 Score):
- 解释:F1分数是精确率和召回率的调和平均数, 用来平衡精确率和召回率。它在处理不平衡数据时尤为有效。
- 公式:
F1 Score = 2 ⋅ Precision ⋅ Recall Precision + Recall \text { F1 Score }=\frac{2 \cdot \text { Precision } \cdot \text { Recall }}{\text { Precision }+ \text { Recall }} F1 Score = Precision + Recall 2⋅ Precision ⋅ Recall
def f1_score(y_true, y_pred):
p = precision(y_true, y_pred)
r = recall(y_true, y_pred)
return 2 * p * r / (p + r)
- ROC AUC (Area Under the Receiver Operating Characteristic Curve):
- 解释:AUC表示ROC曲线下的面积, 用来衡量分类模型的性能。AUC值越接近1表示模型性能越好。
def roc_auc(y_true, y_pred_prob):
thresholds = sorted(set(y_pred_prob), reverse=True)
tpr_list = []
fpr_list = []
for thresh in thresholds:
tp = sum(1 for yt, yp in zip(y_true, y_pred_prob) if yt == 1 and yp >= thresh)
fp = sum(1 for yt, yp in zip(y_true, y_pred_prob) if yt == 0 and yp >= thresh)
fn = sum(1 for yt, yp in zip(y_true, y_pred_prob) if yt == 1 and yp < thresh)
tn = sum(1 for yt, yp in zip(y_true, y_pred_prob) if yt == 0 and yp < thresh)
tpr = tp / (tp + fn)
fpr = fp / (fp + tn)
tpr_list.append(tpr)
fpr_list.append(fpr)
auc = 0.0
for i in range(1, len(tpr_list)):
auc += (fpr_list[i] - fpr_list[i-1]) * (tpr_list[i] + tpr_list[i-1]) / 2
return auc
推荐系统的常用指标
- 平均准确率 (Mean Average Precision, MAP) :
- 解释:MAP是对多个查询结果的平均准确率的平均值, 衡量推荐系统的整体准确率。它计算每个查询的平均准确率, 然后对这些平均准确率取平均值。
- 公式:
M A P = 1 m ∑ i = 1 m A P i \mathrm{MAP}=\frac{1}{m} \sum_{i=1}^m \mathrm{AP}_i MAP=m1i=1∑mAPi
def average_precision(y_true, y_pred):
precisions = []
for true, pred in zip(y_true, y_pred):
relevant_items = 0
total_recommended = 0
precision = 0
for t, p in zip(true, pred):
if p == 1:
total_recommended += 1
if t == 1:
relevant_items += 1
precision += relevant_items / total_recommended
if relevant_items > 0:
precisions.append(precision / relevant_items)
return sum(precisions) / len(precisions) if precisions else 0
- 命中率(Hit Rate):
- 解释:命中率是所有推荐中被用户接受的推荐数占总推荐数的比例。它衡量推荐系统命中目标物品的能力。
- 公式:
Hit Rate = 命中数 推荐数 \text { Hit Rate }=\frac{\text { 命中数 }}{\text { 推荐数 }} Hit Rate = 推荐数 命中数
def hit_rate(y_true, y_pred):
hits = 0
total_recommendations = 0
for true, pred in zip(y_true, y_pred):
hits += sum(1 for t, p in zip(true, pred) if t == 1 and p == 1)
total_recommendations += sum(pred)
return hits / total_recommendations if total_recommendations != 0 else 0
- 平均归一化折损累计增益 (Mean Normalized Discounted Cumulative Gain, NDCG) :
- 解释:NDCG衡量的是推荐系统的推荐结果在不同位置上的相关性, 考虑了推荐结果的顺序。理想情况下的DCG(IDCG)是按照最相关的项目排序的DCG值。
- 公式:
N D C G = 1 m ∑ i = 1 m D C G i I D C G i \mathrm{NDCG}=\frac{1}{m} \sum_{i=1}^m \frac{\mathrm{DCG}_i}{\mathrm{IDCG}_i} NDCG=m1i=1∑mIDCGiDCGi
def ndcg(y_true, y_pred, k):
def dcg(relevance_scores):
return sum((2**rel - 1) / math.log2(idx + 2) for idx, rel in enumerate(relevance_scores))
ndcgs = []
for true, pred in zip(y_true, y_pred):
relevance_scores = [t if p == 1 else 0 for t, p in zip(true, pred)]
ideal_relevance_scores = sorted(true, reverse=True)
ndcgs.append(dcg(relevance_scores[:k]) / dcg(ideal_relevance_scores[:k]))
return sum(ndcgs) / len(ndcgs) if ndcgs else 0
代码合并
import math
# 模拟回归数据
y_true_reg = [3.0, -0.5, 2.0, 7.0]
y_pred_reg = [2.5, 0.0, 2.0, 8.0]
# 回归指标计算函数
def mse(y_true, y_pred):
"""
计算均方误差(Mean Squared Error, MSE)
:param y_true: 实际值列表
:param y_pred: 预测值列表
:return: MSE值
"""
n = len(y_true)
return sum((yt - yp) ** 2 for yt, yp in zip(y_true, y_pred)) / n
def rmse(y_true, y_pred):
"""
计算均方根误差(Root Mean Squared Error, RMSE)
:param y_true: 实际值列表
:param y_pred: 预测值列表
:return: RMSE值
"""
return math.sqrt(mse(y_true, y_pred))
def mae(y_true, y_pred):
"""
计算平均绝对误差(Mean Absolute Error, MAE)
:param y_true: 实际值列表
:param y_pred: 预测值列表
:return: MAE值
"""
n = len(y_true)
return sum(abs(yt - yp) for yt, yp in zip(y_true, y_pred)) / n
def r2_score(y_true, y_pred):
"""
计算R²评分(Coefficient of Determination, R²)
:param y_true: 实际值列表
:param y_pred: 预测值列表
:return: R²值
"""
mean_y_true = sum(y_true) / len(y_true)
ss_tot = sum((yt - mean_y_true) ** 2 for yt in y_true) # 总平方和
ss_res = sum((yt - yp) ** 2 for yt, yp in zip(y_true, y_pred)) # 残差平方和
return 1 - (ss_res / ss_tot)
# 计算回归指标并打印结果
print("MSE:", mse(y_true_reg, y_pred_reg))
print("RMSE:", rmse(y_true_reg, y_pred_reg))
print("MAE:", mae(y_true_reg, y_pred_reg))
print("R² Score:", r2_score(y_true_reg, y_pred_reg))
# 模拟分类数据
y_true_cls = [1, 0, 1, 1, 0, 1, 0, 0, 0, 1]
y_pred_cls = [1, 0, 1, 0, 0, 1, 0, 0, 1, 1]
# 分类指标计算函数
def accuracy(y_true, y_pred):
"""
计算准确率(Accuracy)
:param y_true: 实际类别列表
:param y_pred: 预测类别列表
:return: 准确率
"""
tp = sum(1 for yt, yp in zip(y_true, y_pred) if yt == yp and yt == 1) # 真正类
tn = sum(1 for yt, yp in zip(y_true, y_pred) if yt == yp and yt == 0) # 真负类
fp = sum(1 for yt, yp in zip(y_true, y_pred) if yt != yp and yt == 0) # 假正类
fn = sum(1 for yt, yp in zip(y_true, y_pred) if yt != yp and yt == 1) # 假负类
return (tp + tn) / (tp + tn + fp + fn)
def precision(y_true, y_pred):
"""
计算精确率(Precision)
:param y_true: 实际类别列表
:param y_pred: 预测类别列表
:return: 精确率
"""
tp = sum(1 for yt, yp in zip(y_true, y_pred) if yt == yp and yt == 1) # 真正类
fp = sum(1 for yt, yp in zip(y_true, y_pred) if yt != yp and yt == 0) # 假正类
return tp / (tp + fp)
def recall(y_true, y_pred):
"""
计算召回率(Recall)
:param y_true: 实际类别列表
:param y_pred: 预测类别列表
:return: 召回率
"""
tp = sum(1 for yt, yp in zip(y_true, y_pred) if yt == yp and yt == 1) # 真正类
fn = sum(1 for yt, yp in zip(y_true, y_pred) if yt != yp and yt == 1) # 假负类
return tp / (tp + fn)
def f1_score(y_true, y_pred):
"""
计算F1分数(F1 Score)
:param y_true: 实际类别列表
:param y_pred: 预测类别列表
:return: F1分数
"""
p = precision(y_true, y_pred)
r = recall(y_true, y_pred)
return 2 * p * r / (p + r)
def roc_auc(y_true, y_pred_prob):
"""
计算ROC AUC(Area Under the Receiver Operating Characteristic Curve)
:param y_true: 实际类别列表
:param y_pred_prob: 预测为正类的概率列表
:return: AUC值
"""
thresholds = sorted(set(y_pred_prob), reverse=True)
tpr_list = []
fpr_list = []
for thresh in thresholds:
tp = sum(1 for yt, yp in zip(y_true, y_pred_prob) if yt == 1 and yp >= thresh) # 真正类
fp = sum(1 for yt, yp in zip(y_true, y_pred_prob) if yt == 0 and yp >= thresh) # 假正类
fn = sum(1 for yt, yp in zip(y_true, y_pred_prob) if yt == 1 and yp < thresh) # 假负类
tn = sum(1 for yt, yp in zip(y_true, y_pred_prob) if yt == 0 and yp < thresh) # 真负类
tpr = tp / (tp + fn)
fpr = fp / (fp + tn)
tpr_list.append(tpr)
fpr_list.append(fpr)
# 计算AUC
auc = 0.0
for i in range(1, len(tpr_list)):
auc += (fpr_list[i] - fpr_list[i-1]) * (tpr_list[i] + tpr_list[i-1]) / 2
return auc
# 假设y_pred_cls_prob是预测为正类的概率
y_pred_cls_prob = [0.9, 0.1, 0.8, 0.4, 0.2, 0.85, 0.3, 0.15, 0.75, 0.95]
# 计算分类指标并打印结果
print("Accuracy:", accuracy(y_true_cls, y_pred_cls))
print("Precision:", precision(y_true_cls, y_pred_cls))
print("Recall:", recall(y_true_cls, y_pred_cls))
print("F1 Score:", f1_score(y_true_cls, y_pred_cls))
print("AUC:", roc_auc(y_true_cls, y_pred_cls_prob))
# 模拟推荐系统数据
# y_true: 每个用户的真实相关项目(1 表示相关,0 表示不相关)
# y_pred: 每个用户的推荐项目(1 表示推荐,0 表示不推荐)
y_true_rec = [[1, 0, 1, 0, 1], [0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]
y_pred_rec = [[1, 0, 0, 0, 1], [1, 1, 0, 0, 0], [0, 1, 0, 1, 1]]
# 推荐系统指标计算函数
def hit_rate(y_true, y_pred):
"""
计算命中率(Hit Rate)
:param y_true: 实际相关项目列表
:param y_pred: 推荐项目列表
:return: 命中率
"""
hits = 0
total_recommendations = 0
for true, pred in zip(y_true, y_pred):
hits += sum(1 for t, p in zip(true, pred) if t == 1 and p == 1)
total_recommendations += sum(pred)
return hits / total_recommendations if total_recommendations != 0 else 0
def average_precision(y_true, y_pred):
"""
计算平均准确率(Mean Average Precision, MAP)
:param y_true: 实际相关项目列表
:param y_pred: 推荐项目列表
:return: 平均准确率
"""
precisions = []
for true,