11# -*- coding: utf-8 -*-
22from PyQt4 import QtGui
3- from PyQt4 .QtGui import QLabel , QGridLayout
4- from PyQt4 .QtCore import Qt
53
64from Orange .data import Table
75from Orange .classification .random_forest import RandomForestLearner
@@ -29,72 +27,32 @@ class OWRandomForest(OWBaseLearner):
2927 index_output = settings .Setting (0 )
3028
3129 def add_main_layout (self ):
32- form = QGridLayout ()
33- basic_box = gui .widgetBox (
34- self .controlArea , "Basic Properties" , orientation = form )
35-
36- form .addWidget (QLabel (self .tr ("Number of trees: " )),
37- 0 , 0 , Qt .AlignLeft )
38- spin = gui .spin (basic_box , self , "n_estimators" , minv = 1 , maxv = 10000 ,
39- callback = self .settings_changed , addToLayout = False ,
40- controlWidth = 50 )
41- form .addWidget (spin , 0 , 1 , Qt .AlignRight )
42-
43- max_features_cb = gui .checkBox (
44- basic_box , self , "use_max_features" ,
45- callback = self .settings_changed , addToLayout = False ,
46- label = "Number of attributes considered at each split: " )
47-
48- max_features_spin = gui .spin (
49- basic_box , self , "max_features" , 2 , 50 , addToLayout = False ,
50- callback = self .settings_changed , controlWidth = 50 )
51-
52- form .addWidget (max_features_cb , 1 , 0 , Qt .AlignLeft )
53- form .addWidget (max_features_spin , 1 , 1 , Qt .AlignRight )
54-
55- random_state_cb = gui .checkBox (
56- basic_box , self , "use_random_state" , callback = self .settings_changed ,
57- addToLayout = False , label = "Fixed seed for random generator: " )
58- random_state_spin = gui .spin (
59- basic_box , self , "random_state" , 0 , 2 ** 31 - 1 , addToLayout = False ,
60- callback = self .settings_changed , controlWidth = 50 )
61-
62- form .addWidget (random_state_cb , 2 , 0 , Qt .AlignLeft )
63- form .addWidget (random_state_spin , 2 , 1 , Qt .AlignRight )
64- self ._max_features_spin = max_features_spin
65- self ._random_state_spin = random_state_spin
66-
67- # Growth control
68- form = QGridLayout ()
69- growth_box = gui .widgetBox (
70- self .controlArea , "Growth Control" , orientation = form )
71-
72- max_depth_cb = gui .checkBox (
73- growth_box , self , "use_max_depth" ,
30+ box = gui .vBox (self .controlArea , 'Basic Properties' )
31+ self .n_estimators_spin = gui .spin (
32+ box , self , "n_estimators" , minv = 1 , maxv = 10000 , controlWidth = 50 ,
33+ label = "Number of trees: " , callback = self .settings_changed )
34+ self .max_features_spin = gui .spin (
35+ box , self , "max_features" , 2 , 50 , controlWidth = 50 ,
36+ label = "Number of attributes considered at each split: " ,
37+ callback = self .settings_changed , checked = "use_max_features" ,
38+ checkCallback = self .settings_changed )
39+ self .random_state_spin = gui .spin (
40+ box , self , "random_state" , 0 , 2 ** 31 - 1 , controlWidth = 50 ,
41+ label = "Fixed seed for random generator: " ,
42+ callback = self .settings_changed , checked = "use_random_state" ,
43+ checkCallback = self .settings_changed )
44+
45+ box = gui .vBox (self .controlArea , "Growth Control" )
46+ self .max_depth_spin = gui .spin (
47+ box , self , "max_depth" , 1 , 50 , controlWidth = 50 ,
7448 label = "Limit depth of individual trees: " ,
75- callback = self .settings_changed ,
76- addToLayout = False )
77-
78- max_depth_spin = gui .spin (
79- growth_box , self , "max_depth" , 1 , 50 , addToLayout = False ,
80- callback = self .settings_changed )
81-
82- form .addWidget (max_depth_cb , 3 , 0 , Qt .AlignLeft )
83- form .addWidget (max_depth_spin , 3 , 1 , Qt .AlignRight )
84-
85- min_samples_split_cb = gui .checkBox (
86- growth_box , self , "use_min_samples_split" ,
49+ callback = self .settings_changed , checked = "use_max_depth" ,
50+ checkCallback = self .settings_changed )
51+ self .min_samples_split_spin = gui .spin (
52+ box , self , "min_samples_split" , 1 , 1000 , controlWidth = 50 ,
8753 label = "Do not split subsets smaller than: " ,
88- callback = self .settings_changed , addToLayout = False )
89-
90- min_samples_split_spin = gui .spin (
91- growth_box , self , "min_samples_split" , 1 , 1000 , addToLayout = False ,
92- callback = self .settings_changed )
93-
94- form .addWidget (min_samples_split_cb , 4 , 0 , Qt .AlignLeft )
95- form .addWidget (min_samples_split_spin , 4 , 1 , Qt .AlignRight )
96- self ._max_depth_spin = max_depth_spin
97- self ._min_samples_split_spin = min_samples_split_spin
54+ callback = self .settings_changed , checked = "use_min_samples_split" ,
55+ checkCallback = self .settings_changed )
9856
9957 # Index on the output
10058 # gui.doubleSpin(self.controlArea, self, "index_output", 0, 10000, 1,
@@ -113,12 +71,15 @@ def create_learner(self):
11371
11472 return self .LEARNER (preprocessors = self .preprocessors , ** common_args )
11573
116- def settings_changed (self ):
117- super ().settings_changed ()
118- self ._max_features_spin .setEnabled (self .use_max_features )
119- self ._random_state_spin .setEnabled (self .use_random_state )
120- self ._max_depth_spin .setEnabled (self .use_max_depth )
121- self ._min_samples_split_spin .setEnabled (self .use_min_samples_split )
74+ def check_data (self ):
75+ if super ().check_data ():
76+ n_features = len (self .data .domain .attributes )
77+ if self .use_max_features and self .max_features > n_features :
78+ self .error (self .DATA_ERROR_ID ,
79+ "Number of splitting attributes should "
80+ "be smaller than number of features." )
81+ self .valid_data = False
82+ return self .valid_data
12283
12384 def get_learner_parameters (self ):
12485 """Called by send report to list the parameters of the learner."""
0 commit comments