使用sklearn的pca模块fit数据报错“ValueError: array must not contain infs or NaNs”
使用sklearn的pca模块fit数据报错“ValueError: array must not contain infs or NaNs”:
Traceback (most recent call last): File "xxx.py", line 57, in <module> pca.fit(train_X) File "C:\Users\xyj\AppData\Roaming\Python\Python37\site-packages\sklearn\decomposition\_pca.py", line 351, in fit self._fit(X) File "C:\Users\xyj\AppData\Roaming\Python\Python37\site-packages\sklearn\decomposition\_pca.py", line 425, in _fit return self._fit_truncated(X, n_components, self._fit_svd_solver) File "C:\Users\xyj\AppData\Roaming\Python\Python37\site-packages\sklearn\decomposition\_pca.py", line 545, in _fit_truncated random_state=random_state) File "C:\Users\xyj\AppData\Roaming\Python\Python37\site-packages\sklearn\utils\validation.py", line 73, in inner_f return f(**kwargs) File "C:\Users\xyj\AppData\Roaming\Python\Python37\site-packages\sklearn\utils\extmath.py", line 358, in randomized_svd Uhat, s, V = linalg.svd(B, full_matrices=False) File "X:\anaconda\lib\site-packages\scipy\linalg\decomp_svd.py", line 109, in svd a1 = _asarray_validated(a, check_finite=check_finite) File "X:\anaconda\lib\site-packages\scipy\_lib\_util.py", line 239, in _asarray_validated a = toarray(a) File "C:\Users\xyj\AppData\Roaming\Python\Python37\site-packages\numpy\lib\function_base.py", line 499, in asarray_chkfinite "array must not contain infs or NaNs") ValueError: array must not contain infs or NaNs
参考github对应库的issues进行解决,https://github.com/scikit-learn/scikit-learn/issues/18138,第一次fit在try里面运行,第二次就成功fit。:
try: pca.fit(train_X) except: pca.fit(train_X)
至于原因。。。没时间看了