sklearn's PLSRegression: “ValueError: array must not contain infs or NaNs”

笑着哭i 提交于 2019-12-05 08:52:53
eickenberg

Please check if any of your values being passed in are NaN or inf:

np.isnan(xx).any()
np.isnan(yy).any()

np.isinf(xx).any()
np.isinf(yy).any()

If any of those yields true. Remove the nan entries or inf entries. E.g. you can set them to 0 with:

xx = np.nan_to_num(xx)
yy = np.nan_to_num(yy)

It's also possible for numpy to be fed such large positive and negative and zeroed values, that the equations deep down in the library are producing zeros, Nan's or Inf's. One workaround, oddly enough, is to send in smaller numbers (say representative numbers between -1 and 1. One way to do this is by standardization, see: https://stackoverflow.com/a/36390482/445131

If none of that solves the problem, then you may be dealing with a low level bug in the library your using, or some sort of singularity in your data. Create an sscce and post it to stackoverflow or create a new bug report on the library maintaining your software.

The issue is caused by a bug in scikit-learn. I reported it on GitHub: https://github.com/scikit-learn/scikit-learn/issues/2089#issuecomment-152753095

I can reproduce the same bug, I silenced this bug by filtering all 0s away

threshold_for_bug = 0.00000001 # could be any value, ex numpy.min
xx[xx < threshold_for_bug] = threshold_for_bug

This silences the bug (i never check the precision difference)

My system info:

numpy-1.11.2
python-3.5
macOS Sierra

You may want to check your weights for negative values, since this error will also be triggered with negative weights.

I found a tricky little solution that worked for me.

I was doing time series featurization through cesium with this code:

timeInput = np.array(timeData)
valueInput = np.array(data)

#Featurizing Data
featurizedData = featurize.featurize_time_series(times=timeInput,
                                                     values=valueInput,
                                                     errors=None,
                                                     features_to_use=featuresToUse)

which was resulting in this error:

ValueError: array must not contain infs or NaNs

for laughs, I checked the lengths and types of the data:

data:
70
<class 'numpy.int32'>

timeData: 
70
<class 'numpy.float64'>

which made sense, because my times were calculated from delta data in ms.

I decided I'd try to convert data types with this one line of code:

valueInput = valueInput.astype(float)

and it worked, resulting in this code:

timeInput = np.array(timeData)
valueInput = np.array(data)
valueInput = valueInput.astype(float)

#Featurizing Data
try:
    featurizedData = featurize.featurize_time_series(times=timeInput,
                                                     values=valueInput,
                                                     errors=None,
                                                     features_to_use=featuresToUse)

if you're getting an error like this, give matching datatypes a shot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!