League of Legends rank(ing) guide
Tags: machinelearning, beginners, gamedev
Published: 2022-04-01 01:21:35

# TLDR
Channel your inner scholar and Ryze to the top – leverage machine learning ranking models that can increase your rank in League of Legends through match analysis and predictions, free of code!

# Glossary
- Intro
- Objective
- Dataset
- Data cleaning
- Feature engineering
- Model training
- Evaluation
- Deploying API
- Conclusion

# Intro
League of Legends is a multiplayer online game that is played [by millions](https://hostingtribunal.com/blog/league-of-legends-player-count/) casually and professionally. For a competitive 5 vs 5 game where players fight to take down the others’ home base, it’s no sup-Ryze that if you choose to play the most over-powered (“OP”) champions, which are characters you play, you’ll have a higher likelihood of winning games against players of your skill level.

!["Ryze", a champion in League with rune tattoos on his arm channeling magic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/do02jwkc389hb9xqh2lb.png)_Ryze (Source: [Riot Games](https://www.leagueoflegends.com/en-gb/champions/ryze/))_

Thus, if you’re looking to improve in the competitive League of Legends ranked game mode, how about trying an ML analysis to ensure you make the most statistically-backed decisions in your next game? How about learning more about a buzzword in the tech industry, “machine learning (ML)” while enjoying content about your interests? 

Like Hextech in [Arcane](https://arcane.com/en-us/), we at Mage see ML as a tool that can be wielded by anyone and for anything, not just a few data scientists who excel at math and know how to code. Neither of these are required, you just need to understand the concept of a ranking model.

# What is ranking?
[Ranking](https://www.mage.ai/blog/machine-learning-ml-applications-ranking) is an application of machine learning that sorts data based on a parameter (like whether you win or lose in a League of Legends game). Depending on the order of how the data is sorted, we can make predictions on future outcomes of new data. 

In other words, if you take a **list of Teemo games** and rank them based on whether he **wins or loses**, the model will score the champion Teemo as “low” on relevancy to “wins.” This means that Teemo’s presence in the game has little to no effect on whether you win or lose, giving him a low score in relevance to wins. Then, later when you ask the model to predict whether you’ll win this new Teemo game, it'll want more information.

![Cute, mischievous little raccoon in a bee costume](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t11ozngpwbijfddhedko.png) _Teemo, the “Beemo” skin (Source: [Riot Games](https://www.leagueoflegends.com/en-pl/champions/teemo/))_

# Objective
Other than making glorified Teemo slander, this guide aims to pique your interest in ML concepts through your interests in League. If we can begin by teaching gamers, like you, ML concepts (like what ranking models are through your interests), you can harness the power of a data-driven analysis to show others cool and easy ways AI can be implemented for your projects. 

Therefore, in this guide, we want to answer the following questions using the ML ranking model we are building:
- Which champions increase my likelihood of winning plat/diamond* games? In other words, which champions are meta in my “elo” (meaning skill level) and help me climb?
- Which factors have the greatest impact on whether I win or lose?
- What’s the likelihood that I’ll win a diamond game playing this champion and earning that amount of gold by 10 minutes?

* Note: I generated my dataset using high elo games because I wanted to be taken seriously. 😩 Check out my [Github](https://github.com/witchfelicia/LoL-dataset) if you’d like to learn how to query the [Riot Games API](https://developer.riotgames.com/) to make your own dataset for a more accurate analysis on players in your elo (whether it’s higher or lower 😊).

# Dataset
Most data scientists know that the best models originate from a strong and suitable dataset. For this guide, we painstakingly generated 5000 rows and 14 columns of actual platinum and diamond-tier ranked game data that we carefully selected from parsing responses from the Riot Games API. To download it and make your own data analyses, simply wish upon [this star](https://app.box.com/s/xmtkew2brlnjofocoafr86b92idtkw7a). 🌠

You can find more details about how we generated our dataset from the publicly available League of Legends ranked game data on my [Github](https://github.com/witchfelicia/LoL-dataset) repository. 

![Pretty lady in a veil channeling magic from a book and her staff](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u0ovgme7p7z5pwz2u1u6.png)_Stargazer Soraka (Source: [Riot Games](https://leagueoflegends.fandom.com/wiki/Soraka/LoL/Cosmetics?file=Soraka_StargazerSkin_WR.jpg))_

At a glance, this dataset contains 5000 rows, each containing a player's match info from a platinum or diamond-level ranked game. There are 14 columns, containing data like the player's KDA, champion they picked, ban, if a teammate went Afk, gold at 10 minutes, gold by the end of the game, and whether they won or lost.
Now that we have the data, our next steps are to prepare our data for the model to train.

# Data cleaning
Look out! A tsu-Nami is incoming to wash away excess data 🌊
![Mermaid champion named "Nami" in her cyborg outfit](https://cdn-images-1.medium.com/max/1600/0*QgdqNGlqqvbQloq4) _Nami in her "Program Nami" skin (Source: [Riot Games](https://leagueoflegends.fandom.com/wiki/Nami/LoL/Cosmetics?file=Nami_ProgramSkin.jpg))_

Data cleaning is a step in the process of machine learning to [reduce noise](https://magoosh.com/data-science/what-is-deep-learning-ai/) in your data to **prepare clear** and **concise data for your** ML **model to train on**. What this means is that most of the time, your raw data may contain empty or unrelated rows or columns of data that, if left alone, would confuse your model when it's looking for trends in the rows that lead to an outcome (like winning or losing).

The most difficult part of data cleaning and the next step (feature engineering) is that there is no specific formula or order to do things, which is why it seems so intimidating to get started. Like League of Legends, there's a high learning curve, but you can definitely do it!

As you go through the following steps, always ask yourself "**why** we are performing" this operation, and you'll learn things quickly 😊

## Removing duplicates
At a glance and from how the data was collected from the [Riot Games API](https://developer.riotgames.com/), we gathered data by choosing a random plat or diamond-level player "A", and generating their 20 recent ranked games. However, if our list of 25 players that we gathered data from also contained player A's party member, "B," then there will be games containing player A and B's teammates and opponents that are recorded twice.

Duplicate data can skew prediction results by giving the model an impression that an occurrence of an event happens more frequently. Imagine how falsely a model would predict a Yasuo player's victory if we kept duplicates of a time the one-trick Yasuo main dominated a game.

We can [remove duplicate](https://www.mage.ai/blog/data-cleaning-remove-duplicates) instances, if any, of player A and B's games overlapping by removing rows that contain the same values where players played in the same match, on the same team, and played the same champion.

![Screenshot showing remove duplicate operation](https://cdn-images-1.medium.com/max/1600/0*5ozVoB004VQc7E7F)

## Filter
Sometimes, when we're unsure what data needs to be cleaned, we can go through each of the columns and look for empty, invalid, or incorrect values and [filter](https://www.mage.ai/blog/data-cleaning-filter) them.

There are 5 roles to play in the game, but the "position" feature contains a 6th value, "Invalid," for when the Riot Games API was unable to guess what role a player was playing. (Check the right side of the image below to see the unique values of the column "position")

If we kept the rows with the "Invalid" value in the "position" column, we will be informing the model that players can play the "Invalid" role. We don't want the model to train on inaccurate values, as it will reduce the model's accuracy.

So we will use the [filter](https://www.mage.ai/blog/data-cleaning-filter) operation to keep only the rows where the role played could be determined.

![Show operation for filtering out "Invalid" position from our data](https://cdn-images-1.medium.com/max/1600/0*dWeje-ABJH_KEgFz)

By extension, we have a column representing the amount of the gold earned by their lane opponent at the 10 minute mark, and it contains 35 "0" values. Unfortunately, these 35 rows with empty values in the "ten_min_lane_opponent_gold" column need to be filtered out because the data in these rows are incomplete, and would result in an inaccurate prediction of an outcome.

![Show filter operation for when the ten_min_lane_opponent_gold is 0](https://cdn-images-1.medium.com/max/1600/0*9FKWoCKWnnEqT7p4)

## Removing unrelated columns
Thinking back on our intention for building a model, we're interested in finding out which champions that a player "picks" that maximize their chances of winning. So, we will need to remove unrelated columns. We will come back to this step later, since some of the columns will be removed after extracting relevant information from them.

# Feature engineering
The point 🗡️ of this step is to transform present existing information to be as clear-cut as Fiora's rapier; so, we add columns to specify to the model what good gameplay in ranked matches look like. This also generalizes existing data into fewer, simpler columns.

![Fiora in a witch hat holding her rapier facing off with Nami in a witch Halloween costume](https://cdn-images-1.medium.com/max/1600/0*NOigLuLt5NxWkFVD)_Fiora in her "Bewitching" skin (Source: [Riot Games](https://leagueoflegends.fandom.com/wiki/Fiora/LoL?file=Fiora_BewitchingSkin.jpg))_

## Add column based on a conditional
One method of identifying whether a player is performing well is seeing if they beat their lane opponent at 10 minutes, a crucial turning point of the game. Although this doesn't guarantee their victory, it gives them an edge because it grants them the capacity to use their strength to aid their teammates and pressure the enemy– which can influence whether they win or lose.

Since teamwork is hard to identify and measure, we'll simply label instances where players had an advantage over their lane opponents in the boolean (meaning true or false) column "beat_lane_opponent."

Replacing the "ten_min_gold" column with this boolean column simplifies the numerical range of gold values (the range is 2,223–6,075 gold, to be exact!) into a simple "True" or "False" for whether the player beat their opponent. This value is much easier for the model to understand and determine which factors lead to a favorable outcome.

To [add a new column by comparison](https://www.mage.ai/blog/feature-engineering-add-with-conditional), we simply compare the 2 columns and return "True" if we have beat our opponent.

![Showing the conditional value operation](https://cdn-images-1.medium.com/max/1600/0*4JcX6EQ3zssX_nmC)

## Extract columns from JSON
For those who have experience working with APIs and JSON (a technical term programmers use to describe groups of named textual data), we know that occasionally, we receive data as a JSON and need to write code to dig the relevant data out. If you're wondering why people store data in such a pesky format, explore this [lesson](https://www.mage.ai/blog/feature-engineering-extract-column-from-JSON) about how JSON data types are actually useful in storing auxiliary information.

![Rek'sai, a tunneling monster in League of Legends](https://cdn-images-1.medium.com/max/1600/0*nZYr6oUbpzWGkbQL)_Rek'sai, the digging queen (Source: [Riot Games](https://static.wikia.nocookie.net/leagueoflegends/images/7/71/Rek%27Sai_OriginalSkin.jpg/revision/latest?cb=20181021061755))_

For the scope of this guide, we are simply interested in what data from the "challenges" column that contains JSON would be useful to our goal. Here's an example what kind of information a single row of JSON contains:

![Image of a JSON row that contains info on "deaths by enemy champions," "team damage percentage," etc](https://cdn-images-1.medium.com/max/1600/1*F3IDWBQYuGdJgBiPfGL0Cg.png) _Pretty print generated by [carbon.now.sh](https://carbon.now.sh/)_

Since we want to predict whether a champion is a "carry" and fights well, all we care about is the "teamDamagePercentage."

![Showing extract value from JSON operation by extracting the team damage percentage from the JSON object](https://cdn-images-1.medium.com/max/1600/0*89D-OVeQQU6f2vz0)

## Aggregate sum
Finally, for some Ahri-thmatic calculations!

![Ahri in a school uniform with her peers](https://cdn-images-1.medium.com/max/1600/0*kvidibjLWjAZNhyP)_Academy Ahri skin (Source: [Riot Games](https://static.wikia.nocookie.net/leagueoflegends/images/9/93/Ahri_AcademySkin.jpg/revision/latest/scale-to-width-down/1000?cb=20181021015826))_

One of the main assessments of how well you played in a game is how loaded 🤑 you are compared to your team, a value we dub "gold percentage."

![formula for calculating gold percentage, which is 1 player's gold divided by the team's gold](https://cdn-images-1.medium.com/max/1600/0*waBbR6elsH7cLsRm)

To do this, we need to **aggregate** (fancy term for performing an operation for each group of) matches and teams to **sum** the amount of gold earned by the team in total. This operation is called aggregate total sum, and you can read more about it [here](https://www.mage.ai/blog/feature-engineering-total-sum).

We'll aggregate the "gold" column to find the team's total gold, "team_gold":

![Showing operation for aggregating the sum of a team's gold](https://cdn-images-1.medium.com/max/1600/0*48_k-mnNtkJ0YOHG)

Using the same operation, we can find the sum of "ten_min_gold" and call it "team_ten_min_gold."

![Aggregate sum: sum team's ten minute gold](https://cdn-images-1.medium.com/max/1600/0*8DRGwmEoLp87_S0h)

## Divide column values
With the divisor calculated, we can now divide an individual's gold by the team's to find their "gold_percentage."

![Divide to find team's gold](https://cdn-images-1.medium.com/max/1600/0*6nr32_aujJ7MW_40)

Again, we will do the same by dividing "ten_min_gold" by "team_ten_min_gold" to calculate the column, "gold_ten_min_percentage" that tells us how much a player is contributing in gold at the 10 minute mark.

![Showing how we divide ten minute gold by the team's ten minute gold](https://cdn-images-1.medium.com/max/1600/0*kqbUU-Jshz6Q2VkH)

## Subtract column values
Finally, with the gold percentages, we're interested in seeing whether a player scaled well as the game went on. If a player had a significant gold percentage at 10 minutes, did they leverage the funds they had and "carry" their teammates to victory? Or did they "throw"?

![Ahri, the nine-tailed fox, posing and showing us a finger heart](https://cdn-images-1.medium.com/max/1600/0*r30KR_qGFfrsJpmh)_Ahri throwing out a heart (Source: [Riot Games](https://cdn-images-1.medium.com/max/1600/0*r30KR_qGFfrsJpmh))_

To see whether the player's gold contribution rose or fell, we subtract "gold_percentage" from "gold_ten_min_percentage." I call this feature "scalability."

![Show the difference operation by evaluating "scalability" by subtracting the gold percentages](https://cdn-images-1.medium.com/max/1600/0*9axokNVlfcCmo_pF)

## Last data cleaning step
We mentioned in the previous section that we will remove the columns we either finished extracting information from or created new columns that summarized the data better.

![operation to remove columns](https://cdn-images-1.medium.com/max/1600/0*lTBXY4gWqPKukRaq)

This leaves only 11 columns, which are "bans", "beat_lane_opponent", "deaths", "gold", "gold_percentage", "gold_ten_min_percentage", "kills", "picks", "scalability", "team_damage_percentage", and "win."

Now that we are (finally) done preparing our data, we get to move onto the model training step!

# Model training

A recap of what we're doing with the ranking model:
- We are evaluating which champions (in the "picks" column)
- Have the highest likelihood of winning platinum and diamond-level ranked games
- By sorting our rows of data with the wins on top, we can score each champion for how many wins they got
- Using the relative positioning of each champion score in our sorted list, we can make predictions whether you'll win a ranked game playing a certain champion depending on how high or low a relevant champion placed on the list

## Setup
We know this is confusing, so we created a visualization that helps guide you through the process of selecting what you're ranking and how:

![Visualization of our question: Ranking picks by maximizing wins](https://cdn-images-1.medium.com/max/1600/0*UKfKOiDqkR53_n4j)

## Train-test split
When a machine learning model is being trained, what it means is that it takes the data we painstakingly put together and split a majority of the rows to learn how to predict whether players would win or lose their games playing a certain champion and fulfilling certain performance goals.

To fairly calculate the accuracy of the model, we use approximately 90% of the data for teaching the model how to predict which champions win games and the remaining 10% to test if the model is correct in its predictions.

![showing the train + test split](https://cdn-images-1.medium.com/max/1600/0*TK6otuQzRujRtTdF)_Found in the Review > Statistics tab of Mage_

If you’d like to know more about the process of splitting and get some Sera-tonin by exposing yourself to some K-pop idol girls, this [K-folds cross validation] lesson is perfect for you. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erptkg6ndrgxrvhz85qk.png)_Pop-star Seraphine (Source: [Riot Games](https://static.wikia.nocookie.net/leagueoflegends/images/4/46/Seraphine_KDAALLOUTRisingStarSkin.jpg/revision/latest/scale-to-width-down/1215?cb=20201014003846))_

# Evaluation
Now that the model has been trained, we can now interpret the results of the model. Generally, we evaluate whether a model makes good predictions or not based on the [accuracy, precision, and recall values](https://www.mage.ai/blog/definitive-guide-to-accuracy-precision-recall-for-product-developers). However, since those metrics are general, we also use [SHAP values](https://www.mage.ai/blog/how-to-interpret-explain-machine-learning-models-using-shap-values) to gain insight on how the individual columns affect the outcome.

This is all in an effort to understand and improve model performance in the next retraining step.

## General metrics
The at-a-glance values we rely on to evaluate our model performance are:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vx9thtm76wl52drqj8y.png)
 
Showing accuracy (82.04%), precision (82.04%), recall (82.08%)

Now, a final test of your thresh(hold😏) of enduring my puns! 🎃
![Thresh, a League of Legends character who is a ghost that chains people](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwq0blw35dwqbtdvzrnx.png)_Thresh (Source: [Riot Games](https://www.leagueoflegends.com/en-pl/champions/thresh/))_

During the testing step, we evaluate the metrics we just mentioned by seeing the following testing examples:

- When testing the outcomes of a Thresh player’s games, 82% [accuracy](https://www.mage.ai/blog/model-evaluation-accuracy) means that the model correctly predicted the outcome of a Thresh player’s game results 82% of the time. Including the games they lost.

- [Precision](https://www.mage.ai/blog/model-evaluation-precision) is where the model correctly predicts a Thresh player winning a game.

Thus, an 80% [recall](https://www.mage.ai/blog/model-evaluation-recall) rate means that in the testing set, if there were 5 instances of a Thresh player winning a game, we managed to identify 4 of them.

This is a really technical term, but these three metrics are usually linked to a [confusion matrix](https://www.mage.ai/blog/guide-to-model-metrics-p1-matrix-performance).


![Confusion Matrix](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rev2w2itibzjwlbfpuw.png)

Although this matrix is confusing to some, I’m sure it’s useful to some data scientists out there! 😉

## SHAP values
To find out which columns [influenced the outcome](https://www.mage.ai/blog/how-to-interpret-explain-machine-learning-models-using-shap-values) the most, Mage displays a list of columns and the trend in relation to the outcomes in the Review > Top features tab.
![List of top features that influenced the model. Top three are deaths, gold, gold_percentage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u081mzduhnfinby899y5.png) 

This list tells us a lot:
- Proof that whether players win lane is not correlated to whether they win the game -> since “beat_lane_opponent” didn’t meet the “Top features” list
- Column that influences win rate the most is actually “deaths” -> indicates that decreasing your death count increases your odds of victory more than increasing kills
- Only 1 or 2 correctly predicted outcomes available for each champion in the testing set -> 5000 rows of data wasn’t enough to make predictions on the 140+ champions in League of Legends

Our top features list also tells us which columns had a miniscule influence on the outcome. Since columns like “bans”, “beat_lane_opponent”, and “team_damage_percentage” did not make the list, we can choose to exclude them in our re-train step and [improve our ranking model](https://www.mage.ai/blog/how-to-improve-the-performance-of-a-machine-learning-(ML)-model).

# Deploying API
If you’re curious whether certain values of gold and deaths affect your win rate, you can deploy the API in the Predict > Playground tab to make custom predictions!

In the gif below, we entered “4 deaths” and “7000 gold” and wanted to see a ranked list of champions most likely to win. As you can see, the champions Bard, Nautilus, and Alistar are “picks” that would increase your likelihood of winning with those gold and death counts.
![Playground for entering your custom inputs to get a ranked list of champions who will most likely win](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3asrazj5rcm4p6r6l7o.png) 

# Conclusion
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89l89f23lrnbu6cew3pd.png)_Ghostly rider, Hecarim (Source: [Riot Games](https://www.leagueoflegends.com/en-us/champions/hecarim/)

## Were our questions answered?
After we ran around like headless horsemen (Hecarim) to prepare, train, and evaluate a model, are the results from our ranking model sufficient to answer the questions we asked in the Objective section?

Q: “Which champions increase my likelihood of winning plat/diamond* games?”

A: Although we don’t have enough data to be confident, tentatively, based on only 1 or 2 instances each we will say with a handful of salt:
- Playing Sett, LeBlanc, Fiora, Lux, Shen, and Jax, and
- Not playing Yuumi, Vex, and Yasuo will increase your chances of winning.

Q: “Which factors have the greatest impact on whether I win or lose?”

A: Deaths and gold were the most influential columns, so if you want to win your diamond or plat games, earn gold — learn computer science (CS) 😉– and don’t feed! Wow, such million dollar advice.

Q: “What’s the likelihood that I’ll win a diamond game playing this champion and earning that amount of gold by 10 minutes?”

A: Although we cannot make predictions based on champion “picks”, you can experiment with different inputs (like kills, gold, and deaths) in our Mage Playground to see which champions have the highest chance of winning with those stats.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lkhksqsxszkj83iwdcsh.png)
 
Displayed in the playground is the assertion that Jax, Lucian, and Karthus have the highest chance of winning with 3 deaths, 7000-ish gold, 1 kill, and etc.

## About myself
Since you read all the way to the end, I’d like to share a bit about the inspiration and motivation I had for writing this lengthy piece. I was a participant at the 2018 Riot Games Hackathon where I met [Phreak](https://lol.fandom.com/wiki/Phreak) and an amazing data scientist who introduced me to what data analysis is.

![Fangirling over meeting Phreak!](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3rlbomhkyfiqtjblio0n.png)_Fangirling over meeting Phreak!_

Since then, I strongly believe that there are many like me in 2018 who are intimidated by big buzzwords like machine learning but have curiosity. You read through this guide, so you more or less understand the basic concepts to build and leverage a machine learning model! Unleash those cool ideas 😊

## Begin your data-driven journey 🔮✨
… with Mage? Jk, unless?🥺
Although we used Mage in the above operations, we absolutely believe that there are other methods of doing data analytics. Visuals are important– I would rather display operations with a pretty UI than code to not intimidate non-programmers.

But, as shown in the guide, Mage makes the process of building models easier, so if you are just getting started, I recommend [Mage Academy](https://www.mage.ai/chat)!

We have a plethora of interesting lessons about machine learning concepts. From beginner-friendly intros and advanced topics like this one, becoming an AI-expert has never been easier and more fun.

Whenever you feel ready, you can [build your first model](https://www.mage.ai/). Whenever you need help or want to share the cool ways you leverage data, join our AI community on [Discord](https://www.mage.ai/chat)!

Lastly, hope you enjoyed the article, gamers! Thank you for sticking around until the end 🥰