【发布时间】:2016-01-26 13:26:15
【问题描述】:
我使用的是 Python 3.5,并且安装并导入了 NumPy、SciPy 和 matplotlib。
当我尝试时:
# Import the random forest package
from sklearn.ensemble import RandomForestClassifier
# Create the random forest object which will include all the parameters
# for the fit
forest = RandomForestClassifier(n_estimators = 1)
# Fit the training data to the Survived labels and create the decision trees
forest = forest.fit(train_data[0::,1::],train_data[0::,0])
# Take the same decision trees and run it on the test data
output = forest.predict(test_data)
(test_data 和 train_data 都是浮点数组) 我收到以下错误:
C:\Users\Uri\AppData\Local\Programs\Python\Python35-32\lib\site-packages\sklearn\utils\fixes.py:64: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead
if 'order' in inspect.getargspec(np.copy)[0]:
C:\Users\Uri\AppData\Local\Programs\Python\Python35-32\lib\site-packages\sklearn\base.py:175: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead
args, varargs, kw, default = inspect.getargspec(init)
C:\Users\Uri\AppData\Local\Programs\Python\Python35-32\lib\site-packages\sklearn\base.py:175: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead
args, varargs, kw, default = inspect.getargspec(init)
C:\Users\Uri\AppData\Local\Programs\Python\Python35-32\lib\site-packages\sklearn\base.py:175: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead
args, varargs, kw, default = inspect.getargspec(init)
C:\Users\Uri\AppData\Local\Programs\Python\Python35-32\lib\site-packages\sklearn\base.py:175: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead
args, varargs, kw, default = inspect.getargspec(init)
Traceback (most recent call last):
File "C:/Users/Uri/PycharmProjects/titanic1/fdsg.py", line 54, in <module>
output = forest.predict(test_data)
File "C:\Users\Uri\AppData\Local\Programs\Python\Python35-32\lib\site-packages\sklearn\ensemble\forest.py", line 461, in predict
X = check_array(X, ensure_2d=False, accept_sparse="csr")
File "C:\Users\Uri\AppData\Local\Programs\Python\Python35-32\lib\site-packages\sklearn\utils\validation.py", line 352, in check_array
_assert_all_finite(array)
File "C:\Users\Uri\AppData\Local\Programs\Python\Python35-32\lib\site-packages\sklearn\utils\validation.py", line 52, 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').
Process finished with exit code 1
【问题讨论】:
-
不导入
RandomForrestClassifier就好了吗? -
似乎是警告,而不是错误。当某些库使用不推荐使用的 numpy 函数时,我会经常遇到此类警告。或者您可能没有提供完整的堆栈跟踪。
-
没错,如果它进口,你应该没问题。不要担心太多。
-
按
edit将代码添加到问题正文中,而不是在 cmets 中。我现在在您的评论中添加了代码,如有必要,请更新它。您需要指定错误是否不是 DeprecationWarning(这是一个 Warning)。 -
ValueError: Input contains NaN, infinity or a value too large for dtype('float64').告诉您数据中有无效值。
标签: python scikit-learn random-forest