获取特征重要性 PySpark 朴素贝叶斯分类器

Get feature importance PySpark Naive Bayes classifier

我有一个使用 Pandas 数据框在 Python 中编写的朴素贝叶斯分类器,现在我在 PySpark 中需要它。我的问题是我需要每一列的特征重要性。在查看 PySpark ML 文档时,我找不到任何关于它的信息。 documentation

有谁知道我是否可以通过 Naive Bayes Spark MLlib 获得特征重要性?

使用Python的代码如下。使用 .coef_

检索特征重要性
df = df.fillna(0).toPandas()

X_df = df.drop(['NOT_OPEN', 'unique_id'], axis = 1)
X = X_df.values
Y = df['NOT_OPEN'].values.reshape(-1,1)

mnb = BernoulliNB(fit_prior=True) 
y_pred = mnb.fit(X, Y).predict(X)
estimator = mnb.fit(X, Y)


# coef_: For a binary classification problems this is the log of the estimated probability of a feature given the positive class. It means that higher values mean more important features for the positive class.

feature_names = X_df.columns
coefs_with_fns = sorted(zip(estimator.coef_[0], feature_names))

如果您对 coef_ 的等价物感兴趣,您正在寻找的 属性 是 NaiveBayesModel.theta

log of class conditional probabilities.

New in version 2.0.0.

model = ...  # type: NaiveBayesModel

model.theta.toArray()  # type: numpy.ndarray

生成的数组大小为 (number-of-classes, number-of-features),行对应于连续的标签。

估计差异可能更好
对数(P(feature_X|正))-对数(P(feature_X|负)) 作为特征重要性。

因为,我们对每个feature_X的Discriminative power感兴趣(sure-sure NB是一个生成模型)。 极端例子:一些 feature_X1 在所有 + 和 - 样本中具有相同的值,因此没有判别力。 所以,这个特征值的概率对于+和-样本都很高,但是对数概率的差异= 0.