<h4 dir="auto">Description</h4>

<p dir="auto">I try to 30 fit times a Random Forest Regressor (sklearn.ensemble.RandomForestRegressor) on different sets of data.<br>
In order to do it I use a for loop. But every-times, I run my script the python kernel died unexpectedly.<br>
I run this script on different machines, the power of the machine (RAM and CPU) only delays the moment when the kernel die.<br>
I write a minimal case of my script without my personal data and of the other thinks I want to do normally. In my complete and original script in which I read the data in a csv file and write the prediction in an other csv file, the kernel die even quickly.</p>
<h4 dir="auto">Steps/Code to Reproduce</h4>


<p dir="auto">Example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&quot;&quot;&quot;
@author: Nicolas 

The goal of this script is too highlight a problem that make crash the python 
kernel on Windows and Linux machine by using RandomForestRegressor
With scikit learn 0.18.
&quot;&quot;&quot;

import numpy as np
from sklearn.ensemble import RandomForestRegressor

def main():
    NumberOfRandomForest = 30
    
    # We create a random forest regressor
    RFR = RandomForestRegressor(n_estimators=100, criterion='mae', max_depth=None,min_samples_split=2, min_samples_leaf=1)
        
    print(&quot;Start the for loop&quot;)
    for i in range(NumberOfRandomForest):
        print(i)
        X = np.random.rand(150, 30)
        y = np.random.rand(150, 10)
        RFR.fit(X,y)

if __name__ == &quot;__main__&quot;:
    main()"><pre class="notranslate"><code class="notranslate">"""
@author: Nicolas 

The goal of this script is too highlight a problem that make crash the python 
kernel on Windows and Linux machine by using RandomForestRegressor
With scikit learn 0.18.
"""

import numpy as np
from sklearn.ensemble import RandomForestRegressor

def main():
    NumberOfRandomForest = 30
    
    # We create a random forest regressor
    RFR = RandomForestRegressor(n_estimators=100, criterion='mae', max_depth=None,min_samples_split=2, min_samples_leaf=1)
        
    print("Start the for loop")
    for i in range(NumberOfRandomForest):
        print(i)
        X = np.random.rand(150, 30)
        y = np.random.rand(150, 10)
        RFR.fit(X,y)

if __name__ == "__main__":
    main()
</code></pre></div>

<h4 dir="auto">Versions</h4>

<p dir="auto">I try my script on two different machines.</p>
<p dir="auto">On the first one the script crash after only 3 iterations whereas on the second the script can reach 25 iterations.<br>
The setup of the first machine :<br>
Windows-10-10.0.14393-SP0<br>
Python 3.5.2 |Anaconda 4.2.0 (32-bit)| (default, Jul  5 2016, 11:45:57) [MSC v.1900 32 bit (Intel)]<br>
NumPy 1.11.1<br>
SciPy 0.18.1<br>
Scikit-Learn 0.18</p>
<p dir="auto">The setup of the second one :<br>
Linux-3.10.0-327.28.2.el7.x86_64-x86_64-with-centos-7.2.1511-Core<br>
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul  2 2016, 17:53:06)<br>
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]<br>
NumPy 1.11.1<br>
SciPy 0.18.1<br>
Scikit-Learn 0.18.1</p>
<h4 dir="auto">Description</h4>
<p dir="auto">DecisionTree with mae criterion does not free used memory.<br>
As a result that kind of trees is unusable in cycle, forests and cross-validation.<br>
(originally i encoutered this problem while trying use grid search with mae forest)</p>
<h4 dir="auto">Steps/Code to Reproduce</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import sklearn

params = dict(max_depth=range(1, 10))
params = sklearn.model_selection.ParameterGrid(params)
reg = sklearn.tree.DecisionTreeRegressor(criterion='mae')

# repeated several times in jupyter cell
for p in params:
    print p
    for i in range(100):
        reg.set_params(**p)
        reg.fit(xxx, yyy)"><pre class="notranslate"><code class="notranslate">import sklearn

params = dict(max_depth=range(1, 10))
params = sklearn.model_selection.ParameterGrid(params)
reg = sklearn.tree.DecisionTreeRegressor(criterion='mae')

# repeated several times in jupyter cell
for p in params:
    print p
    for i in range(100):
        reg.set_params(**p)
        reg.fit(xxx, yyy)
</code></pre></div>
<h4 dir="auto">Expected Results</h4>
<p dir="auto">No additional memory usage after first completion of cycle.<br>
Cell with 'for' cycle is runnable unlimited times.</p>
<p dir="auto">Replacing 'mae' with 'mse' does give expected result.</p>
<h4 dir="auto">Actual Results</h4>
<p dir="auto">Additional memory is used after each cell execution.<br>
Deleteing tree with 'del' and enforcing 'gc.collect()' does not help.<br>
When there is no memory left OS (windows) shuts down python process.</p>
<p dir="auto">My guess is that there is memory leak in cython.</p>
<h4 dir="auto">Versions</h4>

<p dir="auto">Windows-7-6.1.7601-SP1<br>
('Python', '2.7.12 | packaged by conda-forge | (default, Aug 12 2016, 18:53:38) [MSC v.1500 64 bit (AMD64)]')<br>
('NumPy', '1.11.2')<br>
('SciPy', '0.18.0')<br>
('Scikit-Learn', '0.18')</p>