How can I use sklearn.naive_bayes with (multiple) categorical features?

前端 未结 2 1752
时光说笑
时光说笑 2021-01-31 19:04

I want to learn a Naive Bayes model for a problem where the class is boolean (takes on one of two values). Some of the features are boolean, but other features are categorical a

2条回答
  •  面向向阳花
    2021-01-31 19:25

    1. CategoricalNB by scikit-learn is a new class to be added in the naive_bayes module. It's in the nightly build here.

    2. Mixed Naive Bayes (https://github.com/remykarem/mixed-naive-bayes). It can assume a mix of Gaussian and categorical (multinoulli) distributions on the training data features. The library is written such that the APIs are similar to scikit-learn's.

    from mixed_naive_bayes import MixedNB
    X = [[0, 0],
         [1, 1],
         [2, 1],
         [1, 1],
         [0, 2]]
    y = [0, 0, 1, 1, 0]
    clf = MixedNB(categorical_features='all')
    clf.fit(X,y)
    clf.predict(X)
    

    See my response in a similar question here https://stackoverflow.com/a/58428035/4570466.

提交回复
热议问题