def scott_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
 sigma = np.std(data)
 dx = ((3.5 * sigma) / (n ** (1 / 3)))
 if return_bins:
  Nbins = np.ceil(((data.max() - data.min()) / dx))
  Nbins = max(1, Nbins)
  bins = (data.min() + (dx * np.arange((Nbins + 1))))
  return (dx, bins)
 else:
  return dx

def scott_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 sigma = np . std ( data ) dx = ( ( 3.5 * sigma ) / ( n ** ( 1 / 3 ) ) ) if return_bins : Nbins = np . ceil ( ( ( data . max ( ) - data . min ( ) ) / dx ) ) Nbins = max ( 1 , Nbins ) bins = ( data . min ( ) + ( dx * np . arange ( ( Nbins + 1 ) ) ) ) return ( dx , bins ) else : return dx
return the optimal histogram bin width using scotts rule scotts rule is a normal reference rule: it minimizes the integrated mean squared error in the bin approximation under the assumption that the data is approximately gaussian .