模型评价 - 分类模型的常用评价指标

分类模型的常用评价指标

基本指标:误差率

指标解释:错分类样本占总样本的比例

基本指标:准确率

指标解释:正确分类样本占总样本的比例
指标解读:准确率越接近1,模型越准确

混淆矩阵

真实情况预测:正例预测:反例
正例TP(真正例)FN(假反例)
反例FP(假正例)TN(真反例)

衍生指标:查准率(precision)

指标解释:所有真正例占所有预测为正的样本的比例
指标举例:在商品推荐的过程中,我们会关心所有推荐给用户的商品(预测为正)中有多少是客户真正喜欢的(真正例)

衍生指标:查全率(recall)

指标解释:所有真正例占所有真实为正的样本的比例
指标举例:在银行用户风险识别中,我们会关心,所有有风险的用户,有多少能被我们的模型识别出来

其他指标:ROC曲线与AUC值

ROC曲线:以真正例比率为纵轴、假正例率为横轴,采用不同的截断点,来绘制ROC曲线
AUC值:ROC曲线与坐标轴构成的图形面积
指标解读:AUC指标越接近1,则代表模型准确率越高;AUC值等于0.5,代表模型准确率与随机猜测准确率一致;AUC值小于0.5时,模型效果不如随机猜测

使用sklearn查看回归模型的各项指标

用到的房价预测的数据集:https://download.csdn.net/download/d1240673769/20910882

# 使用sklearn查看回归模型的各项指标
import pandas as pd
import matplotlib.pyplot as plt

# 样例数据读取
df = pd.read_excel('realestate_sample_preprocessed.xlsx')

# 根据共线性矩阵,保留与房价相关性最高的日间人口,将夜间人口和20-39岁夜间人口进行比例处理
def age_percent(row):
    if row['nightpop'] == 0:
        return 0
    else:
        return row['night20-39']/row['nightpop']
    
df['per_a20_39'] = df.apply(age_percent,axis=1)
df = df.drop(columns=['nightpop','night20-39'])

# 制作标签变量
price_median = df['average_price'].median()
print(price_median)
df['is_high'] = df['average_price'].map(lambda x: True if x>= price_median else False)
print(df['is_high'].value_counts())

# 数据集基本情况查看
print(df.shape)
print(df.dtypes)
print(df.isnull().sum())

在这里插入图片描述

划分数据集

# 划分数据集
x = df[['complete_year','area', 'daypop', 'sub_kde',
       'bus_kde', 'kind_kde','per_a20_39']]
y = df['is_high']
print(x.shape)
print(y.shape)

在这里插入图片描述

建立分类模型

# 建立分类模型
# 使用pipeline整合数据处理、特征筛选与模型
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler, PowerTransformer
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline

# 构建模型工作流
pipe_clf = Pipeline([
        ('sc',StandardScaler()),
        ('power_trans',PowerTransformer()),
        ('polynom_trans',PolynomialFeatures(degree=3)),
        ('lgostic_clf', LogisticRegression(penalty='l1', fit_intercept=True, solver='liblinear'))
        ])
print(pipe_clf)

在这里插入图片描述

查看模型表现

# 查看模型表现
import warnings
from sklearn.metrics import accuracy_score, precision_score, recall_score, roc_auc_score
warnings.filterwarnings('ignore')
pipe_clf.fit(x,y)
y_predict = pipe_clf.predict(x)

print(f'accuracy score is: {accuracy_score(y,y_predict)}')
print(f'precision score is: {precision_score(y,y_predict)}')
print(f'recall score is: {recall_score(y,y_predict)}')
print(f'auc: {roc_auc_score(y,y_predict)}')

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jepson2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值