【问题标题】:Create Matrix multiplication with reference column使用参考列创建矩阵乘法
【发布时间】:2021-07-08 17:00:46
【问题描述】:

想计算乘法。为此,我想将行值转换为 Df1 的列

所以 Df1 矩阵可以是 [6x1] 而 Df2 是 [1x6]

结果 = [1x6] * [6x1]

Df1:

Tech_ID  s1  s2  s3  s4  s5  s6
-------------------------------
   1      1   1   1   0   1   1
   2      1   0   1   1   1   0  
   3      1   1   0   0   1   1 

Df1 = {'Tech_ID':[1,2,3],
        'S1':[1,1,1], 'S2':[1,0,1], 'S3': [1,1,0],  
           'S4':[0,1,0],'S5':[1,1,1],'S6':[1,0,1]}

Df2:

Tech_ID  Std_Id  s1_Norm  s2 Norm  S3 Norm  S4_Norm  S5 Norm  S6_Norm
---------------------------------------------------------------------
   1        1      0.87     0.87     0.27     0.83     0.37     0.83
   1        7      0.25     0.45     0.45     0.28     0.15     0.22
   2        4      0.38     0.38     0.58     0.31     0.28     0.32
   3        8      0.44     0.24     0.64     0.40     0.44     0.48
   2        2      0.73     0.43     0.33     0.78     0.53     0.75

Df2 = {'Tech_ID':[1,1,2,3,2], Std_Id [1,7,4,8,2]
        'S1_norm':[0.87,0.25,0.38,0.44,0.73], 'S2_norm':[0.87,0.45,0.38,0.24,0.43],
         'S3_norm': [0.27,0.45,0.58,0.64,0.33], 'S4_norm':[0.83,0.28,0.31,0.40,0.78], 
          'S5_norm': [0.37,0.15,0.28,0.44,0.53], 'S6_norm':[0.83,0.22,0.32,0.48,0.75],}​

计算说明,

[Df2] X [Df1]
                                                             _  _  
                                                            |    | 
  _                                                    _    | S1 | 
 |                                                      |   | S2 | 
 | s1_Norm  s2 Norm  S3 Norm  S4_Norm  S5 Norm  S6_Norm | X | S3 | 
 |_                                                    _|   | S4 | 
                                                            | S5 | 
                                                            | S6 | 
                                                            |_  _| 

对于 Tech_ID:1 和 Std_Id:1

                                              _ _  
                                             |   | 
     _                                  _    | 1 | 
    |                                    |   | 1 | 
    | 0.87  0.87  0.27  0.83  0.37  0.83 | X | 1 |  = 3.21 
    |_                                  _|   | 0 | 
                                             | 1 | 
                                             | 1 | 
                                             |_ _|    

* Result= 3.21
* Final result = Result/ Length(count) of values = 3.21/6 = 0.535

预期结果:

Tech_ID  Std_Id   Result   Final_Res
-----------------------------------
   1        1      3.21     0.535   
   1        7      2.13     0.355   
   2        4      1.55     0.258   
   3        8      1.87     0.312   
   2        2      2.37     0.395   

对如何将这些理论概念转换成 python 脚本感到困惑。

【问题讨论】:

    标签: python pandas numpy matrix


    【解决方案1】:

    尝试:

    df2['Result'] = (df1.set_index('Tech_ID')
        .mul(df2.set_index('Tech_ID').rename(columns=lambda x: x.split('_')[0]))
        .sum(1)
        .to_numpy()
    )
    
    df2['Final_Res'] = df2['Result']/(df1.shape[1]-1)
    
    print(df2[['Tech_ID','Std_Id','Result','Final_Res']])
    

    输出:

       Tech_ID  Std_Id  Result  Final_Res
    0        1       1    3.21   0.535000
    1        1       7    1.52   0.253333
    2        2       4    1.55   0.258333
    3        3       8    2.37   0.395000
    4        2       2    1.60   0.266667
    

    【讨论】:

    • 感谢重播,从www.calcul.com 手动计算每列值,因此结果与预期结果不匹配。
    • @NiravPrajapati 你是怎么得到第二排的2.13 的?
    • 哦,对不起,用实际值计算并在此处设置 Df 中的虚拟值,因此结果不匹配。
    猜你喜欢
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2017-02-19
    相关资源
    最近更新 更多