————————————————————————— ValueError Traceback (most recent call last) Cell In[195], line 11 9 # データの標準化 10 scaler = StandardScaler() —> 11 X_train_scaled = scaler.fit_transform(X_train) 12 X_test_scaled = scaler.transform(X_test) 14 # モデルの作成 File /home/share/temp/venv/lib/python3.8/site-packages/sklearn/utils/_set_output.py:157, in _wrap_method_output.<locals>.wrapped(self, X, *args, **kwargs) 155 @wraps(f) 156 def wrapped(self, X, *args, **kwargs): –> 157 data_to_wrap = f(self, X, *args, **kwargs) 158 if isinstance(data_to_wrap, tuple): 159 # only wrap the first output for cross decomposition 160 return_tuple = ( 161 _wrap_data_with_container(method, data_to_wrap[0], X, self), 162 *data_to_wrap[1:], 163 ) File /home/share/temp/venv/lib/python3.8/site-packages/sklearn/base.py:916, in TransformerMixin.fit_transform(self, X, y, **fit_params) 912 # non-optimized default implementation; override when a better 913 # method is possible for a given clustering algorithm 914 if y is None: 915 # fit method of arity 1 (unsupervised transformation) … 909 ) 911 if self.with_std: 912 # First pass ValueError: Cannot center sparse matrices: pass `with_mean=False` instead. See docstring for motivation and alternatives.

エラーメッセージから推測するに、StandardScalerは疎行列(sparse matrix)に対して平均を求めることができません。そのため、with_mean=Falseを指定する必要があります。

修正したコードは以下の通りです:

python
# データの標準化 scaler = StandardScaler(with_mean=False) X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test)

これにより、疎行列に対する標準化が正しく行われるはずです。

未分類

Posted by ぼっち