将混合数据类型的Pandas Dataframe转换为LibSVM格式。

问题描述 投票:-1回答:1

我有一个pandas数据框架,有大约百万行和3列。 这些列有3种不同的数据类型。NumberOfFollowers是数值数据类型,UserName是分类数据类型,Embeddings是分类设置类型。

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
...      ...                               ....                       ...                 ..

我想把这个pandas数据框转换为LibSVM的输入格式。

希望的输出。

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
...

我找到的一个解决方案是使用:

https:/scikit-learn.orgstablemodulesgeneratedsklearn.datasets.dump_svmlight_file.html。

它以NumPy数组或稀疏矩阵的形式接受输入。

UserName有100万个唯一值,所以,在这一列上调用pd.get_dummies,并将其存储为一个密集的NumPy数组,这不是一个解决方案,而且在内存中也不合适。

所以,我知道可能会用稀疏矩阵来完成,但是,我就不知道如何将上述混合数据类型的数据转换为稀疏矩阵,然后使用sklearn.datasets.dump_svmlight_file.html。

实际上,我有很多数据类型混合的列,我需要把它们转换成libSVM格式。但是,所有的列都属于上述三种类型之一。

先谢谢大家对如何解决上述问题的看法。

pandas scikit-learn sparse-matrix libsvm
1个回答
0
投票

我自己写了这个函数。它可以工作,但是,真的很慢。欢迎提供任何更快的解决方案。

def construct_line(row, columns, dtypes, string_dict, file_obj, index, time1):

    if index % 1000000 == 0:
            print(str(time.time() - time1) +": "+ str(index))

    global categorical_index
    numerical_index = 0
    new_line = str(row[-1])+ " "
    for i in range(len(columns) - 1):
            value_at_i = row[i]
            dtype_at_i = dtypes[i]
            column_name_at_i = columns[i]

            if ((dtype_at_i == object) or (dtype_at_i == bool)): # Categorical Features
                    if (dtype_at_i == bool):
                            value_at_i =  bool_to_str(value_at_i)

                    value_to_be_found = column_name_at_i +"_"+ value_at_i

                    if value_to_be_found in string_dict:
                            indexed_value = string_dict[value_to_be_found]
                            new_line = new_line + str(indexed_value)+":1 "

                    else:
                            indexed_value = categorical_index
                            categorical_index = categorical_index + 1
                            new_line = new_line + str(indexed_value)+":1 "
                            string_dict[value_to_be_found] = indexed_value

            else: # Numerical Features
                    new_line = new_line + str(numerical_index) + ":" +str(value_at_i)+" "
                    numerical_index = numerical_index + 1
    file_obj.write(new_line+"\n")

cnames_numerical = list(train_df.select_dtypes(exclude=['object', 'bool']).columns)
categorical_index = len(cnames_numerical) - 1 # Categorical indices start from here.
string_dict = {}
f_train_df = open("train_df.csv", 'a')
print(len(train_df))
time1 = time.time()
train_df.apply(lambda x : construct_line(x, train_df.columns, train_df.dtypes, string_dict, f_train_df, x.name,\
                                         time1), axis = 1)
f_train_df.close()
print(len(test_df))
f_test_df = open("test_df.csv", 'a')
test_df.apply(lambda x : construct_line(x, test_df.columns, test_df.dtypes, string_dict, f_test_df, x.name,\
                                       time1), axis = 1)
f_test_df.close()
© www.soinside.com 2019 - 2024. All rights reserved.