Python:当我导入 RandomForestClassifier 时出现“TypeError:无法使用块值操作”

Posted

技术标签:

【中文标题】Python:当我导入 RandomForestClassifier 时出现“TypeError:无法使用块值操作”【英文标题】:Python: "TypeError: Could not operate with block values" coming when I import RandomForestClassifier 【发布时间】:2015-04-11 18:33:06 【问题描述】:

我正在用 python 编写一个数字识别程序。基本代码如下:

import pandas as pd
from sklearn.ensemble import RandomForestClassifier
filteredColumns = delete_useless_columns()
train = pd.read_csv('C:\\Users\\abchauhan\\Downloads\\train.csv')
trainData = train.loc[0:24998, filteredColumns]
target = train['label']
targetData = target[0:24999]
rf = RandomForestClassifier(n_estimators=150, min_samples_split=2, n_jobs=-1)
x = trainData/255 #Feature scaling
print('Fitting the data')
rf.fit(x, targetData)

特征缩放线给出错误:TypeError: Could not operate 255 with block values。现在,如果我删除RandomForestClassifier import 语句,特征缩放工作正常,但显然程序没有用。为什么部门在没有 import 语句的情况下工作?

编辑: trainData.info()如下:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 24999 entries, 0 to 24998
Columns: 708 entries, pixel12 to pixel779
dtypes: int64(708)
memory usage: 135.2 MB
None

堆栈跟踪如下:

Traceback (most recent call last):
 File "C:\Python34\lib\site-packages\pandas-0.15.2-py3.4-win32.egg\pandas\core\internals.py", line 965, in eval
result = get_result(other)
 File "C:\Python34\lib\site-packages\pandas-0.15.2-py3.4-win32.egg\pandas\core\internals.py", line 949, in get_result
return self._try_coerce_result(func(values, other))
 File "C:\Python34\lib\site-packages\pandas-0.15.2-py3.4-win32.egg\pandas\core\ops.py", line 765, in na_op
op, str_rep, x, y, raise_on_error=True, **eval_kwargs)
 File "C:\Python34\lib\site-packages\pandas-0.15.2-py3.4-win32.egg\pandas\computation\expressions.py", line 218, in evaluate
**eval_kwargs)
 File "C:\Python34\lib\site-packages\pandas-0.15.2-py3.4-win32.egg\pandas\computation\expressions.py", line 71, in _evaluate_standard
return op(a, b)
 MemoryError

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
 File        "C:/Users/abchauhan/PycharmProjects/DigitRecognition/PreProcess/RandomForest.py", line 20, in <module>
 x = trainData/255
 File "C:\Python34\lib\site-packages\pandas-0.15.2-py3.4-win32.egg\pandas\core\ops.py", line 831, in f
return self._combine_const(other, na_op)
 File "C:\Python34\lib\site-packages\pandas-0.15.2-py3.4-win32.egg\pandas\core\frame.py", line 3111, in _combine_const
new_data = self._data.eval(func=func, other=other, raise_on_error=raise_on_error)
 File "C:\Python34\lib\site-packages\pandas-0.15.2-py3.4-win32.egg\pandas\core\internals.py", line 2478, in eval
return self.apply('eval', **kwargs)
 File "C:\Python34\lib\site-packages\pandas-0.15.2-py3.4-win32.egg\pandas\core\internals.py", line 2457, in apply
applied = getattr(b, f)(**kwargs)
 File "C:\Python34\lib\site-packages\pandas-0.15.2-py3.4-win32.egg\pandas\core\internals.py", line 972, in eval
result = handle_error()
 File "C:\Python34\lib\site-packages\pandas-0.15.2-py3.4-win32.egg\pandas\core\internals.py", line 956, in handle_error
% (repr(other), str(detail)))
 TypeError: Could not operate 255 with block values 

 Process finished with exit code 1

【问题讨论】:

您能否发布数据以重现此错误,以及df.info()trainData.info() 的输出是什么 添加了 trainData.info()。 df 未使用,因此将其删除。数据是一个范围从 0 到 255 的数字矩阵。 您能发布一些重现此错误的代码吗? 我已经发布了代码。它在划分 trainData/255 时给出了 TypeError。如果我删除 RandomForestClassifier 的导入语句及其后续代码,则除法将顺利进行。 【参考方案1】:

这是一个内存错误,如堆栈跟踪中的第一个错误所示。

【讨论】:

以上是关于Python:当我导入 RandomForestClassifier 时出现“TypeError:无法使用块值操作”的主要内容,如果未能解决你的问题,请参考以下文章

Python:当我导入 RandomForestClassifier 时出现“TypeError:无法使用块值操作”

Python 和 matplotlib:当我尝试导入时,它发送一个无效的循环器构造错误

Ubuntu 上的 Python 2.6 导入包 (psyco)

导入的 python 模块覆盖选项解析器

Python spyder 调试因循环导入而冻结

Anaconda python已安装模块但导入失败