datawhale数据分析5——数据可视化数据建模及模型评估

人工智能27

Task05:数据建模及模型评估

声明:本文主要参考DataWhale开源学习——动手学数据分析,GitHub地址:https://github.com/datawhalechina/hands-on-data-analysis


import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from IPython.display import Image
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['figure.figsize'] = (10, 6)
train = pd.read_csv('train.csv')
train.shape
(891, 12)

【思考】这些库的作用是什么呢?你需要查一查

【思考题回答】numpy,matplotlib,pandas是数据分析的三大基本库。

       seaborn是在Matplotlib的基础上进行了更高级的API封装,因此可以进行更复杂的图形设计和输出的库。

       %matplotlib inline:将matplotlib的图表直接嵌入到Notebook中。

一.特征工程

特征工程是整个机器学习中非常重要的一部分。如何从数据中提取特征对最终的结果有很大的影响。在建模过程中,算法和参数一般优先考虑,但数据特性决定了整体结果的上限,算法和参数只决定如何逼近上限。事实上,特征工程就是从原始数据中找到最有价值的信息,并将其转化为计算机可以阅读的形式。

[En]

Feature engineering is a very important part of the whole machine learning. How to extract features from the data has a great impact on the final result. In the process of modeling, algorithms and parameters are generally given priority, but the data characteristics determine the upper limit of the overall results, and the algorithm and parameters only determine how to approach the upper limit. In fact, feature engineering is to find the most valuable information from the original data and transform it into a form that the computer can read.

1.1 任务一:缺失值填充

缺失值填充方法:

  • 对分类变量缺失值:填充某个缺失值字符(NA)、用最多类别的进行填充
  • 对连续变量缺失值:填充均值、中位数、众数

train['Cabin'] = train['Cabin'].fillna('NA')
train['Embarked'] = train['Embarked'].fillna('S')

train['Age'] = train['Age'].fillna(round(train['Age'].mean()))

train.isnull().sum().sort_values(ascending=False)
Embarked       0
Cabin          0
Fare           0
Ticket         0
Parch          0
SibSp          0
Age            0
Sex            0
Name           0
Pclass         0
Survived       0
PassengerId    0
dtype: int64

1.2 任务二:编码分类变量

这里采用 pd.get_dummies()函数进行虚拟变量转换,get_dummies是利用pandas实现one hot encode的方式。

  • 官方文档:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.get_dummies.html

data = train[['Pclass','Sex','Age','SibSp','Parch','Fare', 'Embarked']]

data = pd.get_dummies(data)
data.head()

PclassAgeSibSpParchFareSex_femaleSex_maleEmbarked_CEmbarked_QEmbarked_S0322.0107.2500010011138.01071.2833101002326.0007.9250100013135.01053.1000100014335.0008.050001001

二.模型搭建

  • 处理完前面的数据我们就得到建模数据,下一步是选择合适模型
  • 在进行模型选择之前我们需要先知道数据集最终是进行 监督学习还是 无监督学习(根据原先有没有标签)
  • 模型的选择一方面是通过我们的任务来决定的。
  • 除了根据我们的任务选择模型外,还可以根据数据样本的大小和特征的稀疏性来确定
    [En]

    in addition to selecting the model according to our task, it can also be determined according to the size of data samples and the sparsity of features.*

  • 刚开始我们总是先尝试使用一个基本的模型来作为其baseline,进而再训练其他模型做对比,最终选择泛化能力或性能比较好的模型

我们这里使用一个机器学习最常用的一个库(sklearn)来完成我们的模型的搭建

官网:https://scikit-learn.org/stable/

下面给出sklearn模型算法路径选择图

datawhale数据分析5——数据可视化数据建模及模型评估

; 2.1 任务一:切割训练集和测试集

这里使用 留出法划分数据集

  • 将数据集分为自变量和因变量
  • 按比例切割训练集和测试集(一般测试集的比例有30%、25%、20%、15%和10%)
  • 使用分层抽样
  • 设置随机种子以便结果能复现

【思考】

  • 划分数据集的方法有哪些?
  • 为什么使用分层抽样,这样的好处有什么?

【思考回答】

  • 划分数据集的方法:留出法,交叉验证法,自助法。参考:https://blog.csdn.net/weixin_38753213/article/details/112690712
  • 分层采用好处:1.当一个总体内部分层明显时,分层抽样能够提高样本的代表性;2.分层抽样特别适用于既要对总体参数进行推断,也要对各子总体的参数进行推断的情形;3.分层抽样实施起来灵活方便,而且便于组织。

