首页 > 解决方案 > 使用 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.

标签: pythonnumpymachine-learningdecision-tree

解决方案


推荐阅读