text stringlengths 0 4.99k |
|---|
This particular patient had a 18.8 percent probability of having a heart disease, as evaluated by our model. |
Using Wide & Deep and Deep & Cross networks for structured data classification. |
Introduction |
This example demonstrates how to do structured data classification using the two modeling techniques: |
Wide & Deep models |
Deep & Cross models |
Note that this example should be run with TensorFlow 2.5 or higher. |
The dataset |
This example uses the Covertype dataset from the UCI Machine Learning Repository. The task is to predict forest cover type from cartographic variables. The dataset includes 506,011 instances with 12 input features: 10 numerical features and 2 categorical features. Each instance is categorized into 1 of 7 classes. |
Setup |
import math |
import numpy as np |
import pandas as pd |
import tensorflow as tf |
from tensorflow import keras |
from tensorflow.keras import layers |
Prepare the data |
First, let's load the dataset from the UCI Machine Learning Repository into a Pandas DataFrame: |
data_url = ( |
\"https://archive.ics.uci.edu/ml/machine-learning-databases/covtype/covtype.data.gz\" |
) |
raw_data = pd.read_csv(data_url, header=None) |
print(f\"Dataset shape: {raw_data.shape}\") |
raw_data.head() |
Dataset shape: (581012, 55) |
0 1 2 3 4 5 6 7 8 9 ... 45 46 47 48 49 50 51 52 53 54 |
0 2596 51 3 258 0 510 221 232 148 6279 ... 0 0 0 0 0 0 0 0 0 5 |
1 2590 56 2 212 -6 390 220 235 151 6225 ... 0 0 0 0 0 0 0 0 0 5 |
2 2804 139 9 268 65 3180 234 238 135 6121 ... 0 0 0 0 0 0 0 0 0 2 |
3 2785 155 18 242 118 3090 238 238 122 6211 ... 0 0 0 0 0 0 0 0 0 2 |
4 2595 45 2 153 -1 391 220 234 150 6172 ... 0 0 0 0 0 0 0 0 0 5 |
5 rows × 55 columns |
The two categorical features in the dataset are binary-encoded. We will convert this dataset representation to the typical representation, where each categorical feature is represented as a single integer value. |
soil_type_values = [f\"soil_type_{idx+1}\" for idx in range(40)] |
wilderness_area_values = [f\"area_type_{idx+1}\" for idx in range(4)] |
soil_type = raw_data.loc[:, 14:53].apply( |
lambda x: soil_type_values[0::1][x.to_numpy().nonzero()[0][0]], axis=1 |
) |
wilderness_area = raw_data.loc[:, 10:13].apply( |
lambda x: wilderness_area_values[0::1][x.to_numpy().nonzero()[0][0]], axis=1 |
) |
CSV_HEADER = [ |
\"Elevation\", |
\"Aspect\", |
\"Slope\", |
\"Horizontal_Distance_To_Hydrology\", |
\"Vertical_Distance_To_Hydrology\", |
\"Horizontal_Distance_To_Roadways\", |
\"Hillshade_9am\", |
\"Hillshade_Noon\", |
\"Hillshade_3pm\", |
\"Horizontal_Distance_To_Fire_Points\", |
\"Wilderness_Area\", |
\"Soil_Type\", |
\"Cover_Type\", |
] |
data = pd.concat( |
[raw_data.loc[:, 0:9], wilderness_area, soil_type, raw_data.loc[:, 54]], |
axis=1, |
ignore_index=True, |
) |
data.columns = CSV_HEADER |
# Convert the target label indices into a range from 0 to 6 (there are 7 labels in total). |
data[\"Cover_Type\"] = data[\"Cover_Type\"] - 1 |
print(f\"Dataset shape: {data.shape}\") |
data.head().T |
Dataset shape: (581012, 13) |
0 1 2 3 4 |
Elevation 2596 2590 2804 2785 2595 |
Aspect 51 56 139 155 45 |
Slope 3 2 9 18 2 |
Horizontal_Distance_To_Hydrology 258 212 268 242 153 |
Vertical_Distance_To_Hydrology 0 -6 65 118 -1 |
Horizontal_Distance_To_Roadways 510 390 3180 3090 391 |
Hillshade_9am 221 220 234 238 220 |
Hillshade_Noon 232 235 238 238 234 |
Hillshade_3pm 148 151 135 122 150 |
Horizontal_Distance_To_Fire_Points 6279 6225 6121 6211 6172 |
Wilderness_Area area_type_1 area_type_1 area_type_1 area_type_1 area_type_1 |
Soil_Type soil_type_29 soil_type_29 soil_type_12 soil_type_30 soil_type_29 |
Cover_Type 4 4 1 1 4 |
The shape of the DataFrame shows there are 13 columns per sample (12 for the features and 1 for the target label). |
Let's split the data into training (85%) and test (15%) sets. |
train_splits = [] |
test_splits = [] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.