Good Books for Learning Python

After some hesitation, I finally decided to start with learning Python. I had some hesitations because I am used to R and that is a different kind of cookie. The decision was made easier because I am working on a project with models developed by other colleagues in Python (and Matlab). Furthermore, although Shiny in R is a great way for visualization of results, I got stuck building a combination of results and information. Python offers Dash (some nice examples can be found in the Dash Galley). Another reason is that nowadays you can use Python in your Gams programs, which is great if you have to work on the input data.

I looked for some good books and courses on Python. I tried the DataCamp courses, which are quite good, but I tend to forget the stuff and although they do a good job, the details are often left out. Finally, I decided to use the following two books with great reviews on Amazon:

Learning Python
The first book I really like is a classic: Learning Python by Mark Lutz. It is well written, contains lots of interesting background information that helps me understand the nuts and bolts from Python. If you read a chapter every week, you will be a good Python programmer in less than a year.


Python for Data Analysis
The second book I recommend (and was often recommended by colleagues) is the book by Wes McKinney. It gives you a more or less quick introduction in the world of Numpy, Panda (Wes McKinney brought Pandas to Python), Matplot, and many other tools for Data Science. Although it is not as thorough as the book by Mark Lutz, it gets you started with most of the stuff.

Happy reading and coding!

Starting your Jupyter Notebook with a double-click

Jupyter Notebooks are a great way of working with Python and R. You can add your notes in Markdown and write code in a cell that can be evaluated over and over again.

However, one thing I didn’t like is that you have to start (on Windows) the Jupyter Notebook server in the directory where you have your notebook saved. Double-clicking a Jupyter notebook (with the extension ipynb) doesn’t work if you don’t install an additional package. Luckily, there is an easy way to get this working found on Axel’s blog.

I used the following code to enable double-click on my Windows 10, Python 3.6 machine in the command window :

And afterwards in a command window with administrator rights:

After a restart of my computer I can now start the jupyter notebook server and use double-click on any notebook on my computer.

Looking for infeasibilities in Gams

A good CGE modelers always checks the calibration of a CGE model using a simple trick: If you set the iteration limit of the model to zero and you try to solve the model, GAMS should find a solution if you correctly initialized and wrote down your model equations with the use of the benchmark data. Often, the calibration is not done properly and this can be seen (using the iteration set to zero) by looking at the infeasibilities of the equations and the variables.

If you use MPSGE to write down your model, the infeasibilities are easily interpreted. If the activity variables show infeasibilities the corresponding zero-profit condition is not met, if the price variables show infeasibilities the corresponding market clearing condition is not met. Either the equation, the initialization or the data is wrong.

Looking for infeasibilities in the listing can be daunting. A simple model can have hundreds of variables and finding the infeasibilities can become time consuming. Therefore, I wrote a small Python script that uses some regular expression magic to find the infeasibilities and show them. Below is an example. It shows all variables with an infeasibility greater than 1E-5. The script runs very fast.

Feel free to use and/or improve the code (I just started learning Python). The script looks like this (assuming that the listing file is called “BenchF.lst”):