Changeset 8217:673dd4e6d0a0 in orange
 Timestamp:
 08/18/11 12:21:17 (3 years ago)
 Branch:
 default
 Convert:
 194318f9be5769745e6688fee972dc00ea55a830
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

orange/Orange/regression/earth.py
r8201 r8217 98 98 values is grater then 2), and a multi response model is learned on these 99 99 new columns. The resulting classifier will then use the computed response 100 values on new examples to select the final predicted class.100 values on new instances to select the final predicted class. 101 101 102 102 """ 103 def __new__(cls, examples=None, weight_id=None, **kwargs):103 def __new__(cls, instances=None, weight_id=None, **kwargs): 104 104 self = Orange.core.LearnerFD.__new__(cls) 105 if examples is not None:105 if instances is not None: 106 106 self.__init__(**kwargs) 107 return self.__call__( examples, weight_id)107 return self.__call__(instances, weight_id) 108 108 else: 109 109 return self … … 111 111 def __init__(self, degree=1, terms=21, penalty= None, thresh=1e3, 112 112 min_span=0, new_var_penalty=0, fast_k=20, fast_beta=1, 113 pruned_terms=None, scale_resp=True, store_ examples=True,113 pruned_terms=None, scale_resp=True, store_instances=True, 114 114 multi_label=False, **kwds): 115 115 """ Initialize the learner instance. … … 139 139 True  ignored for multi response models). 140 140 :type scale_resp: bool 141 :param store_ examples: Store training examples in the model141 :param store_instances: Store training instances in the model 142 142 (default True). 143 :type store_ examples: bool143 :type store_instances: bool 144 144 :param multi_label: If True build a multi label model (default False). 145 145 :type multi_label: bool … … 161 161 self.pruned_terms = pruned_terms 162 162 self.scale_resp = scale_resp 163 self.store_ examples = store_examples163 self.store_instances = store_instances 164 164 self.multi_label = multi_label 165 165 self.__dict__.update(kwds) … … 171 171 [impute, cont]) 172 172 173 def __call__(self, examples, weight_id=None):174 original_domain = examples.domain # Needed by classifier and evimp175 examples = self.preproc(examples)173 def __call__(self, instances, weight_id=None): 174 original_domain = instances.domain # Needed by classifier and evimp 175 instances = self.preproc(instances) 176 176 expanded_class = None 177 177 if self.multi_label: 178 label_mask = data_label_mask( examples.domain)179 data = examples.to_numpy_MA("Ac")[0]178 label_mask = data_label_mask(instances.domain) 179 data = instances.to_numpy_MA("Ac")[0] 180 180 y = data[:, label_mask] 181 181 x = data[:, ~ label_mask] 182 182 else: 183 183 # Expand a discrete class with indicator columns 184 if is_discrete( examples.domain.class_var):185 expanded_class = expand_discrete( examples.domain.class_var)186 y = Table(Domain(expanded_class, None), examples)184 if is_discrete(instances.domain.class_var): 185 expanded_class = expand_discrete(instances.domain.class_var) 186 y = Table(Domain(expanded_class, None), instances) 187 187 y = y.to_numpy_MA("A")[0] 188 x = examples.to_numpy_MA("A")[0]188 x = instances.to_numpy_MA("A")[0] 189 189 label_mask = [False] * x.shape[1] + [True] * y.shape[1] 190 190 label_mask = numpy.array(label_mask) 191 elif is_continuous( examples.domain.class_var):192 label_mask = numpy.zeros(len( examples.domain.variables),191 elif is_continuous(instances.domain.class_var): 192 label_mask = numpy.zeros(len(instances.domain.variables), 193 193 dtype=bool) 194 194 label_mask[1] = True 195 x, y, _ = examples.to_numpy_MA()195 x, y, _ = instances.to_numpy_MA() 196 196 y = y.reshape((1, 1)) 197 197 else: … … 223 223 betas, res, rank, s = numpy.linalg.lstsq(bx_used, y) 224 224 225 return EarthClassifier( examples.domain, used, dirs, cuts, betas.T,225 return EarthClassifier(instances.domain, used, dirs, cuts, betas.T, 226 226 subsets, rss_per_subset, gcv_per_subset, 227 examples=examples if self.store_examples else None,227 instances=instances if self.store_instances else None, 228 228 label_mask=label_mask, multi_flag=self.multi_label, 229 229 expanded_class=expanded_class) … … 234 234 """ 235 235 def __init__(self, domain, best_set, dirs, cuts, betas, subsets=None, 236 rss_per_subset=None, gcv_per_subset=None, examples=None,236 rss_per_subset=None, gcv_per_subset=None, instances=None, 237 237 label_mask=None, multi_flag=False, expanded_class=None, 238 238 original_domain=None, **kwargs): … … 247 247 self.rss_per_subset = rss_per_subset 248 248 self.gcv_per_subset = gcv_per_subset 249 self. examples = examples249 self.instances = instances 250 250 self.label_mask = label_mask 251 251 self.expanded_class = expanded_class … … 253 253 self.__dict__.update(kwargs) 254 254 255 def __call__(self, example, result_type=Orange.core.GetValue):255 def __call__(self, instance, result_type=Orange.core.GetValue): 256 256 if self.multi_flag: 257 257 resp_vars = [v for v, m in zip(self.domain.variables, … … 262 262 resp_vars = [self.class_var] 263 263 264 vals = self.predict( example)264 vals = self.predict(instance) 265 265 vals = [var(val) for var, val in zip(resp_vars, vals)] 266 266 … … 301 301 print self.format_model(percision, indent) 302 302 303 def base_matrix(self, examples=None):303 def base_matrix(self, instances=None): 304 304 """Return the base matrix (bx) of the Earth model for the table. 305 If table is not supplied the base matrix of the training examples305 If table is not supplied the base matrix of the training instances 306 306 is returned. 307 Base matrix is a len( examples) x num_terms matrix of computed values308 of terms in the model (not multiplied by beta) for each example.309 310 :param examples: Input examples for the base matrix.311 :type examples: :class:`Orange.data.Table`307 Base matrix is a len(instances) x num_terms matrix of computed values 308 of terms in the model (not multiplied by beta) for each instance. 309 310 :param instances: Input instances for the base matrix. 311 :type instances: :class:`Orange.data.Table` 312 312 313 313 """ 314 if examples is None:315 examples = self.examples316 (data,) = examples.to_numpy_MA("Ac")314 if instances is None: 315 instances = self.instances 316 (data,) = instances.to_numpy_MA("Ac") 317 317 data = data[:, ~ self.label_mask] 318 318 bx = base_matrix(data, self.best_set, self.dirs, self.cuts) 319 319 return bx 320 320 321 def predict(self, example):322 """ Predict the response values for the example323 324 :param example: exampleinstance325 :type example: :class:`Orange.data.Example`321 def predict(self, instance): 322 """ Predict the response values for the instance 323 324 :param instance: Data instance 325 :type instance: :class:`Orange.data.Instance` 326 326 327 327 """ 328 data = Orange.data.Table(self.domain, [ example])328 data = Orange.data.Table(self.domain, [instance]) 329 329 bx = self.base_matrix(data) 330 330 bx_used = bx[:, self.best_set] … … 421 421 422 422 :param rss: Residual sum of squares. 423 :param n: Number of training examples.423 :param n: Number of training instances. 424 424 :param n_effective_params: Number of effective paramaters. 425 425
Note: See TracChangeset
for help on using the changeset viewer.