任务提示1

  • 切割数据集是为了后续能评估模型泛化能力
  • sklearn中切割数据集的方法为 train_test_split
  • 查看函数文档可以在jupyter notebook里面使用 train_test_split?后回车即可看到
  • 分层和随机种子在参数里寻找
from sklearn.model_selection import train_test_split
X = data
y = train['Survived']

X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0)

X_train.shape, X_test.shape
((668, 10), (223, 10))

【思考】什么情况下切割数据集的时候不用进行随机选取?

[思考答案]数据在被切割之前已经被切割的情况,或者数据集本身非常大的情况。

[En]

[thinking answer] A situation where the data has been cut before it is cut or where the data set itself is very large.

2.2 任务二:模型创建

  • 创建基于线性模型的分类模型(逻辑回归)
  • 创建基于树的分类模型(决策树、随机森林)
  • 分别使用这些模型进行训练,分别的到训练集和测试集的得分
  • 查看模型的参数,并更改参数值,观察模型变化

提示2

  • 逻辑回归不是回归模型而是分类模型,不要与 LinearRegression混淆
  • 随机森林其实是决策树集成为了降低决策树过拟合的情况
  • 线性模型所在的模块为 sklearn.linear_model
  • 树模型所在的模块为 sklearn.ensemble

2.2.1 逻辑回归

逻辑回归不是回归模型而是分类模型,在 sklearn.linear_model中,需要调节参数C(正则化系数)

  • 正则化是机器学习中的概念,具体可参考西瓜书或其它机器学习相关资料,举个例子简单来说,如果训练集有n个样本,那么一定可以通过一个n-1阶函数完美拟合这些点,使得样本在训练集中的准确率是100%,但是这个模型的泛化能力很差,在测试集上效果就不好了。正则化惩罚就是通过惩罚高阶项解决过拟合问题从而提高模型的泛化能力。
from sklearn.linear_model import LogisticRegression

lr = LogisticRegression()
lr.fit(X_train,y_train)
print('训练集得分:{:.2f}'.format(lr.score(X_train,y_train)))
print('测试集得分:{:.2f}'.format(lr.score(X_test,y_test)))
训练集得分:0.80
测试集得分:0.78

lr2 = LogisticRegression(C=100)
lr2.fit(X_train, y_train)
print('训练集得分:{:.2f}'.format(lr2.score(X_train,y_train)))
print('测试集得分:{:.2f}'.format(lr2.score(X_test,y_test)))
训练集得分:0.80
测试集得分:0.79

2.2.2 随机森林

随机森林是 集成学习中的一种算法,通过并行训练一堆决策树来各自独立地学习和作出预测。这些预测最后结合成单预测,因此优于任何一个单分类的做出预测。

可参考:https://blog.csdn.net/yangyin007/article/details/82385967

这里采用 sklearn.emsemble模块,主要调节 n_estimateorsmax_depth

  • n_estimateors:决策树的个数,数量太少欠拟合,数量太多过拟合
  • max_depth:决策树的最大深度,同样深度太浅欠拟合,深度太深过拟合
from sklearn.ensemble import RandomForestClassifier

rfc = RandomForestClassifier()
rfc.fit(X_train,y_train)
print('训练集得分:{:.2f}'.format(rfc.score(X_train,y_train)))
print('测试集得分:{:.2f}'.format(rfc.score(X_test,y_test)))
训练集得分:0.97
测试集得分:0.82

rfc2 = RandomForestClassifier(n_estimators=80, max_depth=5)
rfc2.fit(X_train, y_train)
print("Training set score: {:.2f}".format(rfc2.score(X_train, y_train)))
print("Testing set score: {:.2f}".format(rfc2.score(X_test, y_test)))
Training set score: 0.86
Testing set score: 0.83

【思考】

  • 为什么线性模型可以进行分类任务,背后是怎么的数学关系
  • 对于多分类问题,线性模型是怎么进行分类的

【思考回答】

  • 数据以线性平面分隔,分为平面上和平面下的数据,实现两种分类
    [En]

    the data are separated by a linear plane, which is divided into data above and below the plane, so as to achieve two classifications.*

  • 两个想法:1.通过点距离平面的远近进行分类;2.转化为多个二分类问题求解。

