python - 使用 Python 的 ML 决策树性能
问题描述
仅使用 numpy 作为库来编写决策树时,我发现自己处于运行时间变得过长的地步。为了给出上下文,为了测试代码,我使用了一个包含大约 300,000 个样本和 30 个特征的数据集。当我们决定定义节点的特征和相对分割值时,从m=sqrt(n)
特征开始,对于每个特征,我们针对该特征计算 gini 或其他标准,遍历数据集中存在的所有唯一值。但是,如果考虑下面的代码,如果我们得到 200.000 个唯一值,则运行时间太长。有没有办法克服这个问题?例如,迭代一组表示唯一值子集的值或更有效的计算方法。
def split(x, y, f, v):
x = x[:, f]
mask = x < v
return (x[mask], y[mask])
m = int(math.sqrt(x.shape[1]))
randomfeatures = numpy.random.choice(rangefeatures, m, replace=False)
for randomfeature in randomfeatures:
for value in x[:,randomfeature]:
s = split(x, y, randomfeature, value)
p = numpy.count_nonzero(s[1] == 1)/s[1].shape[0]
myCriteria(p) # Could be Gini or Entropy - just as an example.
解决方案
推荐阅读
- android - popBackStack() 在 android NavController 中不起作用
- flutter - 如何在小部件内调用提取的UserData
- javascript - 如何使用动态字段修补角度值?
- algorithm - 优化搜索算法以根据连续猜测缩小隐藏数字的可能性
- javascript - 如何使用动态字段修补角度值?
- javascript - 从 Django 查询集 json 数据中获取特定键的值并将它们传递给 vueJS
- jquery - 未捕获的类型错误:$ 不是保存按钮中的函数
- excel - 将数据从一个电子表格复制到另一个电子表格后如何在 VBA 中合并格式?
- reactjs - reactjs 获取元素的子元素数量
- java - springboot弹性搜索日期转换ConversionException