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

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

使用sklearn.cross_decomposition.PLSRegression时:

import numpy as np
import sklearn.cross_decomposition

pls2 = sklearn.cross_decomposition.PLSRegression()
xx = np.random.random((5,5))
yy = np.zeros((5,5) ) 

yy[0,:] = [0,1,0,0,0]
yy[1,:] = [0,0,0,1,0]
yy[2,:] = [0,0,0,0,1]
#yy[3,:] = [1,0,0,0,0] # Uncommenting this line solves the issue

pls2.fit(xx, yy)

我得到:

C:\Anaconda\lib\site-packages\sklearn\cross_decomposition\pls_.py:44: RuntimeWarning: invalid value encountered in divide
  x_weights = np.dot(X.T, y_score) / np.dot(y_score.T, y_score)
C:\Anaconda\lib\site-packages\sklearn\cross_decomposition\pls_.py:64: RuntimeWarning: invalid value encountered in less
  if np.dot(x_weights_diff.T, x_weights_diff) < tol or Y.shape[1] == 1:
C:\Anaconda\lib\site-packages\sklearn\cross_decomposition\pls_.py:67: UserWarning: Maximum number of iterations reached
  warnings.warn('Maximum number of iterations reached')
C:\Anaconda\lib\site-packages\sklearn\cross_decomposition\pls_.py:297: RuntimeWarning: invalid value encountered in less
  if np.dot(x_scores.T, x_scores) < np.finfo(np.double).eps:
C:\Anaconda\lib\site-packages\sklearn\cross_decomposition\pls_.py:275: RuntimeWarning: invalid value encountered in less
  if np.all(np.dot(Yk.T, Yk) < np.finfo(np.double).eps):
Traceback (most recent call last):
  File "C:\svn\hw4\code\test_plsr2.py", line 8, in <module>
    pls2.fit(xx, yy)
  File "C:\Anaconda\lib\site-packages\sklearn\cross_decomposition\pls_.py", line 335, in fit
    linalg.pinv(np.dot(self.x_loadings_.T, self.x_weights_)))
  File "C:\Anaconda\lib\site-packages\scipy\linalg\basic.py", line 889, in pinv
    a = _asarray_validated(a, check_finite=check_finite)
  File "C:\Anaconda\lib\site-packages\scipy\_lib\_util.py", line 135, in _asarray_validated
    a = np.asarray_chkfinite(a)
  File "C:\Anaconda\lib\site-packages\numpy\lib\function_base.py", line 613, in asarray_chkfinite
    "array must not contain infs or NaNs")
ValueError: array must not contain infs or NaNs

可能是什么问题?

我知道 scikit-learn GitHub issue #2089,但由于我使用 scikit-learn 0.16.1(使用 Python 2.7.10 x64),这个问题应该得到解决(GitHub 问题工作正常)。

请检查您传入的任何值是否为 NaN 或 inf:

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

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

如果其中任何一个为真。删除 nan 条目或 inf 条目。例如。您可以将它们设置为 0

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

也有可能为 numpy 提供如此大的正负值和归零值,以至于库中深处的方程产生零,Nan 或 Inf。奇怪的是,一种解决方法是发送较小的数字(例如 -1 和 1 之间的代表性数字。一种方法是通过标准化来实现,请参阅:

如果 none 解决了问题,那么您可能正在处理您使用的库中的低级别错误,或者您的数据中存在某种奇点。创建一个 sscce 和 post 到 Whosebug 或在维护您的软件的库上创建一个新的错误报告。

此问题是由 scikit-learn 中的错误引起的。我在 GitHub 上报告了它:https://github.com/scikit-learn/scikit-learn/issues/2089#issuecomment-152753095

我可以重现相同的错误,我通过过滤掉所有 0 来消除这个错误

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

这消除了错误(我从不检查精度差异)

我的系统信息:

numpy-1.11.2
python-3.5
macOS Sierra

您可能需要检查权重是否为负值,因为负权重也会触发此错误。

我找到了一个对我有用的棘手的小解决方案。

我正在使用以下代码通过铯进行时间序列特征化:

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)

导致此错误的原因:

ValueError: array must not contain infs or NaNs

为了笑,我检查了数据的长度和类型:

data:
70
<class 'numpy.int32'>

timeData: 
70
<class 'numpy.float64'>

我决定尝试使用这一行代码来转换数据类型:

valueInput = valueInput.astype(float)

它成功了,产生了这个代码:

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)

如果您遇到这样的错误,请试一试匹配的数据类型