<h3 dir="auto">Describe the issue:</h3>
<p dir="auto">Something is wrong with the "auto" option.</p>
<h3 dir="auto">Reproduce the code example:</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np

hist, bin_edges = np.histogram(
    np.array(
        [
            -4.24264069e00,
            -5.55111512e-17,
            3.81837662e01,
            1.77635684e-15,
            -5.65685425e00,
            3.55271368e-15,
            -1.97989899e01,
            0.00000000e00,
            -1.27279221e01,
            4.24264069e00,
            1.41421356e01,
            -1.83847763e01,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            -5.55111512e-17,
            3.53553391e-01,
            9.54594155e00,
            -3.53553391e-01,
            -9.19238816e00,
            3.53553391e-01,
            -3.53553391e-01,
            2.82842712e00,
            -7.21644966e-16,
            -3.18198052e00,
            -5.55111512e-17,
            -5.55111512e-17,
        ]
    ),
    bins=&quot;auto&quot;,
    density=True,
)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-s1">hist</span>, <span class="pl-s1">bin_edges</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">histogram</span>(
    <span class="pl-s1">np</span>.<span class="pl-en">array</span>(
        [
            <span class="pl-c1">-</span><span class="pl-c1">4.24264069e00</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">3.81837662e01</span>,
            <span class="pl-c1">1.77635684e-15</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.65685425e00</span>,
            <span class="pl-c1">3.55271368e-15</span>,
            <span class="pl-c1">-</span><span class="pl-c1">1.97989899e01</span>,
            <span class="pl-c1">0.00000000e00</span>,
            <span class="pl-c1">-</span><span class="pl-c1">1.27279221e01</span>,
            <span class="pl-c1">4.24264069e00</span>,
            <span class="pl-c1">1.41421356e01</span>,
            <span class="pl-c1">-</span><span class="pl-c1">1.83847763e01</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">3.53553391e-01</span>,
            <span class="pl-c1">9.54594155e00</span>,
            <span class="pl-c1">-</span><span class="pl-c1">3.53553391e-01</span>,
            <span class="pl-c1">-</span><span class="pl-c1">9.19238816e00</span>,
            <span class="pl-c1">3.53553391e-01</span>,
            <span class="pl-c1">-</span><span class="pl-c1">3.53553391e-01</span>,
            <span class="pl-c1">2.82842712e00</span>,
            <span class="pl-c1">-</span><span class="pl-c1">7.21644966e-16</span>,
            <span class="pl-c1">-</span><span class="pl-c1">3.18198052e00</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
            <span class="pl-c1">-</span><span class="pl-c1">5.55111512e-17</span>,
        ]
    ),
    <span class="pl-s1">bins</span><span class="pl-c1">=</span><span class="pl-s">"auto"</span>,
    <span class="pl-s1">density</span><span class="pl-c1">=</span><span class="pl-c1">True</span>,
)</pre></div>
<h3 dir="auto">Error message:</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="~\AppData\Roaming\Python\Python39\site-packages\numpy\core\function_base.py in linspace(start, stop, num, endpoint, retstep, dtype, axis)
    137 
    138     delta = stop - start
--&gt; 139     y = _nx.arange(0, num, dtype=dt).reshape((-1,) + (1,) * ndim(delta))
    140     # In-place multiplication y *= delta/div is faster, but prevents the multiplicant
    141     # from overriding what class is produced, and thus prevents, e.g. use of Quantities,

MemoryError: Unable to allocate 3.81 EiB for an array with shape (549572839412124544,) and data type float64"><pre class="notranslate"><span class="pl-k">~</span><span class="pl-cce">\A</span>ppData<span class="pl-cce">\R</span>oaming<span class="pl-cce">\P</span>ython<span class="pl-cce">\P</span>ython39<span class="pl-cce">\s</span>ite-packages<span class="pl-cce">\n</span>umpy<span class="pl-cce">\c</span>ore<span class="pl-cce">\f</span>unction_base.py <span class="pl-k">in</span> linspace(start, stop, num, endpoint, retstep, dtype, axis)
    137 
    138     delta = stop - start
--<span class="pl-k">&gt;</span> 139     y = _nx.arange(0, num, dtype=dt).reshape<span class="pl-s"><span class="pl-pds">((</span><span class="pl-k">-</span><span class="pl-c1">1</span><span class="pl-k">,</span>) <span class="pl-k">+</span> (<span class="pl-c1">1</span><span class="pl-k">,</span>) <span class="pl-k">*</span> ndim(delta<span class="pl-pds">))</span></span>
    140     <span class="pl-c"><span class="pl-c">#</span> In-place multiplication y *= delta/div is faster, but prevents the multiplicant</span>
    141     <span class="pl-c"><span class="pl-c">#</span> from overriding what class is produced, and thus prevents, e.g. use of Quantities,</span>

MemoryError: Unable to allocate 3.81 EiB <span class="pl-k">for</span> an array with shape (549572839412124544,) and data <span class="pl-c1">type</span> float64</pre></div>
<h3 dir="auto">Runtime information:</h3>
<p dir="auto">1.24.2<br>
3.9.13 (main, Aug 25 2022, 23:51:50) [MSC v.1916 64 bit (AMD64)]</p>
<h3 dir="auto">Context for the issue:</h3>
<p dir="auto">A lot of people use numpy for calculation histograms, I think. bins="auto" is very convenient as it eliminates the need to try multiple times with different bins sizes, therefore, it would be good to fix this.</p>
<p dir="auto">I use Numpy version 1.15. If I call <code class="notranslate">np.histogram(data, bins='auto')</code>, I get <code class="notranslate">MemoryError</code>. The error is probably caused by trying to allocate too large <a href="https://github.com/numpy/numpy/blob/058851c5cfc98f50f11237b1c13d77cfd1f40475/numpy/lib/histograms.py#L375">linspace</a>.</p>
<h3 dir="auto">Reproducing code example:</h3>
<p dir="auto">The error can be reproduced on <a href="https://github.com/numpy/numpy/files/2348354/data.txt">this data</a>. Its range is approximately (-0.00283, 0.00209), with many values being close to zero. The following code raises the error:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy
data = numpy.loadtxt('data.txt')
numpy.histogram(data, bins='auto')"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span>
<span class="pl-s1">data</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">loadtxt</span>(<span class="pl-s">'data.txt'</span>)
<span class="pl-s1">numpy</span>.<span class="pl-en">histogram</span>(<span class="pl-s1">data</span>, <span class="pl-s1">bins</span><span class="pl-c1">=</span><span class="pl-s">'auto'</span>)</pre></div>
<p dir="auto">The problem is in computation of the bin edges. The following code raises the error:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy.lib.histograms
numpy.lib.histograms._get_bin_edges(data, bins='auto', range=None, weights=None)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span>.<span class="pl-s1">lib</span>.<span class="pl-s1">histograms</span>
<span class="pl-s1">numpy</span>.<span class="pl-s1">lib</span>.<span class="pl-s1">histograms</span>.<span class="pl-en">_get_bin_edges</span>(<span class="pl-s1">data</span>, <span class="pl-s1">bins</span><span class="pl-c1">=</span><span class="pl-s">'auto'</span>, <span class="pl-s1">range</span><span class="pl-c1">=</span><span class="pl-c1">None</span>, <span class="pl-s1">weights</span><span class="pl-c1">=</span><span class="pl-c1">None</span>)</pre></div>
<p dir="auto">Even more specifically, the problem is caused by the method <code class="notranslate">bins='fd'</code>. The other method <code class="notranslate">bins='sturges'</code> used for automated computation of bin edges works well.</p>
<h3 dir="auto">Error message:</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;.../.pycharm_helpers/pydev/_pydevd_bundle/pydevd_exec2.py&quot;, line 3, in Exec
    exec(exp, global_vars, local_vars)
  File &quot;&lt;input&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;.../numpy/lib/histograms.py&quot;, line 676, in histogram
    bin_edges, uniform_bins = _get_bin_edges(a, bins, range, weights)
  File &quot;.../numpy/lib/histograms.py&quot;, line 351, in _get_bin_edges
    endpoint=True, dtype=bin_type)
  File &quot;.../numpy/core/function_base.py&quot;, line 115, in linspace
    y = _nx.arange(0, num, dtype=dt)
MemoryError"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File ".../.pycharm_helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "&lt;input&gt;", line 1, in &lt;module&gt;
  File ".../numpy/lib/histograms.py", line 676, in histogram
    bin_edges, uniform_bins = _get_bin_edges(a, bins, range, weights)
  File ".../numpy/lib/histograms.py", line 351, in _get_bin_edges
    endpoint=True, dtype=bin_type)
  File ".../numpy/core/function_base.py", line 115, in linspace
    y = _nx.arange(0, num, dtype=dt)
MemoryError
</code></pre></div>
<h3 dir="auto">Numpy/Python version information:</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="1.15.0 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0]"><pre class="notranslate"><code class="notranslate">1.15.0 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0]
</code></pre></div>