宝可梦数据分析

python入门——天池task4——宝可梦数据分析

  • 数据集下载
# 数据集下载,也可以通过网页链接下载
!wget -O pokemon_data.csv https://pai-public-data.oss-cn-beijing.aliyuncs.com/pokemon/pokemon.csv

提供的csv数据文件是矩阵(801,47),即四十七个纵列(参数)和801个宝可梦。里面的各项参数分别为:
name(英文名): The English name of the Pokemon
japanese_name(日文名): The Original Japanese name of the Pokemon
pokedex_number(宝可梦编号): The entry number of the Pokemon in the National Pokedex
percentage_male(雄性比): The percentage of the species that are male. Blank if the Pokemon is genderless.
type1(第一属性): The Primary Type of the Pokemon
type2(第二属性): The Secondary Type of the Pokemon
classification(分类): The Classification of the Pokemon as described by the Sun and Moon Pokedex
height_m(高度): Height of the Pokemon in metres
weight_kg(重量): The Weight of the Pokemon in kilograms
capture_rate(抓捕纪律): Capture Rate of the Pokemon
base eggsteps(破蛋步数): The number of steps required to hatch an egg of the Pokemon
abilities(特性): A stringified list of abilities that the Pokemon is capable of having
experience_growth(经验值): The Experience Growth of the Pokemon
base_happiness(开心值): Base Happiness of the Pokemon
against_?(相克性能): Eighteen features that denote the amount of damage taken against an attack of a particular type
hp(血量): The Base HP of the Pokemon
attack(攻击值): The Base Attack of the Pokemon
defense(防御值): The Base Defense of the Pokemon
sp_attack(特殊攻击): The Base Special Attack of the Pokemon
sp_defense(特殊防御): The Base Special Defense of the Pokemon
speed(速度): The Base Speed of the Pokemon
generation(代): The numbered generation which the Pokemon was first introduced
is_legendary(是否传说级,即是否为游戏中的稀有精灵): Denotes if the Pokemon is legendary.
total_point(评分)

  • 然后导入我们需要用到的相关包,此处为Pandas, Seaborn, Matplotlib, 并且读取数据
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
#读取文件,文件名与自己的文件名相符
df = pd.read_csv("./pokemon_data.csv")
  • 通过df.shape查看数据的整体尺寸维度, 通过df.info()查看每一条数据的详细信息
print(df.shape)    #(801, 41)
print(df.info)
  • 观察每个特征的缺失情况:
print(df.against_bug.isnull().sum()*100/ len(df),'%') #计算against_bug的缺失比
print(df.type2.isnull().sum()*100/ len(df),'%')    #计算type2的缺失比

# 计算出每个特征有多少百分比是缺失的
percent_missing = df.isnull().sum() * 100 / len(df)
#print(percent_missing)
#对列进行命名
missing_value_df = pd.DataFrame({
    'df.columns': df.columns,
    'percent_missing': percent_missing
})

print(percent_missing.shape)
# 查看Top10缺失的
missing_value_df.sort_values(by='percent_missing', ascending=False).head(10)

输出如下:
可以看到,总共只有4个属性有缺失项,其中type2 这个字段缺失的比率最高,达到了 48% 左右。说明超过半数的宝可梦还是单纯的只有一个属性,剩下一般的则具有两种属性。
可以看到,总共只有4个属性有缺失项,其中type2 这个字段缺失的比率最高,达到了 48% 左右。说明超过半数的宝可梦还是单纯的只有一个属性,剩下一般的则具有两种属性。

  • 然后再来看一下每一代宝可梦的数目
# 查看各代口袋妖怪的数量
df['generation'].value_counts().plot.bar()

各代口袋妖怪的数量
可以发现,宝可梦数量最多的是在第5代,最少的是在第6代。

  • 然后我们再来看不同的主属性的分布。
# 查看每个系口袋妖怪的数量
df['type1'].value_counts().sort_values(ascending=True).plot.barh()

在这里插入图片描述
这里我们可以看到,数量最多的宝可梦是水系,然后是普通,然后是草系。

  • 接下来再来看一下各个属性之间的相关性
# 相关性热力图分析
plt.subplots(figsize=(20,15))   #尺寸
ax = plt.axes()
ax.set_title("Correlation Heatmap")  #标题
corr = df.corr()
#print(corr)
sns.heatmap(corr,annot = True,vmin=0, vmax=2,cmap="YlGnBu",
           xticklabels=corr.columns.values,
            yticklabels=corr.columns.values )   #标出每行每列的标签

在这里插入图片描述
通过观察 attack 这一个特征和 height_m 是正相关的,我们可以得出:越高的宝可梦,攻击力越高。但是再看 height_m,我们会发现它和 base_happiness 是负相关的。这个时候我们可以作出另外一个结论:长得高的宝可梦可能都不太开心。

  • 接下来我们从宝可梦在实战中的角度来分析这组数据。这里我们只关注六个基础值:血量,攻击力,防御力,特攻,特防,速度。因为只有这六个基础值决定了一只宝可梦的战斗力在不考虑派系克制的情况下。
interested = ['hp','attack','defense','sp_attack','sp_defense','speed']
sns.pairplot(df[interested],  palette="husl", kind="reg")

在这里插入图片描述
这里我们可以看到大部分都是成正比例的,一个值的提高往往会拉高另外一个值。

  • 这点我们通过相关性热力图也可以看到
# 通过相关性分析heatmap分析五个基础属性
plt.subplots(figsize=(10,8))
ax = plt.axes()
ax.set_title("Correlation Heatmap")
corr = df[interested].corr()
sns.heatmap(corr, 
            xticklabels=corr.columns.values,
            yticklabels=corr.columns.values,
            annot=True, cmap="YlGnBu",linewidths=.5,fmt='.2!

在这里插入图片描述

  • 最后来挑选一下平民神兽,这里我们可以通过如下方式,先做一个特征类型转化,然后再计算
for c in interested:
    df[c] = df[c].astype(float)
df = df.assign(total_stats = df[interested].sum(axis=1)) 

这样我们就完成了用total_stats这个字段来存储种族值这一特征。我们可以做个柱状图可视化来看看种族值的分布是什么样的:

# 种族值分布
total_stats = df.total_stats
plt.hist(total_stats,bins=35)
plt.xlabel('total_stats')
plt.ylabel('Frequency')

在这里插入图片描述
同时还可以根据不同的属性来看:

# 不同属性的种族值分布
plt.subplots(figsize=(20,12))
#画小提琴图
ax = sns.violinplot(x="type1", y="total_stats",
                    data=df, palette="muted")  

在这里插入图片描述

  • 最后就可以通过简单的过滤和排序来找到我们应该去捕捉的宝可梦了:
df[(df.total_stats >= 570) & (df.is_legendary == 0)]['name'].head(10)

#输出为:
'''
2        Venusaur
5       Charizard
8       Blastoise
17        Pidgeot
64       Alakazam
79        Slowbro
93         Gengar
114    Kangaskhan
126        Pinsir
129      Gyarados
Name: name, dtype: object
'''
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值