Skip to content

Instantly share code, notes, and snippets.

@Bogatinov
Created June 13, 2020 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bogatinov/87f70d7570c4cb0e65f4a5c644da3012 to your computer and use it in GitHub Desktop.
Save Bogatinov/87f70d7570c4cb0e65f4a5c644da3012 to your computer and use it in GitHub Desktop.
from sklearn.naive_bayes import CategoricalNB
from sklearn.preprocessing import OrdinalEncoder
import math
if __name__ == '__main__':
dataset = [
['high', 'light', 'hard', 'dab'],
['high', 'light', 'hard', 'bor'],
['high', 'light', 'hard', 'dab'],
['high', 'light', 'soft', 'bor'],
['high', 'dark', 'hard', 'dab'],
['high', 'dark', 'soft', 'bor'],
['low', 'dark', 'soft', 'dab'],
['high', 'light', 'soft', 'bor'],
['low', 'dark', 'hard', 'bor'],
['low', 'dark', 'hard', 'dab']
]
encoder = OrdinalEncoder()
encoder.fit([dataset[i][:-1] for i in range(0, len(dataset))])
upperThreshold = math.ceil(0.67 * len(dataset))
train_set = dataset[0:upperThreshold]
test_set = dataset[upperThreshold:]
X = encoder.transform([train_set[i][:-1] for i in range(0, len(train_set))])
Y = [train_set[i][-1] for i in range(0, len(train_set))]
clf = CategoricalNB(alpha=2.0)
clf.fit(X, Y)
test_set_x = encoder.transform([test_set[i][:-1] for i in range(0, len(test_set))])
accuracy = 0
for i in range(0,len(test_set)):
predict = clf.predict([test_set_x[i]])
if predict[0] == test_set[i][-1]:
accuracy += 1
print(accuracy / len(test_set))
entry = encoder.transform([['low', 'light', 'hard']])
print(clf.predict(entry))
print(clf.predict_proba(entry))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment