<p dir="auto">Hi, It looks like the way that <code class="notranslate">csr_matrix</code> objects are created by <code class="notranslate">FeatureHasher</code> can result in outputs that explicitly represent a lot of 0s.</p>
<p dir="auto"><code class="notranslate">FeatureHasher</code> uses the tuple-based initialization for <code class="notranslate">csr_matrix</code>, which will just take 0s and store them rather than trying to filter them out for efficient storage.</p>
<p dir="auto">I think the simplest way to address this is to call <code class="notranslate">eliminate_zeros()</code> on the matrix before returning it.  However, I'm not sure this would be efficient enough for very large datasets.  It might be better to construct one row at a time, call <code class="notranslate">eliminate_zeros()</code>, and then stack them or something like that.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="m = sp.csr_matrix(([0,1,0,0],[0,1,0,1],[0,2,4]), dtype=int)
print m.nnz
m.eliminate_zeros()
print m.nnz"><pre class="notranslate"><span class="pl-s1">m</span> <span class="pl-c1">=</span> <span class="pl-s1">sp</span>.<span class="pl-en">csr_matrix</span>(([<span class="pl-c1">0</span>,<span class="pl-c1">1</span>,<span class="pl-c1">0</span>,<span class="pl-c1">0</span>],[<span class="pl-c1">0</span>,<span class="pl-c1">1</span>,<span class="pl-c1">0</span>,<span class="pl-c1">1</span>],[<span class="pl-c1">0</span>,<span class="pl-c1">2</span>,<span class="pl-c1">4</span>]), <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">int</span>)
<span class="pl-k">print</span> <span class="pl-s1">m</span>.<span class="pl-s1">nnz</span>
<span class="pl-s1">m</span>.<span class="pl-en">eliminate_zeros</span>()
<span class="pl-k">print</span> <span class="pl-s1">m</span>.<span class="pl-s1">nnz</span></pre></div>
<h3 dir="auto">Describe the bug</h3>
<p dir="auto">While using <code class="notranslate">sklearn.ensemble.RandomForestClassifier</code> with a custom dictionary specified for <code class="notranslate">class_weight</code> parameter, a <code class="notranslate">ValueError</code> is raised when a key (label) present in <code class="notranslate">class_weight</code> is not found in any of the labels. I got this error while using <code class="notranslate">TimeSeriesSplit</code>. Specifically, if a split is such that training data doesn't have a label specified in <code class="notranslate">class_weight</code>, a <code class="notranslate">ValueError</code> will be raised. This stops users from evaluating their model since <code class="notranslate">class_weight</code> cannot be changed on a go after the model has been created.</p>
<p dir="auto">Is there any reason this check is being done? There's no error when we don't specify class_weight. The code raising this error can be found here (<a href="https://github.com/scikit-learn/scikit-learn/blob/5900e2660f4293b52ea57bf830e7bff312779036/sklearn/utils/class_weight.py#L8">link</a>).</p>
<h3 dir="auto">Steps/Code to Reproduce</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(class_weight={1:10, 0:1})
rfc.fit([[0, 0, 1], [1, 0, 1]], [0, 0])"><pre class="notranslate"><code class="notranslate">from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(class_weight={1:10, 0:1})
rfc.fit([[0, 0, 1], [1, 0, 1]], [0, 0])
</code></pre></div>
<h3 dir="auto">Expected Results</h3>
<p dir="auto">No errors raised.</p>
<h3 dir="auto">Actual Results</h3>
<p dir="auto"><code class="notranslate">ValueError: Class label 1 not present.</code></p>
<h3 dir="auto">Versions</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="System:
    python: 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)]
executable: c:\python3.9.2\python.exe
   machine: Windows-10-10.0.19041-SP0

Python dependencies:
          pip: 21.3.1
   setuptools: 58.3.0
      sklearn: 0.24.1
        numpy: 1.21.3
        scipy: 1.6.1
       Cython: 0.29.24
       pandas: 1.2.3
   matplotlib: 3.3.4
       joblib: 1.0.1
threadpoolctl: 2.1.0

Built with OpenMP: True"><pre class="notranslate">System:
    python: 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)]
executable: c:<span class="pl-cce">\p</span>ython3.9.2<span class="pl-cce">\p</span>ython.exe
   machine: Windows-10-10.0.19041-SP0

Python dependencies:
          pip: 21.3.1
   setuptools: 58.3.0
      sklearn: 0.24.1
        numpy: 1.21.3
        scipy: 1.6.1
       Cython: 0.29.24
       pandas: 1.2.3
   matplotlib: 3.3.4
       joblib: 1.0.1
threadpoolctl: 2.1.0

Built with OpenMP: True</pre></div>