预测英雄联盟(LOL)比赛结果

总结

使用pandas和scikit-learn进行快速的机器学习。只使用英雄选择和第一滴血、一龙、一塔、一大龙的信息就可以获得80%的预测准确率。

参考视频

环境

需要安装pandasscikit-learn包,分别用于读取文件和使用机器学习模型:

shell 复制代码
pythhon -m pip install pandas, scikit-learn

数据集

数据集

其中重要的字段有:

  • 获胜者
  • 一男爵、一小龙、一塔、一血、一水晶、一峡谷先锋
  • 英雄BP和召唤师技能

数据稍微有点过时了,是7年前的排位数据。

代码

python 复制代码
import json
import pandas as pd
# 读取数据 
df = pd.read_csv('games.csv')  # 比赛数据
champ_data = json.load(open('champion_info.json')) # 英雄名称和id的对应

def id2name(id):
    return champ_data['data'][str(id)]['name']
    
for cid in ['t1_champ1id','t1_champ2id','t1_champ3id','t1_champ4id','t1_champ5id',
           't2_champ1id','t2_champ2id','t2_champ3id','t2_champ4id','t2_champ5id']:
    df[cid[:-2]] = df[cid].apply(id2name)

# 只使用英雄名称和第一滴血、一龙、一塔、一大龙的信息
df = df[['t1_champ1', 't1_champ2','t1_champ3','t1_champ4','t1_champ5', 't2_champ1', 't2_champ2','t2_champ3','t2_champ4','t2_champ5', 'winner',
        'firstBlood', 'firstDragon', 'firstTower','firstBaron']]

# 将英雄名称编码成数字
encoding1 = [pd.get_dummies(df[col], prefix='t1') for col in ['t1_champ1','t1_champ2','t1_champ3','t1_champ4','t1_champ5'] ]
combined_df1 = sum(encoding1)

encoding2 = [pd.get_dummies(df[col], prefix='t2') for col in ['t2_champ1','t2_champ2','t2_champ3','t2_champ4','t2_champ5'] ]
combined_df2 = sum(encoding2)


df = df.join(combined_df1).join(combined_df2)

df = df.drop(['t1_champ1','t1_champ2','t1_champ3','t1_champ4','t1_champ5', 't2_champ1','t2_champ2','t2_champ3','t2_champ4','t2_champ5'], axis=1)


from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

x,y = df.drop('winner', axis=1), df['winner']
x_train,x_test , y_train, y_test = train_test_split(x,y,test_size=0.2)

# 使用随机森林分类器
clf = RandomForestClassifier(n_jobs=-1)
clf.fit(x_train, y_train)
score = clf.score(x_test,y_test)
print(f'准确率:{score}')

# 重要性排序
importance = dict(zip(clf.feature_names_in_, clf.feature_importances_))
sorted_importance = sorted(importance.items(), key=lambda x: x[1], reverse=True)
print("前10重要的因素:")
print(sorted_importance[:10])



# 计算 崔丝塔娜 在历史排位中的胜率
champ_name = 'Tristana'
wins1 = len(df[(df[f't1_{champ_name}']==1) & (df['winner'] == 1)])
wins2 = len(df[(df[f't2_{champ_name}']==1) & (df['winner'] == 2)])

losses1 = len(df[(df[f't1_{champ_name}']==1) & (df['winner'] == 2)])
losses2 = len(df[(df[f't2_{champ_name}']==1) & (df['winner'] == 1)])
print(f'Tristana 胜率为:{(wins1+wins2) / (wins1+wins2+losses1+losses2)}' )

运行结果:

python 复制代码
准确率:0.7992814138667702
前10重要的因素:
[('firstBaron', 0.16264685201450763), ('firstTower', 0.1157341294170909), ('firstDragon', 0.06596263062483931), ('firstBlood', 0.0185560893560152), ('t2_Tristana', 0.004941789639522274), ('t1_Tristana', 0.00489940550998658), ('t2_Kayn', 0.004508701682113675), ('t1_Kayn', 0.004488279354184191), ('t2_Vayne', 0.0044497665526188855), ('t1_Vayne', 0.004441653744800986)]
Tristana 胜率为:0.5170607717784795
相关推荐
搏博3 分钟前
机器学习之三:归纳学习
人工智能·深度学习·学习·机器学习
一个天蝎座 白勺 程序猿10 分钟前
Python爬虫(8)Python数据存储实战:JSON文件读写与复杂结构化数据处理指南
爬虫·python·json
q_q王27 分钟前
dify对接飞书云文档,并且将图片传入飞书文档
python·大模型·飞书·dify·智能体·图片展示
noravinsc33 分钟前
django filter 排除字段
后端·python·django
zandy10111 小时前
嵌入式BI开发指南:如何通过衡石API将分析能力集成到业务系统?
开发语言·python·嵌入式
曲幽1 小时前
零基础快速搭建AI绘画网站!用Gradio玩转Stable Diffusion
python·ai作画·stable diffusion·gradio·diffusers·webui
2401_890665862 小时前
免费送源码:Java+ssm+HTML 三分糖——甜品店网站设计与实现 计算机毕业设计原创定制
java·python·微信小程序·html·php·课程设计·android-studio
noravinsc2 小时前
django filter 日期大于当前日期的
python·django
悲喜自渡7212 小时前
pytorch & python常用指令
人工智能·pytorch·python
Star abuse2 小时前
Python爬虫课程实验指导书
开发语言·爬虫·python