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

图片来源网络

总结

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

参考视频

环境

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

pythhon -m pip install pandas, scikit-learn

数据集

数据集

其中重要的字段有:

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

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

代码

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)}' )

运行结果:

准确率:0.799281413866770210重要的因素:
[('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

最近更新

  1. TCP协议是安全的吗?

    2024-06-15 16:38:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-15 16:38:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-15 16:38:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-15 16:38:02       18 阅读

热门阅读

  1. win10下使用docker和VMware

    2024-06-15 16:38:02       8 阅读
  2. Android 14 蓝牙主从模式切换

    2024-06-15 16:38:02       8 阅读
  3. C# —— 位运算符

    2024-06-15 16:38:02       5 阅读
  4. vim 存在三种模式:

    2024-06-15 16:38:02       5 阅读
  5. k8s_探针专题

    2024-06-15 16:38:02       7 阅读
  6. 行为型-观察者模式(Observer)

    2024-06-15 16:38:02       9 阅读
  7. 递归下降解析器在Python中的实现与应用

    2024-06-15 16:38:02       5 阅读