Skip to main content
 首页 » 编程设计

python之从 Pandas GroupBy 函数(和/或建议的其他方法)为 SVM 创建特征(行)向量

2024年11月01日9mayingbao

我正在尝试为 SVM 分类器创建堆叠特征向量。我的所有数据都在一个大矩阵中。手头的问题是一个多类分类问题,所以我需要使用多索引进行分组。

这是我正在努力实现的玩具示例。

N = 4 
col_ids = string.letters[:N] 
df = pd.DataFrame( 
      np.random.randint(10, size=(16,N)),       #np.random.randn(1,N), 
      columns=['col_{}'.format(letter) for letter in col_ids]) 
 
test_cols = ['test1','test1','test1','test1','test1','test1','test1','test1','test2','test2','test2','test2','test2','test2','test2','test2'] 
test_iter = [1,1,1,1,2,2,2,2,1,1,1,1,2,2,2,2] 
 
df.insert(0, 'Activity', test_cols) 
df.insert(1, 'Iteration', test_iter) 

输出:

   Activity  Iteration  col_A  col_B  col_C  col_D 
0     test1          1      7      2      9      7 
1     test1          1      9      7      2      7 
2     test1          1      4      4      2      2 
3     test1          1      0      1      0      6 
4     test1          2      3      5      3      3 
5     test1          2      9      5      7      6 
6     test1          2      9      5      8      6 
7     test1          2      9      7      9      1 
8     test2          1      3      2      5      5 
9     test2          1      8      5      9      0 
10    test2          1      8      6      3      9 
11    test2          1      3      9      2      5 
12    test2          2      0      4      4      1 
13    test2          2      7      0      4      6 
14    test2          2      5      4      0      9 
15    test2          2      0      0      5      0 

我使用以下 groupby 为我的应用程序获取适当的组:

g = df.groupby(["Activity", "Iteration"]) 
 
                      Activity  Iteration  col_A  col_B  col_C  col_D 
Activity   Iteration                                                    
test1    1         0     test1          1      7      2      9      7 
                   1     test1          1      9      7      2      7 
                   2     test1          1      4      4      2      2 
                   3     test1          1      0      1      0      6 
         2         4     test1          2      3      5      3      3 
                   5     test1          2      9      5      7      6 
                   6     test1          2      9      5      8      6 
                   7     test1          2      9      7      9      1 
test2    1         8     test2          1      3      2      5      5 
                   9     test2          1      8      5      9      0 
                   10    test2          1      8      6      3      9 
                   11    test2          1      3      9      2      5 
         2         12    test2          2      0      4      4      1 
                   13    test2          2      7      0      4      6 
                   14    test2          2      5      4      0      9 
                   15    test2          2      0      0      5      0 

现在我想创建特征向量并将它们存储到一个新的 DataFrame 中,但以一种只使用两行来创建一个特征向量的方式。这意味着在测试示例中,test1 事件执行两次,每次迭代都具有相同的标签,因此在本例中它有两个标签:1 和 2。每个标签应堆叠两行以创建所需的输出。

从 test1 我想创建四个行向量,这样完整的输出(理想情况下)如下所示:

test1 test1 ... test2 
    7     4         5 
    2     4         4 
    9     2         0 
    7     2         9 
    9     0         0 
    7     1         0 
    2     0         5 
    7     6         9 

我还没有写出全部内容,但我希望我想要实现的目标是显而易见的。基本上;两行成为一个堆叠的行向量(标签在上面),相同的向量是一个特征向量。由于我有多个事件,因此每个事件需要多个特征向量来训练 SVM。对于这个例子,我最好得到一个 pd.DataFrame,里面有八个特征行向量,这样数据框就会被重新整形(忽略除 col_A 到 col_B 中包含的实际数据之外的所有内容)从 (16,4) 到 (8) ,8).

据我所知,这并没有得到令人惊讶的解释,所以如果您需要更多详细信息,请告知,如果您愿意提供帮助。

谢谢。

请您参考如下方法:

您需要将一个函数传递给为最终输出准备数据的groupby,然后重新标记列,就像这样:

def f(x): 
    values = [v for vals in x.values for v in vals[2:]] 
    return pd.Series(values,name=x.values[0][0]) 
 
res = df.groupby(["Activity", "Iteration"]).apply(f) 
res = res.T.rename(columns={(t,i):t for t,i in res.index}) 
print df    
print res 

在我的测试输出中:(注意数据是随机的!)

   Activity  Iteration  col_A  col_B  col_C  col_D 
0     test1          1      4      6      5      7 
1     test1          1      5      9      5      4 
2     test1          1      1      8      7      9 
3     test1          1      4      8      1      9 
4     test1          2      4      5      5      6 
5     test1          2      6      3      8      6 
6     test1          2      8      1      1      2 
7     test1          2      5      1      8      1 
8     test2          1      6      3      9      9 
9     test2          1      4      9      9      7 
10    test2          1      5      0      1      3 
11    test2          1      5      8      9      5 
12    test2          2      4      8      3      2 
13    test2          2      8      9      9      4 
14    test2          2      6      1      1      8 
15    test2          2      6      4      4      8 
    test1  test1  test2  test2 
0       4      4      6      4 
1       6      5      3      8 
2       5      5      9      3 
3       7      6      9      2 
4       5      6      4      8 
5       9      3      9      9 
6       5      8      9      9 
7       4      6      7      4 
8       1      8      5      6 
9       8      1      0      1 
10      7      1      1      1 
11      9      2      3      8 
12      4      5      5      6 
13      8      1      8      4 
14      1      8      9      4 
15      9      1      5      8 

每个测试的 2 列 8 个元素有点棘手,但您可以采用相同的方式:

def g(x): 
    values = [v for vals in x.values for v in vals[2:]] 
    return pd.DataFrame({1: values[:N/2*len(x)], 2: values[N/2*len(x):]}) 
 
res = df.groupby(["Activity", "Iteration"]).apply(g).unstack() 
r1 = res[1].T.rename(columns={(t,i):t+str(i)+"1" for t,i in res.index}) 
r2 = res[2].T.rename(columns={(t,i):t+str(i)+"2" for t,i in res.index}) 
res = pd.concat([r1,r2],axis=1).sort(axis=1) 
res = res.rename(columns={t:t[:-2] for t in res.columns}) 
 
print df 
print res 

哪些输出:

   Activity  Iteration  col_A  col_B  col_C  col_D 
0     test1          1      0      8      1      7 
1     test1          1      2      0      5      0 
2     test1          1      2      6      6      6 
3     test1          1      5      0      1      4 
4     test1          2      4      5      6      8 
5     test1          2      8      0      1      6 
6     test1          2      6      7      2      4 
7     test1          2      3      2      2      3 
8     test2          1      5      2      1      9 
9     test2          1      8      3      5      9 
10    test2          1      3      7      7      1 
11    test2          1      7      4      5      1 
12    test2          2      9      2      4      0 
13    test2          2      3      1      8      7 
14    test2          2      1      2      7      8 
15    test2          2      4      9      7      0 
   test1  test1  test1  test1  test2  test2  test2  test2 
0      0      2      4      6      5      3      9      1 
1      8      6      5      7      2      7      2      2 
2      1      6      6      2      1      7      4      7 
3      7      6      8      4      9      1      0      8 
4      2      5      8      3      8      7      3      4 
5      0      0      0      2      3      4      1      9 
6      5      1      1      2      5      5      8      7 
7      0      4      6      3      9      1      7      0 

希望对你有帮助