Issue 1:
Describe the bug

When I use the bagging-classifier in conjunction with LinearSVC it throws ValueError: WRITEBACKIFCOPY base is read-only when n_jobs!=1.

Changing n_jobs to 1 removes the error

Steps/Code to Reproduce

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.

My code is as following:

max_samples_pr_model = 50_000
n_models = X_train.shape[0] // max_samples_pr_model # 36
dual = max_samples_pr_model <= X_train.shape[1] # True

model_instance = LinearSVC(
max_iter=5_000,
dual=dual,
C=1.0,
class_weight="balanced")

model = BaggingClassifier(
random_state=42,
n_jobs=-1, # Setting as "1" 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)

Expected Results

No error is thrown and it works

Actual Results

The error ValueError: WRITEBACKIFCOPY is thrown with the following stack-trace

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 <listcomp>
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 "<ipython-input-14-e5fe18752d98>", line 17, in <module>
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

Versions

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

Issue 2:
Describe the bug

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.

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.

My environment is using scipy 1.10.1 and scikit-learn 1.2.2.

Any thoughts?

Steps/Code to Reproduce

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.

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("Loop {}".format(i))
mat = sio.matlab.loadmat(fname.format(i))

print("Mat Loaded")

x = mat['Var0']
y = mat['Var1']

r, fx, fy = linreg(x,y)
print("sklearn Called")
time.sleep(1)

If I run this code- I get this output

Loop 0

Mat Loaded

sklearn Called

C:...\anaconda3\envs\HCAcal\lib\site-packages\scipy\io\matlab_mio.py:227: MatReadWarning: Duplicate variable name "None" in stream - replacing previous with new

Consider mio5.varmats_from_mat to split file into single variable files

matfile_dict = MR.get_variables(variable_names)

If I comment out the line r, fx, fy = linreg(x,y) no error message appears.

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:

Loop 0

Mat Loaded

sklearn Called

Loop 1

Mat Loaded

sklearn Called

C:...\anaconda3\envs\HCAcal\lib\site-packages\scipy\io\matlab_mio.py:227: MatReadWarning: Duplicate variable name "None" in stream - replacing previous with new

Consider mio5.varmats_from_mat to split file into single variable files

matfile_dict = MR.get_variables(variable_names)

C:...\anaconda3\envs\HCAcal\lib\site-packages\scipy\io\matlab_mio.py:227: MatReadWarning: Duplicate variable name "None" in stream - replacing previous with new

Consider mio5.varmats_from_mat to split file into single variable files

matfile_dict = MR.get_variables(variable_names)

Finally, a really bizzare issue is that if I comment out the linreg 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-

LinReg Call - All get Warnings

LinReg Commented Out First Time - First one gets warning

LinReg commented Out after first - No Warning

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.

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.

Finally, the message output is actually deprecated in scipy 1.10.1

https://github.com/scipy/scipy/blob/main/scipy/io/matlab/mio5.py

My environment is using scipy 1.10.1 and scikit-learn 1.2.2.

Any thoughts?

Expected Results

Loop 0

Mat Loaded

sklearn Called

Loop 1

Mat Loaded

sklearn Called

Actual Results

Loop 0

Mat Loaded

sklearn Called

C:...\anaconda3\envs\HCAcal\lib\site-packages\scipy\io\matlab_mio.py:227: MatReadWarning: Duplicate variable name "None" in stream - replacing previous with new

Consider mio5.varmats_from_mat to split file into single variable files

matfile_dict = MR.get_variables(variable_names)

Loop 1

Mat Loaded

sklearn Called

C:...\anaconda3\envs\HCAcal\lib\site-packages\scipy\io\matlab_mio.py:227: MatReadWarning: Duplicate variable name "None" in stream - replacing previous with new

Consider mio5.varmats_from_mat to split file into single variable files

matfile_dict = MR.get_variables(variable_names)

Versions

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