Description
Description
I am trying to calculate the error at each step for GradientBoostingClassifier with loss_ function object but I am getting the error below:
operands could not be broadcast together with shapes (2947,6) (2947,)
I checked the source code and it appears that while function input expects both input with the same size (n_samples,) then it converts the actual labels to one-hot encoding which causes the error. In my case I have 6 different classes and y_test and y_pred both have the shape of (2947, ).
Steps/Code to Reproduce
GBC = GradientBoostingClassifier(subsample=0.5, max_features=4, random_state=42)
GBC.fit(X, y)
for y_pred in GBC.staged_predict(X_test):
print(y_test.shape, y_pred.shape)
GBC.loss_(y_test, y_pred)
Actual Results
(2947,) (2947,)
ValueError Traceback (most recent call last)
<ipython-input-41-0761b83ba8e1> in <module>
1 for y_pred in GBC.staged_predict(X_test):
2 print(y_test.shape, y_pred.shape)
----> 3 GBC.loss_(y_test, y_pred)
~/anaconda3/lib/python3.7/site-packages/sklearn/ensemble/gradient_boosting.py in __call__(self, y, pred, sample_weight)
894
895 if sample_weight is None:
--> 896 return np.sum(-1 * (Y * pred).sum(axis=1) +
897 logsumexp(pred, axis=1))
898 else:
ValueError: operands could not be broadcast together with shapes (2947,6) (2947,)
and this is the actual piece of code (where the error happens)
def __call__(self, y, pred, sample_weight=None):
"""Compute the Multinomial deviance.
Parameters
----------
y : array, shape (n_samples,)
True labels
pred : array, shape (n_samples,)
Predicted labels
sample_weight : array-like, shape (n_samples,), optional
Sample weights.
"""
# create one-hot label encoding
Y = np.zeros((y.shape[0], self.K), dtype=np.float64)
for k in range(self.K):
Y[:, k] = y == k
if sample_weight is None:
return np.sum(-1 * (Y * pred).sum(axis=1) +
logsumexp(pred, axis=1))
else:
return np.sum(-1 * sample_weight * (Y * pred).sum(axis=1) +
logsumexp(pred, axis=1))
Versions
System:
python: 3.7.4 (default, Aug 13 2019, 20:35:49) [GCC 7.3.0]
executable: /home/fazil/anaconda3/bin/python
machine: Linux-4.15.0-65-generic-x86_64-with-debian-buster-sid
BLAS:
macros: SCIPY_MKL_H=None, HAVE_CBLAS=None
lib_dirs: /home/fazil/anaconda3/lib
cblas_libs: mkl_rt, pthread
Python deps:
pip: 19.2.3
setuptools: 41.2.0
sklearn: 0.20.1
4DA5
numpy: 1.15.4
scipy: 1.1.0
Cython: 0.29.2
pandas: 0.23.4