簡體   English   中英

fit_transform中的錯誤:輸入包含NaN,無窮大或對於dtype('float64')而言太大的值

[英]Error in fit_transform: Input contains NaN, infinity or a value too large for dtype('float64')

我有一個形狀為(14407,2564)的數據框。 我正在嘗試使用VarianceThreshold函數刪除低方差特征。 但是,當我調用fit_transform時,出現以下錯誤:

ValueError:輸入包含NaN,無窮大或dtype('float64')太大的值。

在使用Uign VarianceThreshold之前,我使用以下代碼替換了df中所有缺少的值:

    df.replace('null',np.NaN, inplace=True)
    df.replace(r'^\s*$', np.NaN, regex=True, inplace=True)
    df.fillna(value=df.median(), inplace=True)

之后,我使用以下方法檢查了數據框是否有任何空/無限值:

    m = df.isnull().any()
    print "========= COLUMNS WITH NULL VALUES ================="
    print m[m]
    print "========= COLUMNS WITH INFINITE VALUES ================="
    m = np.isfinite(df.select_dtypes(include=['float64'])).any()
    print m[m]

並且我得到一個空的Series作為輸出,這意味着我所有的列都沒有缺失值。 輸出為:

    ========= COLUMNS WITH NULL VALUES =================
    Series([], dtype: bool)
    ========= COLUMNS WITH INFINITE VALUES =================
    Series([], dtype: bool)

完整的錯誤跟蹤:

    Traceback (most recent call last):
      File "/home/users/MyUsername/MyProject/src/main/python/Main.py", line 222, in <module>
        main()
      File "/home/users/MyUsername/MyProject/src/main/python/Main.py", line 218, in         main
        getAllData()
      File "/home/users/MyUsername/MyProject/src/main/python/Main.py", line 95, in getAllData
        predictors, labels, dropped_features = fselector.process(variance=True, corr=True, bestf=True, bestfk=200)
      File         "/home/users/MyUsername/MyProject/src/main/python/classes/featureselector.py", line 54, in process
        self.getVariance(threshold=(.95 * (1 - .95)))
      File "/home/users/MyUsername/MyProject/src/main/python/classes/featureselector.py", line 136, in getVariance
        self.removeLowVarianceColumns(df=self.X, thresh=threshold)
      File "/home/users/MyUsername/MyProject/src/main/python/classes/featureselector.py", line 213, in removeLowVarianceColumns
        selector.fit_transform(df)
      File "/usr/lib64/python2.7/site-packages/sklearn/base.py", line 494, in fit_transform
        return self.fit(X, **fit_params).transform(X)
      File "/usr/lib64/python2.7/site-packages/sklearn/feature_selection/variance_threshold.py", line 64, in fit
        X = check_array(X, ('csr', 'csc'), dtype=np.float64)
    File "/usr/lib64/python2.7/site-packages/sklearn/utils/validation.py", line 407, in check_array
        _assert_all_finite(array)
    File "/usr/lib64/python2.7/site-packages/sklearn/utils/validation.py", line 58, in _assert_all_finite
    " or a value too large for %r." % X.dtype)
    ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

因此,我不確定要檢查什么,這似乎不是缺少值的問題,但是我也無法獲取導致問題的列/值。

我在這里看到幾個線程都以缺少值結尾,但這似乎不是問題所在。

我通過將數據轉換為數字來解決此問題。 看起來,盡管錯誤消息顯示為“ float64”,但我的數據僅是所有對象,而對象與fit_transform不能很好地配合使用。

使用df = df.apply(lambda x: pd.to_numeric(x,errors='ignore'))將我的數據更改為浮動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM