Advertisement
dzocesrce

[VI] Solar Beam

Jan 24th, 2025
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. #Naiven Baes so Kategoricki domeni
  2. import os
  3. os.environ['OPENBLAS_NUM_THREADS'] = '1'
  4. from sklearn.naive_bayes import CategoricalNB
  5. from sklearn.preprocessing import OrdinalEncoder
  6. import warnings
  7. warnings.filterwarnings("ignore")
  8.  
  9. if __name__ == '__main__':
  10.     dataset = [['C', 'S', 'O', '1', '2', '1', '1', '2', '1', '2', '0'],
  11.                ['D', 'S', 'O', '1', '3', '1', '1', '2', '1', '2', '0'],
  12.                ['C', 'S', 'O', '1', '3', '1', '1', '2', '1', '1', '0'],
  13.                ['D', 'S', 'O', '1', '3', '1', '1', '2', '1', '2', '0'],
  14.                ['D', 'A', 'O', '1', '3', '1', '1', '2', '1', '2', '0'],
  15.                ['D', 'A', 'O', '1', '2', '1', '1', '2', '1', '2', '0']]
  16.  
  17.     encoder= OrdinalEncoder()
  18.     encoder.fit([row[:-1] for row in dataset])
  19.  
  20.     train_set= dataset[:int(0.75*len(dataset))]
  21.     train_set_x= [row[:-1] for row in train_set]
  22.     train_set_y= [row[-1:] for row in train_set]
  23.     train_set_x= encoder.transform(train_set_x)
  24.  
  25.     test_set= dataset[int(0.75*len(dataset)):]
  26.     test_set_x= [row[:-1] for row in test_set]
  27.     test_set_y= [row[-1:] for row in test_set]
  28.     test_set_x = encoder.transform(test_set_x)
  29.  
  30.     classifier= CategoricalNB()
  31.     classifier.fit(train_set_x,train_set_y)
  32.  
  33.     accuracy=0
  34.     for i in range(len(test_set)):
  35.         predicted_class= classifier.predict([test_set_x[i]])[0]
  36.         real_class= test_set_y[i][0]
  37.         if predicted_class == real_class:
  38.             accuracy+=1
  39.  
  40.     print(accuracy/len(test_set_x))
  41.  
  42.     entry = [el for el in input().split(' ')]
  43.     encoded_entry= encoder.transform([entry])
  44.  
  45.     print(classifier.predict(encoded_entry)[0])
  46.     print(classifier.predict_proba(encoded_entry))
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement