Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

brs_test.py 1.9 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  1. import os
  2. import random
  3. import unittest
  4. from os.path import join as oj
  5. from random import sample
  6. from pandas.io.parsers import read_csv
  7. from imodels import *
  8. test_dir = os.path.dirname(os.path.abspath(__file__))
  9. class TestBRSClassifier(unittest.TestCase):
  10. def test_brs_tictactoe(self):
  11. '''Test classifiers are properly sklearn-compatible
  12. '''
  13. np.random.seed(13)
  14. random.seed(13)
  15. df = read_csv(oj(test_dir, 'test_data', 'tictactoe_X.txt'), header=0, sep=" ")
  16. Y = np.loadtxt(open(oj(test_dir, 'test_data', 'tictactoe_Y.txt'), "rb"), delimiter=" ")
  17. lenY = len(Y)
  18. idxs_train = sample(range(lenY), int(0.50 * lenY))
  19. idxs_test = [i for i in range(lenY) if i not in idxs_train]
  20. y_test = Y[idxs_test]
  21. model = BayesianRuleSetClassifier(n_rules=100,
  22. supp=5,
  23. maxlen=3,
  24. num_iterations=100,
  25. num_chains=2,
  26. alpha_pos=500, beta_pos=1,
  27. alpha_neg=500, beta_neg=1,
  28. alpha_l=None, beta_l=None,
  29. random_state=13)
  30. # fit and check accuracy
  31. model.fit(df.iloc[idxs_train], Y[idxs_train])
  32. y_pred = model.predict(df.iloc[idxs_test])
  33. acc1 = np.mean(y_pred == y_test)
  34. assert acc1 > 0.75
  35. # try fitting np version
  36. np.random.seed(13)
  37. random.seed(13)
  38. model.fit(df.iloc[idxs_train].values, Y[idxs_train])
  39. y_pred = model.predict(df.iloc[idxs_test].values)
  40. y_test = Y[idxs_test]
  41. acc2 = np.mean(y_pred == y_test)
  42. assert acc2 > 0.75
  43. # assert np.abs(acc1 - acc2) < 0.05 # todo: fix seeding
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...