预测英雄联盟(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
相关推荐
魔道不误砍柴功2 小时前
Java中的Stream API:从入门到实战
java·windows·python
xinghuitunan2 小时前
时间转换(acwing)c/c++/java/python
java·c语言·c++·python
果冻人工智能2 小时前
去中心化 AI:赋权还是混乱?
人工智能·深度学习·机器学习·架构·去中心化·区块链·ai员工
tekin2 小时前
Python 高级数据结构操作全解析:从理论到实践
数据结构·python·集合set·高级数据结构·集合操作·队列操作·堆操作
关关钧2 小时前
【R语言】绘图
开发语言·python·r语言
若兰幽竹2 小时前
【机器学习】衡量线性回归算法最好的指标:R Squared
算法·机器学习·线性回归
m0_748248023 小时前
爬虫自动化(DrissionPage)
爬虫·python·自动化
量化投资技术3 小时前
【量化科普】Arbitrage,套利
python·量化交易·量化·量化投资·qmt·miniqmt
量化投资技术3 小时前
【量化科普】Liquidity,流动性
python·量化交易·量化·量化投资·qmt·miniqmt
EterNity_TiMe_3 小时前
【人工智能】蓝耘智算平台盛大发布DeepSeek满血版:开创AI推理体验新纪元
人工智能·python·机器学习·deepseek