I have a pandas data frame with about Million rows and 3 columns. The columns are of 3 different datatypes. NumberOfFollowers is of a numerical datatype, UserName is of a categorical data type, Embeddings is of categorical-set type.
df:
Index NumberOfFollowers UserName Embeddings Target Variable
0 15 name1 [0.5 0.3 0.2] 0
1 4 name2 [0.4 0.2 0.4] 1
2 8 name3 [0.5 0.5 0.0] 0
3 10 name1 [0.1 0.0 0.9] 0
... ... .... ... ..
I would like to convert this pandas data frame into the LibSVM input format.
Desired Output:
0 0:15 4:1 1:0.5 2:0.3 3:0.2
1 0:4 5:1 1:0.4 2:0.2 3:0.4
0 0:8 6:1 1:0.5 2:0.5 3:0.0
0 0:10 4:1 1:0.1 2:0.0 3:0.9
...
One solution I found was using:
https://scikit-learn.org/stable/modules/generated/sklearn.datasets.dump_svmlight_file.html
It takes input as a NumPy array or a sparse matrix.
The UserName has a million unique values, so calling pd.get_dummies on this column and storing it as a dense NumPy array is not a solution and will not fit in the memory.
So, I know it may be done using sparse matrices, but, then I don't know how to convert the above data with mixed data types into a sparse matrix and then use sklearn.datasets.dump_svmlight_file.html.
In reality, I have many columns with mixed data types and I need to convert them into libSVM format. But, all the columns fall in one of the above three types.
Thanks in advance for any thoughts on how to solve the above problem.