Ignore:
Timestamp:
03/19/12 20:03:27 (2 years ago)
Author:
Lan Zagar <lan.zagar@…>
Branch:
default
Message:

Some changes in least squares regression.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Orange/regression/linear.py

    r10557 r10575  
    244244        domain = table.domain 
    245245         
    246         if numpy.std(y) < 10e-6: # almost constant variable 
    247             return Orange.regression.mean.MeanLearner(table) 
    248       
    249         # set weights to the instances 
    250         W = numpy.identity(n) 
    251246        if weight: 
    252             for i, ins in enumerate(table): 
    253                 W[i, i] = float(ins[weight]) 
    254  
    255         compute_stats = self.compute_stats 
     247            weights = numpy.sqrt([float(ins[weight]) for ins in table]) 
     248            weights = weights.reshape(y.shape) 
     249            X = weights * X 
     250            y = weights * y 
     251         
     252        cov = dot(X.T, X) 
     253         
     254        if self.ridge_lambda: 
     255            cov += self.ridge_lambda * numpy.eye(m + self.intercept) 
     256 
    256257        # adds some robustness by computing the pseudo inverse; 
    257         # normal inverse could fail due to singularity of the X.T * W * X 
    258         if self.ridge_lambda is None: 
    259             cov = pinv(dot(dot(X.T, W), X)) 
    260         else: 
    261             cov = pinv(dot(dot(X.T, W), X) +  
    262                        self.ridge_lambda * numpy.eye(m + self.intercept)) 
    263             # TODO: find inferential properties of the estimators 
    264             compute_stats = False  
    265         D = dot(dot(cov, X.T), W) 
     258        # normal inverse could fail due to the singularity of X.T * X 
     259        invcov = pinv(cov) 
     260        D = dot(invcov, X.T) 
    266261        coefficients = dot(D, y) 
    267262 
     
    275270            std_coefficients = None 
    276271 
    277         if compute_stats is False: 
     272        # TODO: find inferential properties of the estimators for ridge 
     273        if self.compute_stats is False or self.ridge_lambda: 
    278274            return LinearRegression(domain.class_var, domain, 
    279275                coefficients=coefficients, std_coefficients=std_coefficients, 
     
    289285        sst = numpy.sum((y - mu_y) ** 2) 
    290286        # sum of squares due to regression (explained variance) 
    291         ssr = numpy.sum((fitted - mu_y)**2) 
     287        ssr = numpy.sum((fitted - mu_y) ** 2) 
    292288        # error sum of squares (unexplaied variance) 
    293289        sse = sst - ssr 
Note: See TracChangeset for help on using the changeset viewer.