Changeset 11060:340b8bf1cbb4 in orange


Ignore:
Timestamp:
01/06/13 23:44:21 (16 months ago)
Author:
Yuval Greenfield <yuval@…>
Branch:
default
Message:

Avoid OverflowError when the beta is really high

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Orange/classification/logreg.py

    r10885 r11060  
    22from Orange.utils import deprecated_keywords, deprecated_members 
    33from Orange.data import preprocess 
     4import decimal 
    45import math 
    56 
     
    910from numpy.linalg import inv 
    1011from Orange.core import LogRegClassifier, LogRegFitter, LogRegFitter_Cholesky 
     12 
     13def format_decimal(x, prec=2): 
     14    """Allows arbitrary precision with scientific notation""" 
     15    tup = x.as_tuple() 
     16    digits = list(tup.digits[:prec + 1]) 
     17    sign = '-' if tup.sign else '' 
     18    dec = ''.join(str(i) for i in digits[1:]) 
     19    exp = x.adjusted() 
     20    return '{sign}{int}.{dec}e{exp}'.format(sign=sign, int=digits[0], dec=dec, exp=exp) 
    1121 
    1222def dump(classifier): 
     
    3444    formatstr = "%"+str(longest)+"s %10.2f %10.2f %10.2f %10.2f"     
    3545    out.append(formatstr % ("Intercept", classifier.beta[0], classifier.beta_se[0], classifier.wald_Z[0], classifier.P[0])) 
    36     formatstr = "%"+str(longest)+"s %10.2f %10.2f %10.2f %10.2f %10.2f"     
     46    formatstr = "%"+str(longest)+"s %10.2f %10.2f %10.2f %10.2f %s"     
    3747    for i in range(len(classifier.continuized_domain.features)): 
    38         out.append(formatstr % (classifier.continuized_domain.features[i].name, classifier.beta[i+1], classifier.beta_se[i+1], classifier.wald_Z[i+1], abs(classifier.P[i+1]), math.exp(classifier.beta[i+1]))) 
     48        exp = decimal.Decimal(math.e) ** decimal.Decimal(classifier.beta[i+1]) 
     49        out.append(formatstr % (classifier.continuized_domain.features[i].name, 
     50            classifier.beta[i+1], 
     51            classifier.beta_se[i+1], 
     52            classifier.wald_Z[i+1], 
     53            abs(classifier.P[i+1]), 
     54            format_decimal(exp))) 
    3955 
    4056    return '\n'.join(out) 
Note: See TracChangeset for help on using the changeset viewer.