![](/img/trans.png)
[英]Scikit-learn : Input contains NaN, infinity or a value too large for dtype ('float64')
[英]ValueError: Input contains NaN, infinity or a value too large for dtype('float64') with scikit-learn
from sklearn.cluster.bicluster import SpectralCoclustering
from sklearn.feature_extraction.text import TfidfVectorizer
def number_normalizer(tokens):
""" Map all numeric tokens to a placeholder.
For many applications, tokens that begin with a number are not directly
useful, but the fact that such a token exists can be relevant. By applying
this form of dimensionality reduction, some methods may perform better.
"""
return ("#NUMBER" if token[0].isdigit() else token for token in tokens)
class NumberNormalizingVectorizer(TfidfVectorizer):
def build_tokenizer(self):
tokenize = super(NumberNormalizingVectorizer, self).build_tokenizer()
return lambda doc: list(number_normalizer(tokenize(doc)))
vectorizer = NumberNormalizingVectorizer(stop_words='english', min_df=5)
cocluster = SpectralCoclustering(n_clusters=5, svd_method='arpack', random_state=0)
X = vectorizer.fit_transform(data)
cocluster.fit(X)
我選擇 SpectralCoclustering 對大約 30k 條推文進行聚類,在將數據 X 放入“cocluster”之前一切都很順利。
它引發錯誤如下所示。
.env/lib/python3.5/site-packages/sklearn/utils/validation.py", line 43, 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').
當我輸入代碼作為錯誤報告時,但它是“錯誤”。 發生錯誤時它應該是 True ,對嗎?
那么還有什么可以找到錯誤的嗎? 謝謝!
https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/utils/validation.py#L43
X.dtype.char in np.typecodes['AllFloat'] and not np.isfinite(X.sum()) and not np.isfinite(X).all()
錯誤的
當我今天遇到另一個sklearn
-module 的類似問題時,以下故障排除幫助了我:
inf
/ NaN
值。 如果有的話,用常量替換它們。 例如,將inf
替換為LARGE_NUMBER
。LARGE_NUMBER
更小。 如果您的實際數據范圍在 -100 和 100 之間,則 10^100 和 10^10 之間的差異可能不太重要。LARGE_NUMBER
小,然后錯誤就消失了。) 我想這種行為(在我的情況下)是因為sklearn
模塊有時使用指數函數。 因此,您的方法可能會調用另一個函數,其中一個參數是inf
,盡管您的輸入不包含此類值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.