重新计算和更正 numpy 数组中的值
re-calculating and correcting values inside numpy array
我有一个 CSV 格式的数据集,作为二维数组导入:
game_id, player_score, rounds_played
0, 56, 30
0, 44, 30
1, 77, 26
1, 34, 26
2, 36, 23
2, 31, 23
在下一步中,我需要创建一个新数组,其中 player_score 需要在每场比赛中相对于 rounds_played 进行相对化,以估计性能的好坏单看分数的时候。
在“for 循环”中,我做了类似的事情,我计算了每轮得分的“权重”,然后再次将因子乘以轮数:
鉴于 30 是此集合中的最大值 rounds_played:
weight = (player_score * rounds_played) / 30
new_score = weight
然后我会将 new_score 附加到一个新数组。
如果我有一个更大的二维数组,这会变得很慢。 -
是否有更短、更直接的方法(可能是在 numpy 中)来创建一个具有更正分数的新数组?
您可以在 pandas.
中轻松快速地完成(由于 python 矢量化)
df['new_score'] = (df.player_score * df.rounds_played) / 30
结果 df:
我有一个 CSV 格式的数据集,作为二维数组导入:
game_id, player_score, rounds_played
0, 56, 30
0, 44, 30
1, 77, 26
1, 34, 26
2, 36, 23
2, 31, 23
在下一步中,我需要创建一个新数组,其中 player_score 需要在每场比赛中相对于 rounds_played 进行相对化,以估计性能的好坏单看分数的时候。
在“for 循环”中,我做了类似的事情,我计算了每轮得分的“权重”,然后再次将因子乘以轮数:
鉴于 30 是此集合中的最大值 rounds_played:
weight = (player_score * rounds_played) / 30
new_score = weight
然后我会将 new_score 附加到一个新数组。
如果我有一个更大的二维数组,这会变得很慢。 - 是否有更短、更直接的方法(可能是在 numpy 中)来创建一个具有更正分数的新数组?
您可以在 pandas.
中轻松快速地完成(由于 python 矢量化)df['new_score'] = (df.player_score * df.rounds_played) / 30
结果 df: