预测英雄联盟(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
相关推荐
一丝晨光几秒前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
sp_wxf23 分钟前
Lambda表达式
开发语言·python
牛哥带你学代码25 分钟前
交叠型双重差分法
人工智能·深度学习·机器学习
学步_技术32 分钟前
自动驾驶系列—线控系统:驱动自动驾驶的核心技术解读与应用指南
人工智能·机器学习·自动驾驶·线控系统·转向系统
蜡笔小新星43 分钟前
Python Kivy库学习路线
开发语言·网络·经验分享·python·学习
篝火悟者1 小时前
问题-python-运行报错-SyntaxError: Non-UTF-8 code starting with ‘\xd5‘ in file 汉字编码问题
开发语言·python
quaer1 小时前
Open-Sora全面开源?
开发语言·算法·机器学习·matlab·矩阵
hakesashou1 小时前
python如何比较字符串
linux·开发语言·python
墨@#≯2 小时前
机器学习系列篇章0 --- 人工智能&机器学习相关概念梳理
人工智能·经验分享·机器学习
_.Switch2 小时前
Python机器学习模型的部署与维护:版本管理、监控与更新策略
开发语言·人工智能·python·算法·机器学习