【问题标题】:How to use Scikit Learn dictvectorizer to get encoded dataframe from dense dataframe in Python?如何使用 Scikit Learn dictvectorizer 从 Python 中的密集数据帧中获取编码数据帧?
【发布时间】:2016-05-13 06:17:50
【问题描述】:

我有一个如下的数据框:

   user  item  affinity
0     1    13       0.1
1     2    11       0.4
2     3    14       0.9
3     4    12       1.0

据此,我想创建一个编码数据集(用于fastFM),如下所示:

  user1 user2 user4 user4 item11 item12 item13 item14 affinity
    1     0     0     0     0      0      1      0       0.1
    0     1     0     0     1      0      0      0       0.4
    0     0     1     0     0      0      0      1       0.9
    0     0     0     1     0      1      0      0       1.0

我需要来自sklearndictvectorizer 吗?如果是,那么有没有办法将原始数据帧转换为字典,可以将其提供给dictvectorizer,这又会给我编码的数据集,如图所示?

【问题讨论】:

    标签: python pandas scikit-learn encode dictvectorizer


    【解决方案1】:

    您可以将get_dummiesconcat 一起使用如果useritem 列中的值是数字,则由astype 转换为string

    df = pd.DataFrame({'item': {0: 13, 1: 11, 2: 14, 3: 12}, 
                       'affinity': {0: 0.1, 1: 0.4, 2: 0.9, 3: 1.0},
                       'user': {0: 1, 1: 2, 2: 3, 3: 4}},
                        columns=['user','item','affinity'])
    print df
       user  item  affinity
    0     1    13       0.1
    1     2    11       0.4
    2     3    14       0.9
    3     4    12       1.0
    
    df1 = df.user.astype(str).str.get_dummies()
    df1.columns = ['user' + str(x) for x in df1.columns]
    print df1
       user1  user2  user3  user4
    0      1      0      0      0
    1      0      1      0      0
    2      0      0      1      0
    3      0      0      0      1
    
    df2 = df.item.astype(str).str.get_dummies()
    df2.columns = ['item' + str(x) for x in df2.columns]
    print df2
       item11  item12  item13  item14
    0       0       0       1       0
    1       1       0       0       0
    2       0       0       0       1
    3       0       1       0       0
    
    print pd.concat([df1,df2, df.affinity], axis=1)
       user1  user2  user3  user4  item11  item12  item13  item14  affinity
    0      1      0      0      0       0       0       1       0       0.1
    1      0      1      0      0       1       0       0       0       0.4
    2      0      0      1      0       0       0       0       1       0.9
    3      0      0      0      1       0       1       0       0       1.0
    

    时间安排

    len(df) = 4:

    In [49]: %timeit pd.concat([df1,df2, df.affinity], axis=1)
    The slowest run took 4.91 times longer than the fastest. This could mean that an intermediate result is being cached 
    1000 loops, best of 3: 690 µs per loop
    

    len(df) = 40:

    df = pd.concat([df]*10).reset_index(drop=True)
    
    In [51]: %timeit pd.concat([df1,df2, df.affinity], axis=1)
    The slowest run took 5.56 times longer than the fastest. This could mean that an intermediate result is being cached 
    1000 loops, best of 3: 719 µs per loop
    

    len(df) = 400:

    df = pd.concat([df]*100).reset_index(drop=True)
    
    In [43]: %timeit pd.concat([df1,df2, df.affinity], axis=1)
    The slowest run took 4.55 times longer than the fastest. This could mean that an intermediate result is being cached 
    1000 loops, best of 3: 748 µs per loop
    

    len(df) = 4k:

    df = pd.concat([df]*1000).reset_index(drop=True)
    
    In [41]: %timeit pd.concat([df1,df2, df.affinity], axis=1)
    The slowest run took 4.67 times longer than the fastest. This could mean that an intermediate result is being cached 
    1000 loops, best of 3: 761 µs per loop
    

    len(df) = 40k:

    df = pd.concat([df]*10000).reset_index(drop=True)
    
    %timeit pd.concat([df1,df2, df.affinity], axis=1)
    1000 loops, best of 3: 1.83 ms per loop
    

    len(df) = 400k:

    df = pd.concat([df]*100000).reset_index(drop=True)
    
    %timeit pd.concat([df1,df2, df.affinity], axis=1)
    100 loops, best of 3: 15.6 ms per loop
    

    【讨论】:

    • 酷!这会在大型数据集的运行时间方面扩展吗?
    • 嗯,大数据帧中的假人需要很多内存。有问题吗?
    • 目前内存不会成为问题,因为我可以一次编码功能并将其保存到文件等。但我想知道它需要花费的时间。无论如何,我会做一些基准测试并将结果发布在这里。
    • 我添加了一些计时,请查看。
    猜你喜欢
    • 2020-05-25
    • 2018-07-29
    • 1970-01-01
    • 2016-07-13
    • 2018-08-12
    • 2015-02-12
    • 2021-07-21
    • 2016-04-25
    • 1970-01-01
    相关资源
    最近更新 更多