如何使用Patsy的API准备大型数据集?

时间:2014-10-22 06:37:45

标签: python data-processing patsy

我正在进行逻辑回归,并且在使用Patsy的API来准备大于小样本的数据时遇到问题。

直接在DataFrame上使用dmatrices函数,我遇到了这个突然的错误(请注意,在我的笔记本电脑上遇到这个问题后,我用300GB的RAM启动了EC2,并得到了同样的错误):

Traceback (most recent call last):
File "My_File.py", line 22, in <module>
   df, return_type="dataframe")
File "/root/anaconda/lib/python2.7/site-packages/patsy/highlevel.py", line 297, in dmatrices
 NA_action, return_type)
File "/root/anaconda/lib/python2.7/site-packages/patsy/highlevel.py", line 156, in do_highlevel_design
return_type=return_type)
File "/root/anaconda/lib/python2.7/site-packages/patsy/build.py", line 989, in build_design_matrices
results.append(builder._build(evaluator_to_values, dtype))
File "/root/anaconda/lib/python2.7/site-packages/patsy/build.py", line 821, in _build
m = DesignMatrix(np.empty((num_rows, self.total_columns), dtype=dtype),
MemoryError

所以,我梳理了Patsy's docs并找到了这个宝石:

patsy.incr_dbuilder(formula_like, data_iter_maker, eval_env=0)
    Construct a design matrix builder incrementally from a large data set.

但是,该方法记录很少,源代码很大程度上没有注释。

我已经到了这个代码:

def iter_maker():
    with open("test.tsv", "r") as f:
        reader = csv.DictReader(f, delimiter="\t")
        for row in reader:
            yield(row)


y, dta = incr_dbuilders("s ~ C(x) + C(y):C(rgh) + \
C(z):C(f) + C(r):C(p) + C(q):C(w) + \
C(zr):C(rt) + C(ff):C(djjj) + C(hh):C(tt) + \
C(bb):lat + C(jj):lng + C(ee):C(bb) + C(qq):C(uu)",
        iter_maker)

df = dmatrix(dta, {}, 0, "drop", return_type="dataframe")

但我收到PatsyError: Error evaluating factor: NameError: name 'ff' is not defined

抛出这个是因为_try_incr_builders(从dmatrix调用)返回None on line 151 of highlevel.py

使用这些Patsy函数准备数据的正确方法是什么?您可能拥有的任何示例或指导都会有所帮助。

0 个答案:

没有答案