2.3 任务三:输出模型预测结果

  • 输出模型预测分类标签
  • 输出不同分类标签的预测概率

提示3

  • 一般监督模型在sklearn里面有个 predict能输出预测标签, predict_proba则可以输出标签概率

lr_predict = lr.predict(X_train)
lr_predict[:10]
array([0, 1, 1, 1, 0, 0, 1, 0, 1, 1], dtype=int64)

lr_pre = lr.predict_proba(X_train)
lr_pre[:10]
array([[0.62834351, 0.37165649],
       [0.14872739, 0.85127261],
       [0.47129261, 0.52870739],
       [0.20331331, 0.79668669],
       [0.86395534, 0.13604466],
       [0.90416819, 0.09583181],
       [0.13796589, 0.86203411],
       [0.89506733, 0.10493267],
       [0.05712809, 0.94287191],
       [0.13565136, 0.86434864]])

【思考】

  • 预测标签的概率对我们有什么帮助

【思考回答】

  • 我们可以根据自己的需求调整门槛,得到我们想要的分类结果。
    [En]

    We can adjust the threshold to get the classification results we want according to our own needs.*

三.模型评估

  • 模型评估是为了知道模型的泛化能力。
  • 交叉验证(cross-validation)是一种评估泛化性能的统计学方法,它比单次划分训练集和测试集的方法更加稳定、全面。
  • 在交叉验证中,数据被多次划分,并且需要训练多个模型。
  • 最常用的交叉验证是 k 折交叉验证(k-fold cross-validation),其中 k 是由用户指定的数字,通常取 5 或 10。
  • 准确率(precision)度量的是被预测为正例的样本中有多少是真正的正例
  • 召回率(recall)度量的是正类样本中有多少被预测为正类
  • f-分数是准确率与召回率的调和平均

3.1 任务一:交叉验证

  • 用10折交叉验证来评估之前的逻辑回归模型
  • 计算交叉验证精度的平均值

可参考:https://blog.csdn.net/tianguiyuyu/article/details/80697223

datawhale数据分析5——数据可视化数据建模及模型评估

; 提示4

  • 交叉验证在sklearn中的模块为 sklearn.model_selection
from sklearn.model_selection import cross_val_score
lr = LogisticRegression()
score = cross_val_score(lr,X_train,y_train,cv=10)
score.mean()
0.7977425705696117
  • 【思考】k折越多的情况下会带来什么样的影响?
  • 【思考回答】会导致测试集的样本减少,最终score的方差会加大

3.2 任务二:混淆矩阵

  • 计算二分类问题的混淆矩阵
  • 计算精确率、召回率以及f-分数
  • [思考]二分分类问题的混淆矩阵是什么,理解概念并知道它主要计算成哪些任务?
    [En]

    [thinking] what is the confusion matrix of the binary classification problem, understand the concept and know what tasks it is mainly calculated into?*

  • 【思考回答】混淆矩阵是一个二维方阵,包含四个情况:
    真实值是positive,模型认为是positive的数量(True Positive=TP)
    真实值是positive,模型认为是negative的数量(False Negative=FN):这就是统计学上的第二类错误
    真实值是negative,模型认为是positive的数量(False Positive=FP):这就是统计学上的第一类错误
    真实值是negative,模型认为是negative的数量(True Negative=TN)

datawhale数据分析5——数据可视化数据建模及模型评估

datawhale数据分析5——数据可视化数据建模及模型评估

; 提示5

  • 混淆矩阵的方法在sklearn中的 sklearn.metrics模块
  • 混淆矩阵需要输入真实标签和预测标签
  • 精确率、召回率以及f-分数可使用 classification_report模块
from sklearn.metrics import confusion_matrix

lr = LogisticRegression()
lr.fit(X_train, y_train)
pred = lr.predict(X_train)

cnf_matrix = confusion_matrix(y_train,pred)
def plot_confusion_matrix(cm, classes,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
"""
    This function prints and plots the confusion matrix.

"""
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=0)
    plt.yticks(tick_marks, classes)

    thresh = cm.max() / 2.

    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

import itertools
class_names = [0,1]
plt.figure()
plot_confusion_matrix(cnf_matrix
                      , classes=class_names
                      , title='Confusion matrix')
plt.show()

datawhale数据分析5——数据可视化数据建模及模型评估

from sklearn.metrics import classification_report

print(classification_report(y_train, pred))
             precision    recall  f1-score   support

          0       0.83      0.86      0.84       412
          1       0.76      0.71      0.74       256

avg / total       0.80      0.80      0.80       668

3.3 任务三:ROC曲线

  • 绘制ROC曲线
  • 【思考】什么是ROC曲线,OCR曲线的存在是为了解决什么问题?
  • 【思考回答】https://blog.csdn.net/zhaomengszu/article/details/72471474

提示6

  • ROC曲线在sklearn中的模块为 sklearn.metrics
  • ROC曲线下面所包围的面积越大越好
from sklearn.metrics import roc_curve
fpr, tpr, thresholds = roc_curve(y_test, lr.decision_function(X_test))
plt.plot(fpr, tpr, label="ROC Curve")
plt.xlabel("FPR")
plt.ylabel("TPR (recall)")

close_zero = np.argmin(np.abs(thresholds))
plt.plot(fpr[close_zero], tpr[close_zero], 'o', markersize=10, label="threshold zero", fillstyle="none", c='k', mew=2)
plt.legend(loc=4)
<matplotlib.legend.legend at 0x1bb2a9b4b00>
</matplotlib.legend.legend>

datawhale数据分析5——数据可视化数据建模及模型评估

  • 【问题】对于多分类问题如何绘制ROC曲线
  • 【问题回答】https://blog.csdn.net/xyz1584172808/article/details/81839230

Original: https://blog.csdn.net/weixin_45885072/article/details/123701008
Author: 情定诺坎普1
Title: datawhale数据分析5——数据可视化数据建模及模型评估



相关阅读

Title: Tensorflow 2.9.1安装笔记

CPU:i7-4790k

显卡:GTX2060

Cuda 版本:11.3

Cunn版本: 11.6

Python版本:3.7.7

不想用anacoda,直接装 tensorflow

1.准备工作

  • 安装python3.7.7(之前安装好的)

可以根据需要安装相应的版本,不建议安装最新版,python版本之间的代码兼容度不好。3.6~3.8可能比较合适。

我安装的是11.3版本。

deviceQuery.exe 和 bandwithTest.exe测试通过。

  • 下载Tensorflow

我下载的是 tensorflow-2.9.1-cp37-cp37m-win_amd64.whl

  • 安装组件

安装Tensorflow之前,安装好以下支持模块

A.Numpy: pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple

B.mkl: pip install mkl -i https://pypi.tuna.tsinghua.edu.cn/simple

C.protobuf pip install protobuf -i https://pypi.tuna.tsinghua.edu.cn/simple

2.安装Tensorflow

把 tensorflow-2.9.1-cp37-cp37m-win_amd64.whl 放到d盘根目录,打开命令行并转到D:\

pip install tensorflow-2.9.1-cp37-cp37m-win_amd64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple

这样在安装过程中加载其他模块的时候,速度会非常快。

3.测试

import tensorflow as tf
print("Tensorflow version:")
print(tf.__version__)

print("CPU/GPU devices for Tensorflow:")
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
cpus = tf.config.experimental.list_physical_devices(device_type='CPU')
print(gpus)
print(cpus)

运行结果:

Tensorflow version:
2.9.1
CPU/GPU devices for Tensorflow:
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]

至此安装完毕。

IDE可以使用Visual Studio Code(小规模测试)

或者Pycharm(程序较大很方便)

Original: https://blog.csdn.net/st01lsp/article/details/125294794
Author: st01lsp
Title: Tensorflow 2.9.1安装笔记

相关文章
TensorFlow和Pytorch中的音频增强 人工智能

TensorFlow和Pytorch中的音频增强

对于图像相关的任务,对图像进行旋转、模糊或调整大小是常见的数据增强的方法。 因为图像的自身属性与其他数据类型数据增强相比,图像的数据增强是非常直观的,我们只需要查看图像就可以看到特定图像是如何转换的,...
用DirectX实现多视图渲染 人工智能

用DirectX实现多视图渲染

什么是多视图 一般的3D程序都只有一个视图,对应整个窗口的客户区。多视图就是在一个窗口中放置多个视图,以便从不同的角度观察模型或者场景。很多图形软件都有这个功能,比如大家熟知的3DMax就有四个视图,...