Closed
Description
Describe the bug
I get "source array is read only" error in Dictionary Learning when I try to fit it to the Cifar10 Dataset.
Steps/Code to Reproduce
from sklearn.decomposition import DictionaryLearning
from keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data() #or x_train = np.random((50000, 32,32,3))
dict_learner = DictionaryLearning(
n_components= 56,
random_state=0,
n_jobs=15,
fit_algorithm='cd',
verbose= True,
max_iter= 50
)
s_train = dict_learner.fit_transform(x_train.reshape(50000, 3072))
Expected Results
The dictionary
Actual Results
File "sklearn\linear_model\_cd_fast.pyx", line 568, in sklearn.linear_model._cd_fast.enet_coordinate_descent_gram
File "stringsource", line 660, in View.MemoryView.memoryview_cwrapper
File "stringsource", line 350, in View.MemoryView.memoryview.__cinit__
ValueError: buffer source array is read-only
"""
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
Cell In [16], line 13
4 #out = dict_learning_online(x_train[np.random.choice(a=50000, size=10000)].reshape(10000, 3072), n_components=3072)
5 dict_learner = DictionaryLearning(
6 n_components= 56,
7 random_state=0,
(...)
11 max_iter= 50
12 )
---> 13 s_train = dict_learner.fit_transform(x_train.reshape(50000, 3072))
File c:\Users\shafi\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\utils\_set_output.py:142, in _wrap_method_output.<locals>.wrapped(self, X, *args, **kwargs)
140 @wraps(f)
141 def wrapped(self, X, *args, **kwargs):
--> 142 data_to_wrap = f(self, X, *args, **kwargs)
143 if isinstance(data_to_wrap, tuple):
144 # only wrap the first output for cross decomposition
145 return (
146 _wrap_data_with_container(method, data_to_wrap[0], X, self),
147 *data_to_wrap[1:],
148 )
File c:\Users\shafi\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\utils\_set_output.py:142, in _wrap_method_output.<locals>.wrapped(self, X, *args, **kwargs)
140 @wraps(f)
141 def wrapped(self, X, *args, **kwargs):
--> 142 data_to_wrap = f(self, X, *args, **kwargs)
143 if isinstance(data_to_wrap, tuple):
144 # only wrap the first output for cross decomposition
145 return (
146 _wrap_data_with_container(method, data_to_wrap[0], X, self),
147 *data_to_wrap[1:],
148 )
File c:\Users\shafi\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\utils\_set_output.py:142, in _wrap_method_output.<locals>.wrapped(self, X, *args, **kwargs)
140 @wraps(f)
141 def wrapped(self, X, *args, **kwargs):
--> 142 data_to_wrap = f(self, X, *args, **kwargs)
143 if isinstance(data_to_wrap, tuple):
144 # only wrap the first output for cross decomposition
145 return (
146 _wrap_data_with_container(method, data_to_wrap[0], X, self),
147 *data_to_wrap[1:],
148 )
File c:\Users\shafi\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\base.py:848, in TransformerMixin.fit_transform(self, X, y, **fit_params)
844 # non-optimized default implementation; override when a better
845 # method is possible for a given clustering algorithm
846 if y is None:
847 # fit method of arity 1 (unsupervised transformation)
--> 848 return self.fit(X, **fit_params).transform(X)
849 else:
850 # fit method of arity 2 (supervised transformation)
851 return self.fit(X, y, **fit_params).transform(X)
File c:\Users\shafi\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\decomposition\_dict_learning.py:1719, in DictionaryLearning.fit(self, X, y)
1716 else:
1717 n_components = self.n_components
-> 1719 V, U, E, self.n_iter_ = dict_learning(
1720 X,
1721 n_components,
1722 alpha=self.alpha,
1723 tol=self.tol,
1724 max_iter=self.max_iter,
1725 method=self.fit_algorithm,
...
--> 389 raise self._exception
390 else:
391 return self._result
ValueError: buffer source array is read-only
Versions
System:
python: 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)]
executable: c:\Users\shafi\AppData\Local\Programs\Python\Python39\python.exe
machine: Windows-10-10.0.19041-SP0
Python dependencies:
sklearn: 1.2.0
pip: 20.2.3
setuptools: 49.2.1
numpy: 1.23.4
scipy: 1.8.0
Cython: None
pandas: 1.5.1
matplotlib: 3.4.3
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\shafi\AppData\Roaming\Python\Python39\site-packages\numpy\.libs\libopenblas.WCDJNK7YVMPZQ2ME2ZZHJJRJ3JIKNDB7.gfortran-win_amd64.dll
version: 0.3.13
threading_layer: pthreads
architecture: Zen
num_threads: 16
user_api: blas
internal_api: openblas
prefix: libopenblas
filepath: C:\Users\shafi\AppData\Local\Programs\Python\Python39\Lib\site-packages\scipy\.libs\libopenblas.XWYDX2IKJW2NMTWSFYNGFUWKQU3LYTCZ.gfortran-win_amd64.dll
version: 0.3.17
threading_layer: pthreads
architecture: Zen
num_threads: 16
user_api: openmp
internal_api: openmp
prefix: vcomp
filepath: C:\Users\shafi\AppData\Local\Programs\Python\Python39\Lib\site-packages\sklearn\.libs\vcomp140.dll
version: None
num_threads: 16