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

2024-06-21 12:50:56 浏览数 (1)

总结

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

参考视频[1]

环境

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

代码语言:javascript复制
pythhon -m pip install pandas, scikit-learn

数据集

数据集[2]

其中重要的字段有:

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

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

代码

代码语言:javascript复制
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)}' )

运行结果:

代码语言:javascript复制
准确率: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
引用链接

[1] 参考视频: https://www.youtube.com/watch?v=7ALGiitlDzA [2] 数据集: https://www.kaggle.com/datasets/datasnaek/league-of-legends

0 人点赞