Skip to content

Commit

Permalink
check_type_int for labels
Browse files Browse the repository at this point in the history
  • Loading branch information
szczepanskiNicolas committed Feb 21, 2024
1 parent 8dd5a43 commit e93f1c9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
23 changes: 23 additions & 0 deletions pyxai/examples/DT/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pyxai import Learning, Explainer

learner = Learning.Scikitlearn("pyxai/tests/dermatology.csv", learner_type=Learning.CLASSIFICATION)
model = learner.evaluate(method=Learning.HOLD_OUT, output=Learning.DT)
instance, prediction = learner.get_instances(model, n=1, correct=True, predictions=[0])

print("le:", learner.dict_labels)

explainer = Explainer.initialize(model, instance)
print("instance:", instance)
print("binary representation:", explainer.binary_representation)

sufficient_reason = explainer.sufficient_reason(n=1)
print("sufficient_reason:", sufficient_reason)
print("to_features:", explainer.to_features(sufficient_reason))

instance, prediction = learner.get_instances(model, n=1, correct=False)
explainer.set_instance(instance)
contrastive_reason = explainer.contrastive_reason()
print("contrastive reason", contrastive_reason)
print("to_features:", explainer.to_features(contrastive_reason, contrastive=True))

explainer.visualisation.screen(instance, contrastive_reason, contrastive=True)
20 changes: 15 additions & 5 deletions pyxai/sources/learning/learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,21 @@ def create_dict_labels(self, labels):
index = 0
self.dict_labels = OrderedDict()
self.inverse_dict_labels = OrderedDict()
for p in labels:
if str(p) not in self.dict_labels:
self.dict_labels[str(p)] = index
self.inverse_dict_labels[index] = str(p)
index += 1
set_labels = set(labels)
check_type_int = all(numpy.issubdtype(x, numpy.integer) or isinstance(x, int) or (isinstance(x, str) and x.isnumeric()) for x in set_labels)
if check_type_int is True:
for p in labels:
if str(p) not in self.dict_labels:
self.dict_labels[str(p)] = int(p)
self.inverse_dict_labels[int(p)] = str(p)
else:
for p in labels:
if str(p) not in self.dict_labels:
self.dict_labels[str(p)] = index
self.inverse_dict_labels[index] = str(p)
index += 1




"""
Expand Down

0 comments on commit e93f1c9

Please sign in to comment.