{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# TensorFlow2.0教程-结构化数据分类\n", "\n", "tensorflow2教程知乎专栏:https://zhuanlan.zhihu.com/c_1091021863043624960\n", "\n", "本教程展示了如何对结构化数据进行分类(例如CSV中的表格数据)。我们使用Keras定义模型,并将csv中各列的特征转化为训练的输入。 本教程包含一下功能代码:\n", "\n", "- 使用Pandas加载CSV文件。\n", "- 构建一个输入的pipeline,使用tf.data批处理和打乱数据。\n", "- 从CSV中的列映射到用于训练模型的输入要素。\n", "- 使用Keras构建,训练和评估模型。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.0.0-alpha0\n" ] } ], "source": [ "from __future__ import absolute_import, division, print_function\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "import tensorflow as tf\n", "\n", "from tensorflow import feature_column\n", "from tensorflow.keras import layers\n", "from sklearn.model_selection import train_test_split\n", "print(tf.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.数据集\n", "我们将使用克利夫兰诊所心脏病基金会提供的一个小数据集。 CSV中有几百行。 每行描述一个患者,每列描述一个属性。 我们将使用此信息来预测患者是否患有心脏病,该疾病在该数据集中是二元分类任务。\n", "\n", ">Column| Description| Feature Type | Data Type\n", ">------------|--------------------|----------------------|-----------------\n", ">Age | Age in years | Numerical | integer\n", ">Sex | (1 = male; 0 = female) | Categorical | integer\n", ">CP | Chest pain type (0, 1, 2, 3, 4) | Categorical | integer\n", ">Trestbpd | Resting blood pressure (in mm Hg on admission to the hospital) | Numerical | integer\n", ">Chol | Serum cholestoral in mg/dl | Numerical | integer\n", ">FBS | (fasting blood sugar > 120 mg/dl) (1 = true; 0 = false) | Categorical | integer\n", ">RestECG | Resting electrocardiographic results (0, 1, 2) | Categorical | integer\n", ">Thalach | Maximum heart rate achieved | Numerical | integer\n", ">Exang | Exercise induced angina (1 = yes; 0 = no) | Categorical | integer\n", ">Oldpeak | ST depression induced by exercise relative to rest | Numerical | integer\n", ">Slope | The slope of the peak exercise ST segment | Numerical | float\n", ">CA | Number of major vessels (0-3) colored by flourosopy | Numerical | integer\n", ">Thal | 3 = normal; 6 = fixed defect; 7 = reversable defect | Categorical | string\n", ">Target | Diagnosis of heart disease (1 = true; 0 = false) | Classification | integer\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.准备数据\n", "使用pandas读取数据" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | age | \n", "sex | \n", "cp | \n", "trestbps | \n", "chol | \n", "fbs | \n", "restecg | \n", "thalach | \n", "exang | \n", "oldpeak | \n", "slope | \n", "ca | \n", "thal | \n", "target | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "63 | \n", "1 | \n", "1 | \n", "145 | \n", "233 | \n", "1 | \n", "2 | \n", "150 | \n", "0 | \n", "2.3 | \n", "3 | \n", "0 | \n", "fixed | \n", "0 | \n", "
| 1 | \n", "67 | \n", "1 | \n", "4 | \n", "160 | \n", "286 | \n", "0 | \n", "2 | \n", "108 | \n", "1 | \n", "1.5 | \n", "2 | \n", "3 | \n", "normal | \n", "1 | \n", "
| 2 | \n", "67 | \n", "1 | \n", "4 | \n", "120 | \n", "229 | \n", "0 | \n", "2 | \n", "129 | \n", "1 | \n", "2.6 | \n", "2 | \n", "2 | \n", "reversible | \n", "0 | \n", "
| 3 | \n", "37 | \n", "1 | \n", "3 | \n", "130 | \n", "250 | \n", "0 | \n", "0 | \n", "187 | \n", "0 | \n", "3.5 | \n", "3 | \n", "0 | \n", "normal | \n", "0 | \n", "
| 4 | \n", "41 | \n", "0 | \n", "2 | \n", "130 | \n", "204 | \n", "0 | \n", "2 | \n", "172 | \n", "0 | \n", "1.4 | \n", "1 | \n", "0 | \n", "normal | \n", "0 | \n", "