当前位置:   article > 正文

机器学习中性能度量——sklearn.metrics_from sklearn import metrics

from sklearn import metrics

accuracy_score

sklearn.metrics.accuracy_score

import numpy as np 
from sklearn.metrics import accuracy_score 
y_pred = [0, 1, 0, 1] 
y_true = [0, 1, 1, 1]
print('ACC:',accuracy_score(y_true, y_pred))
#ACC:0.75

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Precision

sklearn.metrics.precision_score

from sklearn import metrics
y_pred = [0, 1, 0, 0] 
y_true = [0, 1, 0, 1]
print('Precision',metrics.precision_score(y_true, y_pred))
#Precision 1.0
  • 1
  • 2
  • 3
  • 4
  • 5

Recall

sklearn.metrics.recall_score

from sklearn import metrics
y_pred = [0, 1, 0, 0] 
y_true = [0, 1, 0, 1]
print('Recall',metrics.recall_score(y_true, y_pred)) 
#Recall 0.5

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

F1-score

sklearn.metrics.f1_score

from sklearn import metrics
y_pred = [0, 1, 0, 0] 
y_true = [0, 1, 0, 1]
print('Recall',metrics.f1_score(y_true, y_pred)) 
#F1-score  0.6666666666666666 
  • 1
  • 2
  • 3
  • 4
  • 5

AUC

sklearn.metrics.roc_auc_score

import numpy as np 
from sklearn.metrics 
import roc_auc_score 
y_true = np.array([0, 0, 1, 1]) 
y_scores = np.array([0.1, 0.4, 0.35, 0.8]) 
print('AUC socre:',roc_auc_score(y_true, y_scores))
#AUC socre: 0.75 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

MSE 、RMSE、MAE、MAPE

import numpy as np 
from sklearn import metrics
# MAPE需要自己实现 
def mape(y_true, y_pred):    
	return np.mean(np.abs((y_pred - y_true) / y_true))
y_true = np.array([1.0, 5.0, 4.0, 3.0, 2.0, 5.0, -3.0]) 
y_pred = np.array([1.0, 4.5, 3.8, 3.2, 3.0, 4.8, -2.2])
# MSE 
print('MSE:',metrics.mean_squared_error(y_true, y_pred)) 
# RMSE 
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_true, y_pred))) 
# MAE 
print('MAE:',metrics.mean_absolute_error(y_true, y_pred)) 
# MAPE 
print('MAPE:',mape(y_true, y_pred))
#MSE: 0.2871428571428571 
#RMSE: 0.5358571238146014 
#MAE: 0.4142857142857143 
#MAPE: 0.1461904761904762 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

R2-score

from sklearn.metrics import r2_score 
y_true = [3, -0.5, 2, 7] 
y_pred = [2.5, 0.0, 2, 8] 
print('R2-score:',r2_score(y_true, y_pred))
#R2-score: 0.9486081370449679 
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/142787
推荐阅读
相关标签
  

闽ICP备14008679号