<h3 dir="auto">Describe the bug</h3>
<p dir="auto">When I use the bagging-classifier in conjunction with LinearSVC it throws <code class="notranslate">ValueError: WRITEBACKIFCOPY base is read-only</code> when <code class="notranslate">n_jobs!=1</code>.</p>
<p dir="auto">Changing <code class="notranslate">n_jobs</code> to 1 removes the error</p>
<h3 dir="auto">Steps/Code to Reproduce</h3>
<p dir="auto">The issue is that I cannot reproduce this error with e.g iris-data but I can't share the dataset since it's company-classified.</p>
<p dir="auto">My code is as following:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="
  max_samples_pr_model = 50_000
  n_models = X_train.shape[0] // max_samples_pr_model # 36
  dual = max_samples_pr_model &lt;= X_train.shape[1]  # True

  model_instance = LinearSVC(
      max_iter=5_000,
      dual=dual,
      C=1.0,
      class_weight=&quot;balanced&quot;)

  model = BaggingClassifier(
      random_state=42,
      n_jobs=-1, # Setting as &quot;1&quot; seems to remove the error
      n_estimators=n_models,
      max_samples=max_samples_pr_model,
      max_features=1.0,
      estimator=model_instance

  )

    model.fit(X_train, y_train)"><pre class="notranslate">  <span class="pl-s1">max_samples_pr_model</span> <span class="pl-c1">=</span> <span class="pl-c1">50_000</span>
  <span class="pl-s1">n_models</span> <span class="pl-c1">=</span> <span class="pl-v">X_train</span>.<span class="pl-s1">shape</span>[<span class="pl-c1">0</span>] <span class="pl-c1">//</span> <span class="pl-s1">max_samples_pr_model</span> <span class="pl-c"># 36</span>
  <span class="pl-s1">dual</span> <span class="pl-c1">=</span> <span class="pl-s1">max_samples_pr_model</span> <span class="pl-c1">&lt;=</span> <span class="pl-v">X_train</span>.<span class="pl-s1">shape</span>[<span class="pl-c1">1</span>]  <span class="pl-c"># True</span>

  <span class="pl-s1">model_instance</span> <span class="pl-c1">=</span> <span class="pl-v">LinearSVC</span>(
      <span class="pl-s1">max_iter</span><span class="pl-c1">=</span><span class="pl-c1">5_000</span>,
      <span class="pl-s1">dual</span><span class="pl-c1">=</span><span class="pl-s1">dual</span>,
      <span class="pl-v">C</span><span class="pl-c1">=</span><span class="pl-c1">1.0</span>,
      <span class="pl-s1">class_weight</span><span class="pl-c1">=</span><span class="pl-s">"balanced"</span>)

  <span class="pl-s1">model</span> <span class="pl-c1">=</span> <span class="pl-v">BaggingClassifier</span>(
      <span class="pl-s1">random_state</span><span class="pl-c1">=</span><span class="pl-c1">42</span>,
      <span class="pl-s1">n_jobs</span><span class="pl-c1">=</span><span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c"># Setting as "1" seems to remove the error</span>
      <span class="pl-s1">n_estimators</span><span class="pl-c1">=</span><span class="pl-s1">n_models</span>,
      <span class="pl-s1">max_samples</span><span class="pl-c1">=</span><span class="pl-s1">max_samples_pr_model</span>,
      <span class="pl-s1">max_features</span><span class="pl-c1">=</span><span class="pl-c1">1.0</span>,
      <span class="pl-s1">estimator</span><span class="pl-c1">=</span><span class="pl-s1">model_instance</span>

  )

    <span class="pl-s1">model</span>.<span class="pl-en">fit</span>(<span class="pl-v">X_train</span>, <span class="pl-s1">y_train</span>)</pre></div>
