Analysis of Chessgames with Python

Rohit Raj
4 min readOct 29, 2023

Lots of websites offer chess insights as a paid feature these days. Even if avail of these paid features, you often do not get the analysis you want.

Chesscom gives insight to its paid members. But it is free on Lichess website. But both of these website lacks certain analysis that I want. So I wrote python code to analyse chessgames from Lichess or Chesscom.

For analysing games using this repo, you can use computer chess engine of your choice. But it is hard to find something better than stockfish chess engine.

Stockfish is the undisputed computer chess champion. It is also free and open source.

Link to my library github repo is given below

You can use the library to analyse your chess games with engines of your choice. It gives the rate of blunders in your games. You also get win-loss statistics for different openings and time control.

The instructions to use the library are given below

  1. Install python

2. git clone https://github.com/sotaaisolutions/chess-accuracy

3 cd to directory

4 pip install -r requirements.txt

5 Download lichess or chesscom games which you want to analyse

6 Install stockfish.

a Download stockfish from here

b Make stockfish binary executable by running the following command

chmod u+x <stockfish file>

7 Download the pgn of your lichess or chesscom games.

8 Go to the folder in step 3 and run the following command in terminal

python script.py -- file input.pgn -- stockfish /path/to/stockfish -- blunder_threshold 100 -- analysis_time 0.1

Blunder threshold and analysis_time parameters are optional. Blunder threshold is value in centipawns above which a bad move will be classed as blunder. Its default value is 100. Analysis_time is time per move which the specified engine will be spending on analysis of each move. I have selected default value is 0.1 second per move.

The script outputs three different files as below:

a. Out.pgn — This is a modified pgn file. Opening information is added to the pgn given as input.

b. Chess.csv — This contains blunders made by white and black in each game of pgn. Moves that worsen the position by more than the blunder threshold are considered as blunder. If the game is already lost then even such a move is not classed as a blunder.

c. Output.xlsx — This contains analysis of chess.csv. Script identifies the player appearing in most games. The analysis is done from the point of view of identified player. Win loss statistics and accuracy score in various situations is calculated.

For example I did analysis of my rapid games in Lichess. Ouput of code is given below

1Total win loss

2 Win loss time control wise

3 Color wise win loss

4 Accuracy

5 Time Control wise accuracy

6 Color wise accuracy

8 Opening wise win loss

9 Date wise win loss

With help of these insights you can find weakness in your games. This will help you become a stronger player.

Sign up to discover human stories that deepen your understanding of the world.

Rohit Raj
Rohit Raj

Written by Rohit Raj

Studied at IIT Madras and IIM Indore. Love Data Science

No responses yet

Write a response