Skip to main content
 首页 » 编程设计

python之dtype 的值太大 ('float64' ) sklearn.preprocessing .StandardScaler()

2025年02月15日7luoye11

当我尝试在 python 中执行此操作时:

from sklearn.preprocessing import StandardScaler 
sc = StandardScaler() 
X_train = sc.fit_transform(X_train) 

我收到这个错误:

ValueError: Input contains NaN, infinity or a value too large for dtype('float64')

我知道有些帖子有相同的标题,但大多数时候问题是因为数据是 NaN,这不是我的情况,当我尝试下面的函数时,它会判断是否存在 NaN 或无穷大数:

import numpy 
numpy.isfinite(X_train).all() 

其中 X_train 是我的 float 组
( https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.isfinite.html )

这是 X_train 中的数据类型。也许这些数据的范围从大约 -15 到 4000 太宽以至于无法进行缩放?

  4068.725 
  4035.808 
  4067.000 
  4051.697 
412761.343 
   101.905 
  4050.460 
  4067.000 
   -15.303 
     1.099 
    52.363 
    56.739 
    68.997 
    72.410 
    62.171 
  4052.077 
  4033.867 
    33.385 
  4050.690 
  4031.547 
    19.143 
    13.494 
    11.298 
    43.261 

请您参考如下方法:

我的错,我以为

numpy.isfinite(X_train).all() 

如果所有的值都是有限的而不是 NaN,应该返回 True,但它是 false。 找到偷偷摸摸的值的好方法太执行下面的代码了:

numpy.argwhere(numpy.isnan(X_train)) 

(我在数组中发现了一些 NaN)。 感谢您顺便纠正我的问题,user3666197,我在这里退出了新手。