use*_*144 43 python classification machine-learning data-mining scikit-learn
我正在使用Python中的scikit-learn开发一种分类算法来预测某些客户的性别.除此之外,我想使用朴素贝叶斯分类器,但我的问题是我有混合的分类数据(例如:"在线注册","接受电子邮件通知"等)和连续数据(例如:"年龄","长度")会员资格"等).我之前没有使用scikit,但我认为高斯朴素贝叶斯适用于连续数据,而伯努利朴素贝叶斯可用于分类数据.但是,由于我想在我的模型中同时拥有分类和连续数据,我真的不知道如何处理这个问题.任何想法将不胜感激!
ogr*_*sel 49
您至少有两个选择:
通过计算每个连续变量的百分位数,然后使用百分位数作为bin边界对连续变量进行分箱,将所有数据转换为分类表示.例如,对于一个人的身高,创建以下垃圾箱:"非常小","小","常规","大","非常大",确保每个垃圾箱包含大约20%的训练集人口.我们没有任何实用程序可以在scikit-learn中自动执行此操作,但它自己不应该太复杂.然后在数据的分类表示上放置一个唯一的多项式NB.
在数据的连续部分上独立地拟合高斯NB模型,在分类部分上独立地拟合多项NB模型.然后通过将类分配概率(使用predict_proba
方法)作为新特征来变换所有数据集:np.hstack((multinomial_probas, gaussian_probas))
然后在新特征上重新构造新模型(例如,新的高斯NB).
Rai*_*rim 12
Hope I'm not too late. I recently wrote a library called Mixed Naive Bayes, written in NumPy. It can assume a mix of Gaussian and categorical (multinoulli) distributions on the training data features.
https://github.com/remykarem/mixed-naive-bayes
The library is written such that the APIs are similar to scikit-learn's.
In the example below, let's assume that the first 2 features are from a categorical distribution and the last 2 are Gaussian. In the fit()
method, just specify categorical_features=[0,1]
, indicating that Columns 0 and 1 are to follow categorical distribution.
from mixed_naive_bayes import MixedNB
X = [[0, 0, 180.9, 75.0],
[1, 1, 165.2, 61.5],
[2, 1, 166.3, 60.3],
[1, 1, 173.0, 68.2],
[0, 2, 178.4, 71.0]]
y = [0, 0, 1, 1, 0]
clf = MixedNB(categorical_features=[0,1])
clf.fit(X,y)
clf.predict(X)
Run Code Online (Sandbox Code Playgroud)
Pip installable via pip install mixed-naive-bayes
. More information on the usage in the README.md file. Pull requests are greatly appreciated :)
简单的答案:乘以结果!! 一样的.
朴素贝叶斯基于应用贝叶斯定理和每对特征之间的"天真"独立假设 - 意味着你计算贝叶斯概率依赖于特定特征而不保持其他特征 - 这意味着算法将每个概率乘以一个特征与来自第二个特征的概率(我们完全忽略了分母 - 因为它只是一个归一化器).
所以正确的答案是: