stanfordmlgroup / ngboost

Natural Gradient Boosting for Probabilistic Prediction
Apache License 2.0
1.65k stars 215 forks source link

why sometimes get "LInAlgError:Singular matrix" error? #67

Closed YeoungJe closed 4 years ago

YeoungJe commented 4 years ago

model = NGBClassifier(Base=default_tree_learner, Dist=Bernoulli, Score=MLE, natural_gradient=True, verbose=False,n_estimators = 500)

Here is error


` 226 # fitting --> 227 model.fit(train_arx,train_ary) 228 if return_proba : 229 predict_value = model.predict_proba(test_arx)

~/anaconda3/lib/python3.6/site-packages/ngboost/ngboost.py in fit(self, X, Y, X_val, Y_val, sample_weight, val_sample_weight, train_loss_monitor, val_loss_monitor, early_stopping_rounds) 119 loss_list += [train_loss_monitor(D, Y_batch)] 120 loss = loss_list[-1] --> 121 grads = S.grad(D, Y_batch, natural=self.natural_gradient) 122 123 proj_grad = self.fit_base(X_batch, grads, sample_weight)

~/anaconda3/lib/python3.6/site-packages/ngboost/scores.py in grad(forecast, Y, natural) 13 grad = forecast.D_nll(Y) 14 if natural: ---> 15 grad = np.linalg.solve(fisher, grad) 16 return grad 17

<__array_function__ internals> in solve(*args, **kwargs) ~/anaconda3/lib/python3.6/site-packages/numpy/linalg/linalg.py in solve(a, b) 401 signature = 'DD->D' if isComplexType(t) else 'dd->d' 402 extobj = get_linalg_error_extobj(_raise_linalgerror_singular) --> 403 r = gufunc(a, b, signature=signature, extobj=extobj) 404 405 return wrap(r.astype(result_t, copy=False)) ~/anaconda3/lib/python3.6/site-packages/numpy/linalg/linalg.py in _raise_linalgerror_singular(err, flag) 95 96 def _raise_linalgerror_singular(err, flag): ---> 97 raise LinAlgError("Singular matrix") 98 99 def _raise_linalgerror_nonposdef(err, flag): LinAlgError: Singular matrix` --------------------------------------------------------------------------------------------------------------------------------------- On the same data, sometimes works fine but sometimes occur errors. What should I do when this error occurs?
alejandroschuler commented 4 years ago

Hi @YeoungJe , I'm not sure exactly what is causing the error withpppout being able to reproduce it myself- is it possible for you to share the data with me?

Something that should "fix" the problem is setting natural_gradient=False in the NGBClassifier constructor, but this is more of a workaround than a fix and I still want to know what it is about the data that causes the problem.

alejandroschuler commented 4 years ago

Hi @YeoungJe I'm going to close this issue for now but please get in touch again if you are still having problems and we can try and help if you provide more information.

astrogilda commented 4 years ago

I am having the exact same issue! @alejandroschuler any thoughts on this?

qq492947833 commented 1 year ago

I have the same problem! This seems to be due to the use of inappropriate parameters! Such as larger learning rates, smaller max depths, and so on.