Repository navigation
Closed
Description
Description
ValueError: Buffer dtype mismatch, expected 'double' but got 'float' when calling fit on GradientBoostingRegressor with init = Ridge
Steps/Code to Reproduce
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.linear_model import Ridge
base_model_params = {'alpha' : 0.01,
'fit_intercept' :True,
'normalize' : True,
'copy_X' : True,
'max_iter' : None,
'tol' : 0.01,
'solver' : 'sag'}
base_model = Ridge(**base_model_params)
gb_params = {'loss' : 'huber',
'criterion' : 'friedman_mse',
'learning_rate' : 0.05,
'max_depth' : 5,
'max_features' : 'sqrt',
'n_estimators' : 100,
'init' : base_model
}
model = GradientBoostingRegressor(**gb_params)
model.fit(X,Y)Expected Results
The model fits
Actual Results
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<timed exec> in <module>()
~/anaconda/envs/python3/lib/python3.6/site-packages/sklearn/ensemble/gradient_boosting.py in fit(self, X, y, sample_weight, monitor)
996
997 # fit initial model - FIXME make sample_weight optional
--> 998 self.init_.fit(X, y, sample_weight)
999
1000 # init predictions
~/anaconda/envs/python3/lib/python3.6/site-packages/sklearn/linear_model/ridge.py in fit(self, X, y, sample_weight)
663 self : returns an instance of self.
664 """
--> 665 return super(Ridge, self).fit(X, y, sample_weight=sample_weight)
666
667
~/anaconda/envs/python3/lib/python3.6/site-packages/sklearn/linear_model/ridge.py in fit(self, X, y, sample_weight)
504 max_iter=self.max_iter, tol=self.tol, solver=self.solver,
505 random_state=self.random_state, return_n_iter=True,
--> 506 return_intercept=False)
507 self._set_intercept(X_offset, y_offset, X_scale)
508
~/anaconda/envs/python3/lib/python3.6/site-packages/sklearn/linear_model/ridge.py in ridge_regression(X, y, alpha, sample_weight, solver, max_iter, tol, verbose, random_state, return_n_iter, return_intercept)
425 max_iter, tol, verbose, random_state, False, max_squared_sum,
426 init,
--> 427 is_saga=solver == 'saga')
428 if return_intercept:
429 coef[i] = coef_[:-1]
~/anaconda/envs/python3/lib/python3.6/site-packages/sklearn/linear_model/sag.py in sag_solver(X, y, sample_weight, loss, alpha, beta, max_iter, tol, verbose, random_state, check_input, max_squared_sum, warm_start_mem, is_saga)
295 num_seen_init = 0
296
--> 297 dataset, intercept_decay = make_dataset(X, y, sample_weight, random_state)
298
299 if max_squared_sum is None:
~/anaconda/envs/python3/lib/python3.6/site-packages/sklearn/linear_model/base.py in make_dataset(X, y, sample_weight, random_state)
62 intercept_decay = SPARSE_INTERCEPT_DECAY
63 else:
---> 64 dataset = ArrayDataset(X, y, sample_weight, seed=seed)
65 intercept_decay = 1.0
66
sklearn/utils/seq_dataset.pyx in sklearn.utils.seq_dataset.ArrayDataset.__cinit__()
ValueError: Buffer dtype mismatch, expected 'double' but got 'float'
Versions
Darwin-15.0.0-x86_64-i386-64bit
Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 13:14:59)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
NumPy 1.12.1
SciPy 0.19.1
Scikit-Learn 0.19.0
Metadata
Metadata
Assignees
Labels
No labels
Activity
jnothman commentedon Dec 13, 2017
You should be using scikit-learn 0.19.1. You also should provide some
Xandy(fine if they're generated) for which this occurs. What type and what dtype, at least, is your data?chorus12 commentedon Dec 13, 2017
Hi. Will provide the data in a shortwhile but as for now dtype is float.
jnothman commentedon Dec 13, 2017
chorus12 commentedon Dec 13, 2017
float64
XY.zip
import pickle
with open('X.pkl','br') as file:
X = pickle.load(file)
with open('Y.pkl','br') as file:
Y = pickle.load(file)
lesteve commentedon Dec 13, 2017
I can reproduce here is a stand-alone snippet:
The problem is that
sample_weightsis float32. The float32 sample_weights comes from GradientBoosting.fit:scikit-learn/sklearn/ensemble/gradient_boosting.py
Line 1000 in 08ff565
Note that I could not get the snippet to run even passing float64 weights into
model.fit. Maybe there is a deeper problem here. Here is the stack-trace withmodel.fit(X, y, sample_weight=np.ones(200, dtype='float64')):Details
glemaitre commentedon Dec 13, 2017
y_pred[:, k]is the issue sincey_predis of shape (n,) andkis the index of the estimator being updated. I'll check a bit more later on.Edit:
What I mean is that
y_predis 1D and indexed as a 2D array.glemaitre commentedon Dec 13, 2017
Actually y_pred is expected to be 2 d while predict from ridge is 1 d
jeremiedbb commentedon Mar 2, 2020
Fixed by #12983. Closing.