df = {1,2,3
4,5,6
7,8,9,
10,11,12
}
weights={[1,3,3],[2,2,2],[3,1,1]}
我想把我的df与每行矩阵权重相乘(所以我会有三个不同的df,每个权重向量一个,通过保持最大的值行来组合每个df)。例如:
df0=df * weights[0]={1,6,9
4,15,18,
7,24,27
10,33,36
}
df1=df*wieghts[1]={2,4,6,
8,19,12,
14,16,18,
20,22,24
}
df2=df*wieghts[2]={3,2,3,
12,5,6,
21,8,9,
30,11,12
}
以及
final_df_lines=max{df0,df1,df2}={1,6,9 - max line line from df0,
4,15,18, - max line from df0,
7,24,27 - max line from df0,
10,33,36 - max line from df0,
}
在这个例子中,所有的max都来自df0。。。但他们可能来自三个国防军中的任何一个。Max line只是将同一行中的数字相加。。你知道吗
我需要做这些事情矢量化(没有任何循环或如果…)我怎么做?至少有可能吗?我真的需要韦尔普:(两天来我在网上搜索做这个。。。我在python工作的时间不长。。。你知道吗
编辑:由于问题已更新,我也必须更新:
您必须首先对齐矩阵,才能在不使用任何循环的情况下执行按元素的矩阵操作:
输出:
解决方案是
numpy
,当然,如果您愿意,也可以使用pandas
来实现。你知道吗您可以尝试
concatenat
将所有权重的多个列作为一个数据帧,suffix of column
表示每个权重, 再乘以grouping with respect to the weight
,得到指数的最大和使用最大索引权重,可以将数据帧相乘
相关问题 更多 >
编程相关推荐