def freedman_bin_width(data, return_bins=False):
 data = np.asarray(data)
 if (data.ndim != 1):
  raise ValueError(u'data should be one-dimensional')
 n = data.size
 if (n < 4):
  raise ValueError(u'data should have more than three entries')
 (v25, v75) = np.percentile(data, [25, 75])
 dx = ((2 * (v75 - v25)) / (n ** (1 / 3)))
 if return_bins:
  (dmin, dmax) = (data.min(), data.max())
  Nbins = max(1, np.ceil(((dmax - dmin) / dx)))
  bins = (dmin + (dx * np.arange((Nbins + 1))))
  return (dx, bins)
 else:
  return dx

def freedman_bin_width ( data , return_bins = False ) : data = np . asarray ( data ) if ( data . ndim != 1 ) : raise ValueError ( u'data should be one-dimensional' ) n = data . size if ( n < 4 ) : raise ValueError ( u'data should have more than three entries' ) ( v25 , v75 ) = np . percentile ( data , [ 25 , 75 ] ) dx = ( ( 2 * ( v75 - v25 ) ) / ( n ** ( 1 / 3 ) ) ) if return_bins : ( dmin , dmax ) = ( data . min ( ) , data . max ( ) ) Nbins = max ( 1 , np . ceil ( ( ( dmax - dmin ) / dx ) ) ) bins = ( dmin + ( dx * np . arange ( ( Nbins + 1 ) ) ) ) return ( dx , bins ) else : return dx
return the optimal histogram bin width using the freedman-diaconis rule the freedman-diaconis rule is a normal reference rule like scotts rule .