Exploring Pokémon with Python and APIs: Statistical Analysis of Pokémon Types and Combinations

Pokemon is a popular media franchise that has millions of fans worldwide. Some fans use the term “Pokemonology” to describe their deep interest and study of the Pokemon world.
Although there is no formal academic discipline dedicated to pokemonology, enthusiasts may explore various aspects of the franchise, including the different species of Pokemon, their unique characteristics and abilities, the regions and landscapes they inhabit, and the culture and history of the Pokemon world.
Fans of the franchise may also develop their own theories, interpretations, and participate in the competitive scene through Pokemon battles, trading, and collecting rare cards or memorabilia. Pokemonology is a way for fans to engage with the fictional world of Pokemon on a deeper level.
import requests
uri_host = 'https://pokeapi.co/api/v2/';
pkmn = 'pokemon';
r = requests.get(uri_host+pkmn)
r_json = r.json();
pkmn_count=r_json['count']
query='?limit={count}'.format(count=pkmn_count);
r = requests.get(uri_host+pkmn+query)
r_json = r.json();
pkmns_info=r_json['results'];
The provided code uses the PokeAPI to retrieve information about all Pokemon available. It retrieves the number of Pokemon available, which is then used to obtain information about each Pokemon. With this information, statistical analysis can be conducted on various Pokemon properties, such as their type, strength, and weaknesses. This knowledge can be used to create effective strategies for playing Pokemon games or building a balanced Pokemon team.
from itertools import chain
from numpy import concatenate
from eule import spread_euler
for pkmn_info in tqdm(pkmns_info):
pkmn_name=pkmn_info['name'];
pkmn=requests.get(pkmn_info['url']).json();
pkmn_types=[]
for type_obj in pkmn['types']:
pkmn_types.append(type_obj['type']['name']);
pkmns_types[pkmn_name]=pkmn_types;
types=list(map(lambda item: item[1], pkmns_types.items()))
types=list(set(list(concatenate(types).flat)))
type_to_pkmn=dict(map(lambda type: (type, []), types));
for pkmn_name in tqdm(pkmns_types.keys()):
for pkmn_types in pkmns_types[pkmn_name]:
type_to_pkmn[pkmn_types].append(pkmn_name)
pkmn_euler=spread_euler(type_to_pkmn)
print(pkmn_euler)
The code utilizes the itertools
, numpy
, and eule
libraries to perform data manipulation and statistical calculations on the Pokemon data obtained using the PokeAPI. It first loops through the Pokemon data obtained from the API and retrieves each Pokemon’s types, which are then stored in a dictionary called pkmns_types
with the Pokemon’s name as the key. The code then creates a list of all unique types found among all the Pokemon and initializes a dictionary called type_to_pkmn
with each type as a key and an empty list as the value. The type_to_pkmn
dictionary is populated with each Pokemon’s name in the corresponding type list, and thespread_euler()
function from the eule
library is called to calculate the Euler characteristic of the Pokemon data. The resulting pkmn_euler
variable contains the calculated Euler characteristic, which is printed to the console.
The resulting data is a dictionary that shows the number of occurrences of each Pokemon type in the dataset. The keys of the dictionary represent the different types of Pokemon, and the values represent the number of Pokemon of that type in the dataset.
{'water': 185, 'normal': 154, 'flying': 149, 'grass': 144, 'psychic': 133, 'electric': 109, 'bug': 104, 'dragon': 102, 'fire': 101, 'rock': 100, 'fighting': 99, 'poison': 98, 'dark': 94, 'ground': 92, 'ghost': 89, 'steel': 89, 'fairy': 82, 'ice': 66}
For example, the dataset contains 185 water-type Pokemon, 154 normal-type Pokemon, and 149 flying-type Pokemon, among others.
The dictionary also shows the count of different combinations of two Pokemon types in the dataset, with each key in the dictionary representing a unique combination of two types, separated by a comma, and the value representing the count of how many Pokemon have that combination of types.
{'bug,dark': 1, 'bug,ghost': 1, 'fairy,poison': 1, 'dragon,fairy': 1, 'fairy,fighting': 1, 'fairy,ice': 1, 'electric,psychic': 1, 'fighting,ground': 1, 'normal,water': 1, 'fire,water': 1, 'steel,water': 1, 'fighting,ice': 1, 'fire,ice': 1, 'ghost,ice': 1, 'electric,ghost': 1, 'fire,steel': 1, 'fire,grass': 1, 'electric,fire': 1, 'fighting,rock': 1, 'flying,ice': 2, 'bug,ice': 2, 'bug,ground': 2, 'normal,poison': 2, 'poison,steel': 2, 'poison,psychic': 2, 'electric,fairy': 2, 'psychic,rock': 2, 'ground,psychic': 2, 'electric,ground': 2, 'ground,normal': 2, 'electric,ice': 2, 'ice,steel': 2, 'ghost,normal': 2, 'fighting,ghost': 2, 'fire,normal': 2, 'dragon,rock': 2, 'dragon,normal': 2, 'electric,normal': 2, 'grass,rock': 2, 'dark,rock': 2, 'dark,electric': 2, 'fighting,flying': 3, 'flying,ghost': 3, 'flying,poison': 3, 'bug,fairy': 3, 'poison,rock': 3, 'fairy,rock': 3, 'dark,psychic': 3, 'grass,ground': 3, 'ground,ice': 3, 'grass,water': 3, 'electric,water': 3, 'grass,ice': 3, 'dark,ice': 3, 'ice,rock': 3, 'dark,ghost': 3, 'grass,steel': 3, 'dark,steel': 3, 'dragon,fire': 3, 'electric,fighting': 3, 'electric,grass': 3, 'flying': 4, 'flying,steel': 4, 'flying,ground': 4, 'fairy,flying': 4, 'bug,psychic': 4, 'dragon,poison': 4, 'fighting,poison': 4, 'fire,poison': 4, 'ground,poison': 4, 'dark,fairy': 4, 'fairy,water': 4, 'fire,psychic': 4, 'ghost,psychic': 4, 'dark,ground': 4, 'fire,ground': 4, 'ghost,water': 4, 'fighting,steel': 4, 'fighting,normal': 4, 'electric,rock': 4, 'bug,electric': 5, 'bug,fighting': 5, 'bug,fire': 5, 'electric,poison': 5, 'ghost,poison': 5, 'fairy,grass': 5, 'fairy,normal': 5, 'fairy,ghost': 5, 'grass,psychic': 5, 'dragon,psychic': 5, 'ice,psychic': 5, 'dragon,ghost': 5, 'ghost,steel': 5, 'electric,steel': 5, 'dark,fire': 5, 'fighting,grass': 5, 'dark,fighting': 5, 'dark,dragon': 5, 'grass,normal': 5, 'bug,grass': 6, 'bug,rock': 6, 'bug,water': 6, 'poison,water': 6, 'fighting,psychic': 6, 'ghost,ground': 6, 'dragon,ice': 6, 'dragon,steel': 6, 'fire,rock': 6, 'dark,normal': 6, 'flying,grass': 7, 'bug,steel': 7, 'dark,poison': 7, 'normal,psychic': 7, 'psychic,water': 7, 'ground,steel': 7, 'fighting,water': 7, 'rock,steel': 7, 'dragon,grass': 7, 'dark,flying': 8, 'electric,flying': 8, 'dragon,flying': 8, 'fire,flying': 8, 'dragon,water': 8, 'fighting,fire': 8, 'dragon,fighting': 8, 'dragon,electric': 8, 'dark,grass': 8, 'flying,psychic': 9, 'fairy,steel': 9, 'psychic,steel': 9, 'ground,rock': 9, 'dark,water': 9, 'ice,water': 9, 'fire,ghost': 9, 'flying,water': 10, 'ground,water': 10, 'fairy,psychic': 11, 'dragon,ground': 11, 'rock,water': 12, 'bug,poison': 13, 'dragon': 13, 'ghost,grass': 14, 'steel': 14, 'bug,flying': 15, 'grass,poison': 16, 'dark': 16, 'poison': 17, 'flying,rock': 18, 'ground': 18, 'ghost': 19, 'rock': 20, 'ice': 22, 'bug': 23, 'fairy': 23, 'flying,normal': 31, 'fighting': 31, 'fire': 38, 'psychic': 47, 'grass': 48, 'electric': 52, 'normal': 83, 'water': 84}
For example, there is 1 pokemon in the dataset that has a combination of bug and dark types, 1 pokemon with a combination of bug and ghost types, and so on. The highest count in this dataset is 84, which represents the water types.
This data could be useful for analyzing the distribution of pokemon types in the dataset, identifying which combinations of types are most common, and potentially using that information to inform gameplay strategies or game development decisions.
Finally, here are some possible statistics on pokemon properties in general:
- Type distribution: This statistic would show the distribution of pokemon types across all generations. It could show the most common and least common types, as well as how the distribution has changed over time.
- Base stat distribution: This statistic would show the distribution of base stats (HP, Attack, Defense, Special Attack, Special Defense, and Speed) across all pokemon. It could show the average base stats for each generation, as well as the pokemon with the highest and lowest base stats.
- Gender distribution: This statistic would show the distribution of male and female pokemon across all generations. It could show the percentage of pokemon that are male, female, or genderless, as well as any trends in gender distribution over time.
- Evolution chains: This statistic would show the distribution of pokemon evolution chains across all generations. It could show the most common and least common evolution chains, as well as any patterns in how pokemon evolve.
- Legendary pokemon: This statistic would show the distribution of legendary pokemon across all generations. It could show the number of legendary pokemon in each generation, as well as any trends in how legendary pokemon are introduced.
- Move distribution: This statistic would show the distribution of moves across all pokemon. It could show the most common and least common moves, as well as any trends in move distribution over time.
- Height and weight distribution: This statistic would show the distribution of pokemon heights and weights across all generations. It could show the average height and weight for each generation, as well as the tallest and heaviest pokemon.