11import numpy as np
22
3- from Orange .data import Domain
3+ from Orange .data import Domain , ContinuousVariable
44from Orange .statistics import distribution
55from Orange .util import Reprable
66from .preprocess import Normalize
@@ -42,20 +42,27 @@ def normalize(self, dist, var):
4242 var = self .normalize_by_sd (dist , var )
4343 elif self .norm_type == Normalize .NormalizeBySpan :
4444 var = self .normalize_by_span (dist , var )
45- var .number_of_decimals = None
4645 return var
4746
48- def normalize_by_sd (self , dist , var ) :
47+ def normalize_by_sd (self , dist , var : ContinuousVariable ) -> ContinuousVariable :
4948 avg , sd = (dist .mean (), dist .standard_deviation ()) if dist .size else (0 , 1 )
5049 if sd == 0 :
5150 sd = 1
5251 if self .center :
5352 compute_val = Norm (var , avg , 1 / sd )
5453 else :
5554 compute_val = Norm (var , 0 , 1 / sd )
56- return var .copy (compute_value = compute_val )
5755
58- def normalize_by_span (self , dist , var ):
56+ # When dealing with integers, and multiplying by something smaller than
57+ # 1, the number of decimals should be decreased, but this integer will
58+ # likely turn into a float, which should have some default number of
59+ # decimals
60+ num_decimals = var .number_of_decimals + int (np .round (np .log10 (sd )))
61+ num_decimals = max (num_decimals , 1 ) # num decimals can't be negative
62+
63+ return var .copy (compute_value = compute_val , number_of_decimals = num_decimals )
64+
65+ def normalize_by_span (self , dist , var : ContinuousVariable ) -> ContinuousVariable :
5966 dma , dmi = (dist .max (), dist .min ()) if dist .shape [1 ] else (np .nan , np .nan )
6067 diff = dma - dmi
6168 if diff < 1e-15 :
@@ -64,4 +71,9 @@ def normalize_by_span(self, dist, var):
6471 compute_val = Norm (var , dmi , 1 / diff )
6572 else :
6673 compute_val = Norm (var , (dma + dmi ) / 2 , 2 / diff )
67- return var .copy (compute_value = compute_val )
74+ if not np .isnan (diff ):
75+ num_decimals = var .number_of_decimals + int (np .ceil (np .log10 (diff )))
76+ num_decimals = max (num_decimals , 0 ) # num decimals can't be negative
77+ return var .copy (compute_value = compute_val , number_of_decimals = num_decimals )
78+ else :
79+ return var .copy (compute_value = compute_val )
0 commit comments