用Python抓取并分析了1982场英雄联盟数据,教你开局前预测游戏对局胜负!

英雄联盟想必大多数读者不会陌生,这是一款来自拳头,由腾讯代理的大型网络游戏,现在一进网吧,你就能发现一大片玩英雄联盟的人。在2017年中国战队无缘鸟巢的世界总决赛后,一大片人选择了弃游,只是终究没躲过“真香定理”,在2018年的中旬,又有大批战友又回到熟悉的召唤师峡谷战场,时至今日,英雄联盟已经不仅仅是一款游戏,一个电竞项目了,它已经成为了我们生活的快乐源泉了。

问君能有几多愁,辅助闪现抢人头;问君能有几多愁,卡牌千里送人头。问君能有几多愁,皇子开大关队友;清明时节雨纷纷,各种队友各种坑。别人笑我不买眼,我笑别人浪费钱;孤帆远影碧空尽,草丛惊现蛮易信 。相见时难别亦难,碰见赵信菊花残;我自横刀向天笑,剁人只需用三刀。苦练盲僧千百次, 盲目对战N 多次;战输不下五十次, 砸得鼠标要出事;举杯邀明月,草丛遇盖伦。

鉴于现在喜欢英雄联盟的大佬如此之多,为了帮助大家尽快的拿到首胜,小编爬取并分析了1982余场LOL数据。非常神奇的是!在开局之前能够高概率的预测本局的胜负!让你提前做好心理准备。

假设

假设没有王者等大神代玩小号

假设没有代练

假设没有因为半途被媳妇抓到而挂机的行为

假设没有突然掉网连不上去的行为

游戏对战数据获取

国服:腾讯游戏平台非官方API - http://www.games-cube.com/

 

 

外服:Riot开发者平台API- https://developer.riotgames.com/

 
连接数据库:
import numpy as np
import pymysql, random, json
def connect_db(database):
 try:
   conn = pymysql.connect(host='localhost', port=3306, user='root', password='zandaoguang', db=database, charset='utf8')
   return conn
 except:
   print ('Exception: MySQL Connection')
   return None

分析和训练数据

利用神经网络(neural_network)、随机森林(random_forest)和支持向量机(support_vector)等若干智能算法进行训练和回归,最终进行比较效果。

下面为神经网络部分代码:

from __future__ import print_function
import numpy as np
#np.random.seed(1337)  # for reproducibility
from keras.datasets import mnist
from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
from keras.optimizers import SGD, Adam, RMSprop
import gzip
import sys
from six.moves import cPickle
from fetcher import *
batch_size = 256
nb_classes = 2
nb_epoch = 100
champion_dict = fetch_champion_dict("champion136.json")
champion_num = len(champion_dict)
X_train = X_train.astype('int8')
X_test = X_test.astype('int8')
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()
model.add(Dense(1500, input_dim = champion_num, init='uniform'))
model.add(Activation('sigmoid'))
model.add(Dense(2))
model.add(Activation('softmax'))
model.summary()
model.compile(loss='categorical_crossentropy',
             optimizer=RMSprop(),
             metrics=['accuracy'])
history = model.fit(X_train, Y_train,
                   batch_size=batch_size, nb_epoch=nb_epoch,
                   verbose=1, validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
 

随机森林代码:

from sklearn.ensemble import RandomForestClassifier
from sklearn.externals import joblib
from fetcher import *
champion_dict = fetch_champion_dict("champion136.json")
champion_num = len(champion_dict)
X_train, y_train, X_test, y_test = fetch_one_side_riot('12', 'MATCHED_GAME', 'KING_PORO', 'KINGPORO', ('1492660800000', '1493740800000'), champion_dict)
clf = RandomForestClassifier(n_estimators=500, n_jobs=2)
clf.fit(X_train, y_train)
train_score = clf.score(X_train, y_train)
print ("Train Score = "+str(train_score))
test_score = clf.score(X_test, y_test)
print ("Test Score = "+str(test_score))
 

支持向量机代码:

from sklearn.externals import joblib
from sklearn import svm
from fetcher import *
champion_dict = fetch_champion_dict("champion136.json")
champion_num = len(champion_dict)
X_train, y_train, X_test, y_test = fetch_one_side_riot('12', 'MATCHED_GAME', 'ARAM_UNRANKED_5x5', 'ARAM', ('1492660800000', '1493740800000'), champion_dict)
clf = svm.SVC()
clf.fit(X_train, y_train)
train_score = clf.score(X_train, y_train)
print ("Train Score = "+str(train_score))
test_score = clf.score(X_test, y_test)
print ("Test Score = "+str(test_score))

 

游戏对战胜负预测

根据双方英雄阵容,预测获胜方的准确率(%)

PS:由于阿广近期没有玩英雄联盟,所以分析的数据为2017年的游戏数据。

 

 

只根据本方英雄阵容,预测本方能否获胜的准确率(%)

 

 

期望研究的问题

  • LOL中游戏胜利是否与能性别有关?
  • LOL的胜率是否和每天的时间段有关系?
  • 在女朋友阻止自己玩游戏的情况下,LOL的胜率下降多少?如何解决?

 

注:由于数据量太小,以后能收集到更多的数据,是希望可以对上面以及更多的方向进行研究。

结论

  • LOL取得最后胜利的三个重要因素为:团队阵容、团队水平,配合默契度。
  • 合理的分配BUFF也是游戏胜利的必要因素。
  • 打游戏的心态也是游戏胜利一个不可或缺的条件。

 

结语:

虽然我们通过机器学习算法可以预测这把的输赢,但是这毕竟不是我们最后的结果。玩英雄联盟和爱情类似,明明知道是我们不可能,却坚持去追求,这正是爱的美丽。也许没有什么结果,可那过程本身的美丽便足够用一生去品味。

游戏本来的用意是给大家放松心情,释放压力的(职业选手就不同了)。所以我们一定要把握好时间,对于沉迷游戏这种的就大可不必了,好的游戏应该是带给我们快乐,假如天天沉迷游戏,堕落不已,那就落入了错误的深渊了。当然好的方面也不是没有,看厂长、UZI这些选手就是好的代表啊!以上就是本文的全部内容,喜欢的小伙伴点个赞呗!

写在最后:

本文来自网络,如有侵权,请联系小编删除!

原文地址:https://www.cnblogs.com/djdjdj123/p/10254967.html