<h3 dir="auto">Expected Results</h3>
<p dir="auto">No error is thrown and it works</p>
<h3 dir="auto">Actual Results</h3>
<p dir="auto">The error <code class="notranslate">ValueError: WRITEBACKIFCOPY</code> is thrown with the following stack-trace</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="joblib.externals.loky.process_executor._RemoteTraceback: 
&quot;&quot;&quot;
Traceback (most recent call last):
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\externals\loky\process_executor.py&quot;, line 428, in _process_worker
    r = call_item()
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\externals\loky\process_executor.py&quot;, line 275, in __call__
    return self.fn(*self.args, **self.kwargs)
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\_parallel_backends.py&quot;, line 620, in __call__
    return self.func(*args, **kwargs)
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\parallel.py&quot;, line 288, in __call__
    return [func(*args, **kwargs)
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\parallel.py&quot;, line 288, in &lt;listcomp&gt;
    return [func(*args, **kwargs)
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\utils\parallel.py&quot;, line 123, in __call__
    return self.function(*args, **kwargs)
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\ensemble\_bagging.py&quot;, line 141, in _parallel_build_estimators
    estimator_fit(X_, y, sample_weight=curr_sample_weight)
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\svm\_classes.py&quot;, line 263, in fit
    X, y = self._validate_data(
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\base.py&quot;, line 565, in _validate_data
    X, y = check_X_y(X, y, **check_params)
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\utils\validation.py&quot;, line 1106, in check_X_y
    X = check_array(
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\utils\validation.py&quot;, line 845, in check_array
    array = _ensure_sparse_format(
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\utils\validation.py&quot;, line 549, in _ensure_sparse_format
    spmatrix = spmatrix.astype(dtype)
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\scipy\sparse\_data.py&quot;, line 72, in astype
    self._deduped_data().astype(dtype, casting=casting, copy=copy),
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\scipy\sparse\_data.py&quot;, line 32, in _deduped_data
    self.sum_duplicates()
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\scipy\sparse\_compressed.py&quot;, line 1118, in sum_duplicates
    self.sort_indices()
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\scipy\sparse\_compressed.py&quot;, line 1164, in sort_indices
    _sparsetools.csr_sort_indices(len(self.indptr) - 1, self.indptr,
ValueError: WRITEBACKIFCOPY base is read-only
&quot;&quot;&quot;


The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\IPython\core\interactiveshell.py&quot;, line 3460, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File &quot;&lt;ipython-input-14-e5fe18752d98&gt;&quot;, line 17, in &lt;module&gt;
    model.fit(X_train, y_train)
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\ensemble\_bagging.py&quot;, line 337, in fit
    return self._fit(X, y, self.max_samples, sample_weight=sample_weight)
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\ensemble\_bagging.py&quot;, line 472, in _fit
    all_results = Parallel(
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\utils\parallel.py&quot;, line 63, in __call__
    return super().__call__(iterable_with_config)
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\parallel.py&quot;, line 1098, in __call__
    self.retrieve()
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\parallel.py&quot;, line 975, in retrieve
    self._output.extend(job.get(timeout=self.timeout))
  File &quot;C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\_parallel_backends.py&quot;, line 567, in wrap_future_result
    return future.result(timeout=timeout)
  File &quot;C:\Users\my_user\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\_base.py&quot;, line 445, in result
    return self.__get_result()
  File &quot;C:\Users\my_user\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\_base.py&quot;, line 390, in __get_result
    raise self._exception
ValueError: WRITEBACKIFCOPY base is read-only"><pre class="notranslate"><code class="notranslate">joblib.externals.loky.process_executor._RemoteTraceback: 
"""
Traceback (most recent call last):
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\externals\loky\process_executor.py", line 428, in _process_worker
    r = call_item()
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\externals\loky\process_executor.py", line 275, in __call__
    return self.fn(*self.args, **self.kwargs)
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\_parallel_backends.py", line 620, in __call__
    return self.func(*args, **kwargs)
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\parallel.py", line 288, in __call__
    return [func(*args, **kwargs)
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\parallel.py", line 288, in &lt;listcomp&gt;
    return [func(*args, **kwargs)
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\utils\parallel.py", line 123, in __call__
    return self.function(*args, **kwargs)
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\ensemble\_bagging.py", line 141, in _parallel_build_estimators
    estimator_fit(X_, y, sample_weight=curr_sample_weight)
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\svm\_classes.py", line 263, in fit
    X, y = self._validate_data(
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\base.py", line 565, in _validate_data
    X, y = check_X_y(X, y, **check_params)
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\utils\validation.py", line 1106, in check_X_y
    X = check_array(
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\utils\validation.py", line 845, in check_array
    array = _ensure_sparse_format(
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\utils\validation.py", line 549, in _ensure_sparse_format
    spmatrix = spmatrix.astype(dtype)
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\scipy\sparse\_data.py", line 72, in astype
    self._deduped_data().astype(dtype, casting=casting, copy=copy),
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\scipy\sparse\_data.py", line 32, in _deduped_data
    self.sum_duplicates()
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\scipy\sparse\_compressed.py", line 1118, in sum_duplicates
    self.sort_indices()
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\scipy\sparse\_compressed.py", line 1164, in sort_indices
    _sparsetools.csr_sort_indices(len(self.indptr) - 1, self.indptr,
ValueError: WRITEBACKIFCOPY base is read-only
"""


The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\IPython\core\interactiveshell.py", line 3460, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "&lt;ipython-input-14-e5fe18752d98&gt;", line 17, in &lt;module&gt;
    model.fit(X_train, y_train)
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\ensemble\_bagging.py", line 337, in fit
    return self._fit(X, y, self.max_samples, sample_weight=sample_weight)
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\ensemble\_bagging.py", line 472, in _fit
    all_results = Parallel(
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\sklearn\utils\parallel.py", line 63, in __call__
    return super().__call__(iterable_with_config)
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\parallel.py", line 1098, in __call__
    self.retrieve()
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\parallel.py", line 975, in retrieve
    self._output.extend(job.get(timeout=self.timeout))
  File "C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\lib\site-packages\joblib\_parallel_backends.py", line 567, in wrap_future_result
    return future.result(timeout=timeout)
  File "C:\Users\my_user\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\_base.py", line 445, in result
    return self.__get_result()
  File "C:\Users\my_user\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\_base.py", line 390, in __get_result
    raise self._exception
ValueError: WRITEBACKIFCOPY base is read-only
</code></pre></div>
<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.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]
executable: C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\Scripts\python.exe
   machine: Windows-10-10.0.22621-SP0
Python dependencies:
      sklearn: 1.2.1
          pip: 22.3.1
   setuptools: 67.5.1
        numpy: 1.24.2
        scipy: 1.10.1
       Cython: 0.29.33
       pandas: 1.5.3
   matplotlib: 3.7.1
       joblib: 1.2.0
threadpoolctl: 3.1.0
Built with OpenMP: True
threadpoolctl info:
       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\Lib\site-packages\numpy\.libs\libopenblas64__v0.3.21-gcc_10_3_0.dll
        version: 0.3.21
threading_layer: pthreads
   architecture: SkylakeX
    num_threads: 16
       user_api: openmp
   internal_api: openmp
         prefix: vcomp
       filepath: C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\Lib\site-packages\sklearn\.libs\vcomp140.dll
        version: None
    num_threads: 16
       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: C:\Users\my_user\AppData\Local\pypoetry\Cache\virtualenvs\transtotag-6uueIOAW-py3.10\Lib\site-packages\scipy.libs\libopenblas-802f9ed1179cb9c9b03d67ff79f48187.dll
        version: 0.3.18
threading_layer: pthreads
   architecture: Prescott
    num_threads: 16"><pre class="notranslate">System:
    python: 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]
executable: C:<span class="pl-cce">\U</span>sers<span class="pl-cce">\m</span>y_user<span class="pl-cce">\A</span>ppData<span class="pl-cce">\L</span>ocal<span class="pl-cce">\p</span>ypoetry<span class="pl-cce">\C</span>ache<span class="pl-cce">\v</span>irtualenvs<span class="pl-cce">\t</span>ranstotag-6uueIOAW-py3.10<span class="pl-cce">\S</span>cripts<span class="pl-cce">\p</span>ython.exe
   machine: Windows-10-10.0.22621-SP0
Python dependencies:
      sklearn: 1.2.1
          pip: 22.3.1
   setuptools: 67.5.1
        numpy: 1.24.2
        scipy: 1.10.1
       Cython: 0.29.33
       pandas: 1.5.3
   matplotlib: 3.7.1
       joblib: 1.2.0
threadpoolctl: 3.1.0
Built with OpenMP: True
threadpoolctl info:
       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: C:<span class="pl-cce">\U</span>sers<span class="pl-cce">\m</span>y_user<span class="pl-cce">\A</span>ppData<span class="pl-cce">\L</span>ocal<span class="pl-cce">\p</span>ypoetry<span class="pl-cce">\C</span>ache<span class="pl-cce">\v</span>irtualenvs<span class="pl-cce">\t</span>ranstotag-6uueIOAW-py3.10<span class="pl-cce">\L</span>ib<span class="pl-cce">\s</span>ite-packages<span class="pl-cce">\n</span>umpy<span class="pl-cce">\.</span>libs<span class="pl-cce">\l</span>ibopenblas64__v0.3.21-gcc_10_3_0.dll
        version: 0.3.21
threading_layer: pthreads
   architecture: SkylakeX
    num_threads: 16
       user_api: openmp
   internal_api: openmp
         prefix: vcomp
       filepath: C:<span class="pl-cce">\U</span>sers<span class="pl-cce">\m</span>y_user<span class="pl-cce">\A</span>ppData<span class="pl-cce">\L</span>ocal<span class="pl-cce">\p</span>ypoetry<span class="pl-cce">\C</span>ache<span class="pl-cce">\v</span>irtualenvs<span class="pl-cce">\t</span>ranstotag-6uueIOAW-py3.10<span class="pl-cce">\L</span>ib<span class="pl-cce">\s</span>ite-packages<span class="pl-cce">\s</span>klearn<span class="pl-cce">\.</span>libs<span class="pl-cce">\v</span>comp140.dll
        version: None
    num_threads: 16
       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: C:<span class="pl-cce">\U</span>sers<span class="pl-cce">\m</span>y_user<span class="pl-cce">\A</span>ppData<span class="pl-cce">\L</span>ocal<span class="pl-cce">\p</span>ypoetry<span class="pl-cce">\C</span>ache<span class="pl-cce">\v</span>irtualenvs<span class="pl-cce">\t</span>ranstotag-6uueIOAW-py3.10<span class="pl-cce">\L</span>ib<span class="pl-cce">\s</span>ite-packages<span class="pl-cce">\s</span>cipy.libs<span class="pl-cce">\l</span>ibopenblas-802f9ed1179cb9c9b03d67ff79f48187.dll
        version: 0.3.18
threading_layer: pthreads
   architecture: Prescott
    num_threads: 16</pre></div>
<h3 dir="auto">Describe the bug</h3>
<p dir="auto">I am working on a project for work and the data is sensitive and I share those files.  I've reduced the issue to the attached code and longer description.</p>
<p dir="auto">I get scipy MatReadWarnings when I called sklearn linear regression module.  Further, the mat read warning I get is a deprecated method.  This warning shows up if and only if I call the linear regression function that uses the sklearn.linear_model.LinearRegression class, just importing it did not cause any issues.  No warning messages appeared.</p>
<p dir="auto">My environment is using scipy 1.10.1 and scikit-learn 1.2.2.</p>
<p dir="auto">Any thoughts?</p>
<h3 dir="auto">Steps/Code to Reproduce</h3>
<p dir="auto">I am working on a project for work and the data is sensitive and I share those files.  I've reduced the issue to the following code.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import scipy.io as sio
from sklearn.linear_model import LinearRegression as LR
import time

def linreg(x,y):
    model = LR().fit(x,y)
    r = model.score(x,y)
    fitx = [min(x),max(x)]
    fity = model.predict(fitx)
    
    return r, fitx, fity


if __name__ == '__main__':
    
    
    fname = 'file{}.mat'
    for i in range(5):
        print(&quot;Loop {}&quot;.format(i))
        mat = sio.matlab.loadmat(fname.format(i))
        
        print(&quot;Mat Loaded&quot;)
        
        x = mat['Var0']
        y = mat['Var1']
 
        r, fx, fy = linreg(x,y)
        print(&quot;sklearn Called&quot;)
        time.sleep(1)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">io</span> <span class="pl-k">as</span> <span class="pl-s1">sio</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">linear_model</span> <span class="pl-k">import</span> <span class="pl-v">LinearRegression</span> <span class="pl-k">as</span> <span class="pl-v">LR</span>
<span class="pl-k">import</span> <span class="pl-s1">time</span>

<span class="pl-k">def</span> <span class="pl-en">linreg</span>(<span class="pl-s1">x</span>,<span class="pl-s1">y</span>):
    <span class="pl-s1">model</span> <span class="pl-c1">=</span> <span class="pl-v">LR</span>().<span class="pl-en">fit</span>(<span class="pl-s1">x</span>,<span class="pl-s1">y</span>)
    <span class="pl-s1">r</span> <span class="pl-c1">=</span> <span class="pl-s1">model</span>.<span class="pl-en">score</span>(<span class="pl-s1">x</span>,<span class="pl-s1">y</span>)
    <span class="pl-s1">fitx</span> <span class="pl-c1">=</span> [<span class="pl-en">min</span>(<span class="pl-s1">x</span>),<span class="pl-en">max</span>(<span class="pl-s1">x</span>)]
    <span class="pl-s1">fity</span> <span class="pl-c1">=</span> <span class="pl-s1">model</span>.<span class="pl-en">predict</span>(<span class="pl-s1">fitx</span>)
    
    <span class="pl-k">return</span> <span class="pl-s1">r</span>, <span class="pl-s1">fitx</span>, <span class="pl-s1">fity</span>


<span class="pl-k">if</span> <span class="pl-s1">__name__</span> <span class="pl-c1">==</span> <span class="pl-s">'__main__'</span>:
    
    
    <span class="pl-s1">fname</span> <span class="pl-c1">=</span> <span class="pl-s">'file{}.mat'</span>
    <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">5</span>):
        <span class="pl-en">print</span>(<span class="pl-s">"Loop {}"</span>.<span class="pl-en">format</span>(<span class="pl-s1">i</span>))
        <span class="pl-s1">mat</span> <span class="pl-c1">=</span> <span class="pl-s1">sio</span>.<span class="pl-s1">matlab</span>.<span class="pl-en">loadmat</span>(<span class="pl-s1">fname</span>.<span class="pl-en">format</span>(<span class="pl-s1">i</span>))
        
        <span class="pl-en">print</span>(<span class="pl-s">"Mat Loaded"</span>)
        
        <span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">mat</span>[<span class="pl-s">'Var0'</span>]
        <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">mat</span>[<span class="pl-s">'Var1'</span>]
 
        <span class="pl-s1">r</span>, <span class="pl-s1">fx</span>, <span class="pl-s1">fy</span> <span class="pl-c1">=</span> <span class="pl-en">linreg</span>(<span class="pl-s1">x</span>,<span class="pl-s1">y</span>)
        <span class="pl-en">print</span>(<span class="pl-s">"sklearn Called"</span>)
        <span class="pl-s1">time</span>.<span class="pl-en">sleep</span>(<span class="pl-c1">1</span>)</pre></div>
<p dir="auto">If I run this code- I get this output</p>
<blockquote>
<p dir="auto">Loop 0<br>
Mat Loaded<br>
sklearn Called<br>
C:...\anaconda3\envs\HCAcal\lib\site-packages\scipy\io\matlab_mio.py:227: MatReadWarning: Duplicate variable name "None" in stream - replacing previous with new<br>
Consider mio5.varmats_from_mat to split file into single variable files<br>
matfile_dict = MR.get_variables(variable_names)</p>
</blockquote>
<p dir="auto">If I comment out the line <code class="notranslate">r, fx, fy = linreg(x,y)</code> no error message appears.<br>
If I comment out the line time.sleep(1) then there is a bit more randomness to the output. In the sense it will do it in groups of 3-5:</p>
<blockquote>
<p dir="auto">Loop 0<br>
Mat Loaded<br>
sklearn Called<br>
Loop 1<br>
Mat Loaded<br>
sklearn Called<br>
C:...\anaconda3\envs\HCAcal\lib\site-packages\scipy\io\matlab_mio.py:227: MatReadWarning: Duplicate variable name "None" in stream - replacing previous with new<br>
Consider mio5.varmats_from_mat to split file into single variable files<br>
matfile_dict = MR.get_variables(variable_names)<br>
C:...\anaconda3\envs\HCAcal\lib\site-packages\scipy\io\matlab_mio.py:227: MatReadWarning: Duplicate variable name "None" in stream - replacing previous with new<br>
Consider mio5.varmats_from_mat to split file into single variable files<br>
matfile_dict = MR.get_variables(variable_names)</p>
</blockquote>
<p dir="auto">Finally, a really bizzare issue is that if I comment out the <code class="notranslate">linreg</code> call after having it on, the first time I run the script with it commented out throws the same warning, but then it disappears for the rest of them.  And I run it back to back commented out, no warning.  So that goes-<br>
LinReg Call - All get Warnings<br>
LinReg Commented Out First Time - First one gets warning<br>
LinReg commented Out after first - No Warning</p>
<p dir="auto">I have tried to create a test set of mat files to share but I have not been able to do so.  One would think it is related to my Mats, but I have triple checked that there is no variable 'None' in any of them, let alone duplicates.  Which also seems like part of a garbage collection step, but I'm just making noise on that.  No idea.  The biggest thing is that this error does not happen when I load the mat and do not call LinearRegression module from scikit-learn.</p>
<p dir="auto">It would seem there is some memory freeing issue but I cannot really figure out anything interesting using the garbage tools in python.  I tried to create large mat files to simulate the ones I cannot share, but so far, the problem is a unique to the mat files as well.  Which doesn't help matters, but the issue still stands that I get a deprecated warning from scikit learn for scipy.</p>
<p dir="auto">Finally, the message output is actually deprecated in scipy 1.10.1<br>
<a href="https://github.com/scipy/scipy/blob/main/scipy/io/matlab/mio5.py">https://github.com/scipy/scipy/blob/main/scipy/io/matlab/mio5.py</a></p>
<p dir="auto">My environment is using scipy 1.10.1 and scikit-learn 1.2.2.</p>
<p dir="auto">Any thoughts?</p>
<h3 dir="auto">Expected Results</h3>
<blockquote>
<p dir="auto">Loop 0<br>
Mat Loaded<br>
sklearn Called<br>
Loop 1<br>
Mat Loaded<br>
sklearn Called</p>
</blockquote>
<h3 dir="auto">Actual Results</h3>
<blockquote>
<p dir="auto">Loop 0<br>
Mat Loaded<br>
sklearn Called<br>
C:...\anaconda3\envs\HCAcal\lib\site-packages\scipy\io\matlab_mio.py:227: MatReadWarning: Duplicate variable name "None" in stream - replacing previous with new<br>
Consider mio5.varmats_from_mat to split file into single variable files<br>
matfile_dict = MR.get_variables(variable_names)<br>
Loop 1<br>
Mat Loaded<br>
sklearn Called<br>
C:...\anaconda3\envs\HCAcal\lib\site-packages\scipy\io\matlab_mio.py:227: MatReadWarning: Duplicate variable name "None" in stream - replacing previous with new<br>
Consider mio5.varmats_from_mat to split file into single variable files<br>
matfile_dict = MR.get_variables(variable_names)</p>
</blockquote>
<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.10.10 | packaged by Anaconda, Inc. | (main, Mar 21 2023, 18:39:17) [MSC v.1916 64 bit (AMD64)]
executable: C:\Users\i12500198\AppData\Local\anaconda3\envs\HCAcal\python.exe
   machine: Windows-10-10.0.19045-SP0

Python dependencies:
      sklearn: 1.2.2
          pip: 23.1.2
   setuptools: 67.8.0
        numpy: 1.24.3
        scipy: 1.10.1
       Cython: None
       pandas: 1.5.3
   matplotlib: 3.7.1
       joblib: 1.2.0
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: C:\Users\i12500198\AppData\Local\anaconda3\envs\HCAcal\Library\bin\mkl_rt.1.dll
         prefix: mkl_rt
       user_api: blas
   internal_api: mkl
        version: 2021.4-Product
    num_threads: 8
threading_layer: intel

       filepath: C:\Users\i12500198\AppData\Local\anaconda3\envs\HCAcal\vcomp140.dll
         prefix: vcomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 16

       filepath: C:\Users\i12500198\AppData\Local\anaconda3\envs\HCAcal\Library\bin\libiomp5md.dll
         prefix: libiomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 16"><pre class="notranslate">System:
    python: 3.10.10 <span class="pl-k">|</span> packaged by Anaconda, Inc. <span class="pl-k">|</span> (main, Mar 21 2023, 18:39:17) [MSC v.1916 64 bit (AMD64)]
executable: C:<span class="pl-cce">\U</span>sers<span class="pl-cce">\i</span>12500198<span class="pl-cce">\A</span>ppData<span class="pl-cce">\L</span>ocal<span class="pl-cce">\a</span>naconda3<span class="pl-cce">\e</span>nvs<span class="pl-cce">\H</span>CAcal<span class="pl-cce">\p</span>ython.exe
   machine: Windows-10-10.0.19045-SP0

Python dependencies:
      sklearn: 1.2.2
          pip: 23.1.2
   setuptools: 67.8.0
        numpy: 1.24.3
        scipy: 1.10.1
       Cython: None
       pandas: 1.5.3
   matplotlib: 3.7.1
       joblib: 1.2.0
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: C:<span class="pl-cce">\U</span>sers<span class="pl-cce">\i</span>12500198<span class="pl-cce">\A</span>ppData<span class="pl-cce">\L</span>ocal<span class="pl-cce">\a</span>naconda3<span class="pl-cce">\e</span>nvs<span class="pl-cce">\H</span>CAcal<span class="pl-cce">\L</span>ibrary<span class="pl-cce">\b</span>in<span class="pl-cce">\m</span>kl_rt.1.dll
         prefix: mkl_rt
       user_api: blas
   internal_api: mkl
        version: 2021.4-Product
    num_threads: 8
threading_layer: intel

       filepath: C:<span class="pl-cce">\U</span>sers<span class="pl-cce">\i</span>12500198<span class="pl-cce">\A</span>ppData<span class="pl-cce">\L</span>ocal<span class="pl-cce">\a</span>naconda3<span class="pl-cce">\e</span>nvs<span class="pl-cce">\H</span>CAcal<span class="pl-cce">\v</span>comp140.dll
         prefix: vcomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 16

       filepath: C:<span class="pl-cce">\U</span>sers<span class="pl-cce">\i</span>12500198<span class="pl-cce">\A</span>ppData<span class="pl-cce">\L</span>ocal<span class="pl-cce">\a</span>naconda3<span class="pl-cce">\e</span>nvs<span class="pl-cce">\H</span>CAcal<span class="pl-cce">\L</span>ibrary<span class="pl-cce">\b</span>in<span class="pl-cce">\l</span>ibiomp5md.dll
         prefix: libiomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 16</pre></div>