1
votes

I have a train pandas df with 20 million rows and a test pandas df with around 10 million rows.

There are columns in both of the df's that I want to apply LabelEncoder() to, but I keep getting a Memory Error on my laptop and even on a 64 gig RAM AWS instance.

Is there a way I can deal with this in chunks without losing the mapping?

Here is the code I was using:

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()

for col in cols_to_encode:
    le.fit(list(train[col])+list(test[col]))
    train[col] = le.transform(train[col])
    test[col] = le.transform(test[col])

I sampled 500,000 rows from each and was able to run the code with no error, so I know it's not a syntax error or something.

Any help would be greatly appreciated.

1

1 Answers

1
votes

I have not used LabelEncoder before, but from my work with Sklearn, I know that there are options that can help parallelize. Have you tried looking into parallelizing this task? Either using a parameter like n_jobs which many sklearn classifiers have, or even the python multiprocessing library.