【发布时间】:2019-11-24 00:02:22
【问题描述】:
我收集了一个大型口袋妖怪数据集,我的目标是根据我构建的比率确定“前 10 名团队” - 口袋妖怪 BST(基础统计总数据):平均弱点。对于那些关心的人,我将平均弱点计算为口袋妖怪对每种类型的弱点的总和(0.25 飞行 + 1 水 + 2 钢 + 4 火等)然后除以 18(总数游戏中可用的类型)。
举个简单的例子 - 由以下三个口袋妖怪组成的团队:Kingler、Mimikyu、Magnezone 将产生 1604.1365384615383 的团队比率。
因为数据将用于竞技比赛,我删除了所有未完全进化的口袋妖怪以及传奇/神话口袋妖怪。到目前为止,这是我的过程:
- 创建完全进化的口袋妖怪团队的所有可能组合的集合
- 使用 for 循环遍历每个组合
- 前 10 个组合将自动添加到列表中
- 从第11个组合开始,我将当前团队迭代添加到列表中,将列表按降序排序,然后移除比例最低的团队。这样可以确保每次迭代后只保留前 10 名。
很明显,这个过程需要很长时间才能运行。我想知道是否有更有效的方法来运行它。最后,请看我下面的代码:
import itertools
import pandas as pd
df = pd.read_csv("Downloads/pokemon.csv") # read in csv of fully-evolved Pokemon data
# list(df) # list of df column names - useful to see what data has been collected
df = df[df["is_legendary"] == 0] # remove legendary pokemon - many legendaries are allowed in competitive play
df = df[['abilities', # trim df to contain only the columns we care about
'against_bug',
'against_dark',
'against_dragon',
'against_electric',
'against_fairy',
'against_fight',
'against_fire',
'against_flying',
'against_ghost',
'against_grass',
'against_ground',
'against_ice',
'against_normal',
'against_poison',
'against_psychic',
'against_rock',
'against_steel',
'against_water',
'attack',
'defense',
'hp',
'name',
'sp_attack',
'sp_defense',
'speed',
'type1',
'type2']]
df["bst"] = df["hp"] + df["attack"] + df["defense"] + df["sp_attack"] + df["sp_defense"] + df["speed"] # calculate BSTs
df['average_weakness'] = (df['against_bug'] # calculates a Pokemon's 'average weakness' to other types
+ df['against_dark']
+ df['against_dragon']
+ df['against_electric']
+ df['against_fairy']
+ df['against_fight']
+ df['against_fire']
+ df['against_flying']
+ df['against_ghost']
+ df['against_grass']
+ df['against_ground']
+ df['against_ice']
+ df['against_normal']
+ df['against_poison']
+ df['against_psychic']
+ df['against_rock']
+ df['against_steel']
+ df['against_water']) / 18
df['bst-weakness-ratio'] = df['bst'] / df['average_weakness'] # ratio of BST:avg weakness - the higher the better
names = df["name"] # pull out list of all names for creating combinations
combinations = itertools.combinations(names, 6) # create all possible combinations of 6 pokemon teams
top_10_teams = [] # list for storing top 10 teams
for x in combinations:
ratio = sum(df.loc[df['name'].isin(x)]['bst-weakness-ratio']) # pull out sum of team's ratio
if(len(top_10_teams) != 10):
top_10_teams.append((x, ratio)) # first 10 teams will automatically populate list
else:
top_10_teams.append((x, ratio)) # add team to list
top_10_teams.sort(key=lambda x:x[1], reverse=True) # sort list by descending ratios
del top_10_teams[-1] # drop team with the lowest ratio - only top 10 remain in list
top_10_teams
【问题讨论】:
-
如果限制为 2 的组合需要多长时间?如果可能的话,甚至是 1 个?
-
好吧,我对口袋妖怪了解不多,但我首先要说的是,肯定有一种方法可以不遍历六个口袋妖怪的所有可能组合(六个火口袋妖怪肯定不会进入前 10例如团队),因此您可以首先尝试想一种方法来获取您当前拥有的 3.31e12 组合的子集!然后,我建议您将可能的组合分成较小的组(以免遇到内存错误),并尝试使用
NumPy数组而不是pandas数据帧来矢量化您想要做的事情。 -
进展如何,你找到了完美的团队吗?
标签: python loops bigdata combinations