我需要计算 clf.predict 的 np.mean,但仅限于预测值百分比超过 80% 的行。
我当前的代码:
clf = DecisionTreeClassifier(random_state=1)
clf.fit(X, Y)
dropIndexes = []
for i in range(len(X)):
proba = clf.predict_proba ([X.values[i]])
if (proba[0][0] < 80 and proba[0][1] < 80):
dropIndexes.append(i)
# 删除预测值低于 80% 的所有行
X.drop(dropIndexes, inplace=True)
Y.drop(dropIndexes, inplace=True)
# 返回数组元素的平均值
print ("ERR:", np.mean(Y != clf.predict(X)))
有没有可能让这个代码运行得更快?
回答:
您的循环是多余的,因为 predict_proba
可以处理矩阵。您可以用以下代码替换它:
prd = clf.predict_proba(X)
dropIndexes = (prd[:, 0] < 0.8) & (prd[:, 1] < 0.8)