赞
踩
在本练习中,您将从头开始实施决策树,并将其应用于蘑菇可食用还是有毒的分类任务。
首先,让我们运行下面的单元格来导入此分配过程中所需的所有包。
import numpy as np
import matplotlib.pyplot as plt
from public_tests import *
%matplotlib inline
假设你正在创办一家种植和销售野生蘑菇的公司。
你能用这些数据来帮助你确定哪些蘑菇可以安全销售吗?
注:所使用的数据集仅用于说明目的。它并不意味着成为识别食用蘑菇的指南。
您将从加载此任务的数据集开始。您收集的数据集如下:
为了便于实现,我们对特性进行了热编码(将它们转换为0或1值特性)
因此
X_train = np.array([[1,1,1],[1,0,1],[1,0,0],[1,0,0],[1,1,1],[0,1,1],[0,0,0],[1,0,1],[0,1,0],[1,0,0]])
y_train = np.array([1,1,0,0,1,0,0,1,1,0])
查看变量
让我们更熟悉您的数据集。
下面的代码打印X_train的前几个元素和变量的类型。
print("First few elements of X_train:\n", X_train[:5])
print("Type of X_train:",type(X_train))
First few elements of X_train:
[[1 1 1]
[1 0 1]
[1 0 0]
[1 0 0]
[1 1 1]]
Type of X_train: <class 'numpy.ndarray'>
现在,让我们为y_train做同样的事情
print("First few elements of y_train:", y_train[:5])
print("Type of y_train:",type(y_train))
First few elements of y_train: [1 1 0 0 1]
Type of y_train: <class 'numpy.ndarray'>
检查变量的维度
熟悉数据的另一种有用方法是查看其维度。
请打印X_train和y_train的形状,并查看您的数据集中有多少训练示例。
print ('The shape of X_train is:', X_train.shape)
print ('The shape of y_train is: ', y_train.shape)
print ('Number of training examples (m):', len(X_train))
The shape of X_train is: (10, 3)
The shape of y_train is: (10,)
Number of training examples (m): 10
在这个实践实验室中,你将根据提供的数据集构建一个决策树。
首先,您将编写一个名为compute_entropy的辅助函数,用于计算节点处的熵(杂质的度量)。
完成下面的compute_entropy()函数以:
计算
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。