Stanislav Jirak
Stanislav Jirak

Reputation: 853

Can only use the '.sparse' accessor with Sparse data

I performed OHE on my data with sparse=True parameter - which doesn't seem doing anything?

I've try:

# One Hot Encoding
df_ohe = pd.get_dummies(df, columns=cats, drop_first=True, sparse=True)
df_ohe = df_ohe.sparse.to_coo().tocsr() #Explicitely convert
df_ohe.memory_usage().sum()

...which returns

AttributeError: Can only use the '.sparse' accessor with Sparse data.

Help would be appreciated. Thanks!

Upvotes: 1

Views: 1434

Answers (1)

Tom C
Tom C

Reputation: 372

You will want to import the csr_matrix method (which converts numpy arrays to a sparse matrix) using

from scipy.sparse import csr_matrix

You can then just write

df_ohe = pd.get_dummies(df, columns=cats, drop_first=True)
df_ohe = csr_matrix(df_ohe.values)

So note that here I removed the sparse=True from the get_dummies method and then changed the syntax for converting to a sparse matrix.

Upvotes: 1

Related Questions