1 | # Description: Shows how to sample example by random divisions into two groups |
---|

2 | # Category: sampling |
---|

3 | # Classes: SubsetIndices2, RandomGenerator |
---|

4 | # Uses: lenses |
---|

5 | # Referenced: RandomIndices.htm |
---|

6 | |
---|

7 | import Orange |
---|

8 | |
---|

9 | lenses = Orange.data.Table("lenses") |
---|

10 | |
---|

11 | indices2 = Orange.data.sample.SubsetIndices2(p0=6) |
---|

12 | |
---|

13 | ind = indices2(lenses) |
---|

14 | print ind |
---|

15 | lenses0 = lenses.select(ind, 0) |
---|

16 | lenses1 = lenses.select(ind, 1) |
---|

17 | print len(lenses0), len(lenses1) |
---|

18 | |
---|

19 | print "\nIndices without playing with random generator" |
---|

20 | for i in range(5): |
---|

21 | print indices2(lenses) |
---|

22 | |
---|

23 | print "\nIndices with random generator" |
---|

24 | indices2.random_generator = Orange.misc.Random(42) |
---|

25 | for i in range(5): |
---|

26 | print indices2(lenses) |
---|

27 | |
---|

28 | print "\nIndices with randseed" |
---|

29 | indices2.random_generator = None |
---|

30 | indices2.randseed = 42 |
---|

31 | for i in range(5): |
---|

32 | print indices2(lenses) |
---|

33 | |
---|

34 | |
---|

35 | print "\nIndices with p0 set as probability (not 'a number of')" |
---|

36 | indices2.p0 = 0.25 |
---|

37 | print indices2(lenses) |
---|

38 | |
---|

39 | print "\n... with stratification" |
---|

40 | indices2.stratified = indices2.Stratified |
---|

41 | ind = indices2(lenses) |
---|

42 | print ind |
---|

43 | lenses2 = lenses.select(ind) |
---|

44 | od = Orange.core.getClassDistribution(lenses) |
---|

45 | sd = Orange.core.getClassDistribution(lenses2) |
---|

46 | od.normalize() |
---|

47 | sd.normalize() |
---|

48 | print od |
---|

49 | print sd |
---|

50 | |
---|

51 | print "\n... and without stratification" |
---|

52 | indices2.stratified = indices2.NotStratified |
---|

53 | print indices2(lenses) |
---|

54 | ind = indices2(lenses) |
---|

55 | print ind |
---|

56 | lenses2 = lenses.select(ind) |
---|

57 | od = Orange.core.getClassDistribution(lenses) |
---|

58 | sd = Orange.core.getClassDistribution(lenses2) |
---|

59 | od.normalize() |
---|

60 | sd.normalize() |
---|

61 | print od |
---|

62 | print sd |
---|

63 | |
---|

64 | print "\n... stratified 'if possible'" |
---|

65 | indices2.stratified = indices2.StratifiedIfPossible |
---|

66 | print indices2(lenses) |
---|

67 | |
---|

68 | print "\n... stratified 'if possible', after removing the first example's class" |
---|

69 | lenses[0].setclass("?") |
---|

70 | print indices2(lenses) |
---|