Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Naiven Baes so Kategoricki domeni
- import os
- os.environ['OPENBLAS_NUM_THREADS'] = '1'
- from sklearn.naive_bayes import CategoricalNB
- from sklearn.preprocessing import OrdinalEncoder
- import warnings
- warnings.filterwarnings("ignore")
- if __name__ == '__main__':
- dataset = [['C', 'S', 'O', '1', '2', '1', '1', '2', '1', '2', '0'],
- ['D', 'S', 'O', '1', '3', '1', '1', '2', '1', '2', '0'],
- ['C', 'S', 'O', '1', '3', '1', '1', '2', '1', '1', '0'],
- ['D', 'S', 'O', '1', '3', '1', '1', '2', '1', '2', '0'],
- ['D', 'A', 'O', '1', '3', '1', '1', '2', '1', '2', '0'],
- ['D', 'A', 'O', '1', '2', '1', '1', '2', '1', '2', '0']]
- encoder= OrdinalEncoder()
- encoder.fit([row[:-1] for row in dataset])
- train_set= dataset[:int(0.75*len(dataset))]
- train_set_x= [row[:-1] for row in train_set]
- train_set_y= [row[-1:] for row in train_set]
- train_set_x= encoder.transform(train_set_x)
- test_set= dataset[int(0.75*len(dataset)):]
- test_set_x= [row[:-1] for row in test_set]
- test_set_y= [row[-1:] for row in test_set]
- test_set_x = encoder.transform(test_set_x)
- classifier= CategoricalNB()
- classifier.fit(train_set_x,train_set_y)
- accuracy=0
- for i in range(len(test_set)):
- predicted_class= classifier.predict([test_set_x[i]])[0]
- real_class= test_set_y[i][0]
- if predicted_class == real_class:
- accuracy+=1
- print(accuracy/len(test_set_x))
- entry = [el for el in input().split(' ')]
- encoded_entry= encoder.transform([entry])
- print(classifier.predict(encoded_entry)[0])
- print(classifier.predict_proba(encoded_entry))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement