Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OWNomogram: Add a new widget #1936

Merged
merged 4 commits into from
Jan 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Orange/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def __call__(self, data):

if isinstance(data, Instance):
data = Table(data.domain, [data])
origdata = data
data = self.preprocess(data)

if len(data.domain.class_vars) > 1 and not self.supports_multiclass:
Expand All @@ -123,6 +124,7 @@ def __call__(self, data):
model.supports_multiclass = self.supports_multiclass
model.name = self.name
model.original_domain = origdomain
model.original_data = origdata
return model

def preprocess(self, data):
Expand Down
31 changes: 17 additions & 14 deletions Orange/classification/naive_bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from Orange.classification import Learner, Model
from Orange.data import Instance, Storage
from Orange.statistics import contingency
from Orange.preprocess import Discretize
from Orange.preprocess import Discretize, RemoveNaNColumns

__all__ = ["NaiveBayesLearner"]

Expand All @@ -19,7 +19,8 @@ class NaiveBayesLearner(Learner):
An ordered list of preprocessors applied to data before training
or testing.
"""
preprocessors = [Discretize()]
preprocessors = [RemoveNaNColumns(), Discretize()]
name = 'naive bayes'

def fit_storage(self, table):
if not isinstance(table, Storage):
Expand All @@ -31,33 +32,35 @@ def fit_storage(self, table):
cont = contingency.get_contingencies(table)
class_freq = np.array(np.diag(
contingency.get_contingency(table, table.domain.class_var)))
return NaiveBayesModel(cont, class_freq, table.domain)
class_prob = (class_freq + 1) / (np.sum(class_freq) + len(class_freq))
log_cont_prob = [np.log(
(np.array(c) + 1) / (np.sum(np.array(c), axis=0)[None, :] +
c.shape[0]) / class_prob[:, None])
for c in cont]
return NaiveBayesModel(log_cont_prob, class_prob, table.domain)


class NaiveBayesModel(Model):
def __init__(self, cont, class_freq, domain):
def __init__(self, log_cont_prob, class_prob, domain):
super().__init__(domain)
self.cont = cont
self.class_freq = class_freq
self.log_cont_prob = log_cont_prob
self.class_prob = class_prob

def predict_storage(self, data):
if isinstance(data, Instance):
data = [data]
n_cls = len(self.class_freq)
class_prob = (self.class_freq + 1) / (np.sum(self.class_freq) + n_cls)
if len(data.domain.attributes) == 0:
probs = np.tile(class_prob, (len(data), 1))
probs = np.tile(self.class_prob, (len(data), 1))
else:
log_cont_prob = [np.log(np.divide(np.array(c) + 1,
self.class_freq.reshape((n_cls, 1)) +
c.shape[1])) for c in self.cont]
probs = np.exp(np.array([np.sum(attr_prob[:, int(attr_val)]
for attr_val, attr_prob
in zip(ins, log_cont_prob)
in zip(ins, self.log_cont_prob)
if not np.isnan(attr_val))
for ins in data]) + np.log(class_prob))
for ins in data]) + np.log(
self.class_prob))
probs /= probs.sum(axis=1)[:, None]
values = probs.argmax(axis=1)
return values, probs


NaiveBayesLearner.__returns__ = NaiveBayesModel
16 changes: 16 additions & 0 deletions Orange/widgets/visualize/icons/Nomogram.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading