Python:当我导入RandomForestClassifier时,“TypeError:无法使用块值”

时间:2015-02-11 11:41:45

标签: python pandas machine-learning scikit-learn random-forest

我正在用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

Stack Trace如下:

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

1 个答案:

答案 0 :(得分:0)

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