Commits

Anonymous committed 340b8bf

Avoid OverflowError when the beta is really high

Comments (0)

Files changed (1)

Orange/classification/logreg.py

 import Orange
 from Orange.utils import deprecated_keywords, deprecated_members
 from Orange.data import preprocess
+import decimal
 import math
 
 
 from numpy.linalg import inv
 from Orange.core import LogRegClassifier, LogRegFitter, LogRegFitter_Cholesky
 
+def format_decimal(x, prec=2):
+    """Allows arbitrary precision with scientific notation"""
+    tup = x.as_tuple()
+    digits = list(tup.digits[:prec + 1])
+    sign = '-' if tup.sign else ''
+    dec = ''.join(str(i) for i in digits[1:])
+    exp = x.adjusted()
+    return '{sign}{int}.{dec}e{exp}'.format(sign=sign, int=digits[0], dec=dec, exp=exp)
+
 def dump(classifier):
     """ Return a formatted string describing the logistic regression model
 
     out.append('')
     formatstr = "%"+str(longest)+"s %10.2f %10.2f %10.2f %10.2f"    
     out.append(formatstr % ("Intercept", classifier.beta[0], classifier.beta_se[0], classifier.wald_Z[0], classifier.P[0]))
-    formatstr = "%"+str(longest)+"s %10.2f %10.2f %10.2f %10.2f %10.2f"    
+    formatstr = "%"+str(longest)+"s %10.2f %10.2f %10.2f %10.2f %s"    
     for i in range(len(classifier.continuized_domain.features)):
-        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])))
+        exp = decimal.Decimal(math.e) ** decimal.Decimal(classifier.beta[i+1])
+        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]),
+            format_decimal(exp)))
 
     return '\n'.join(out)
         
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.