我问维度明智等我试图用随机森林实现这项惊人的工作https://www.kaggle.com/allunia/how-to-attack-a-machine-learning-model/notebook

逻辑回归和随机森林来自sklearn,但是当我从随机森林模型获得权重时(784,),而逻辑回归返回(10,784)

我的大多数问题主要是维度和NaN,无穷大或者对于使用攻击方法的dtype错误而言太大的值 . 使用逻辑回归的权重是(10,784),但随机森林它(784,)可能是这导致问题?或者你可以建议对攻击方法进行一些修改吗?我尝试了Imputer for NaN值的错误,但它想让我重塑一下,所以我有了这个 . 我尝试将np.mat应用于我得到的维度错误,但它们没有用 .

def non_targeted_gradient(target, output, w):
    target = target.reshape(1, -1)
    output = output.reshape(1, -1)
    w = w.reshape(1,-1)
    target = imp.fit_transform(target)
    output = imp.fit_transform(output)
    w = imp.fit_transform(w)
    ww = calc_output_weighted_weights(output, w)
    for k in range(len(target)):
        if k == 0:
            gradient = np.mat((1-target[k])) * np.mat((w[k]-ww))
        else:
            gradient += np.mat((1-target[k])) * np.mat((w[k]-ww))
    return gradient

我可能做了很多错误,但 TL;DR 是我试图在上面的链接中应用随机森林而不是Logistic回归 .

Edit:

我为randomforestclassifier添加了一个包装器:

class RandomForestWrapper(RandomForestClassifier):
    def fit(self, *args, **kwargs):
        super(RandomForestClassifierWithCoef, self).fit(*args, **kwargs)
        self.coef_ = self.feature_importances_