What I Learned Today

No frills, just learn

Posts in the Coding category

In the first part of the file tutorial2.py of repository Tensorflow in my github, you have the commented version of the MINST tutorial for experts in Tensorflow.

Tensorflow is an opensource API for several languages that act as frontend for a machine learning backend that is quite flexible.

What I was surprised for is the result of this first small exercise, that is: you take the pixel of the MINST package for character recognition traning. You flatten them. A packet of 784 pixel serves as an ordinate of your linear regression, and the numbers from 0 to 9 (actually 10, than you flat it) as abscissa of possible values contained in the pixel packet.

A linear regression fitting the possible outcomes, even with a simple ordinary least squares and a fit a first order degree, results in an accuracy around 92%.

True, is terrible and embarrassing (as Google points out). After all, there only 10 options, missing 1 over 10 is not a great deal, but think about the feature here: image recognition with a linear regression!

I also made a live youtube video about it (in Italian), that received decent following (add 100 views).

Now with the dubious part: It might be this condition what makes neural network possible and feasible?

Now I'm trying to go a step further with nuclear mass models, and while image recognition are all fine and dandy, even a simple mass formula has several fractional exponents and a polynomial expansion to take into account. Fitting a non local density functional is something orders of magnitude more complicated!

In fact one of the limits that I'm noticing with Tensorflow is the choice of optmization algorithms, mostly (all?) based on derivatives (no Nelder-Mead, sadly), thus suited for well behaved systems but less for the complex ridges of energy landscapes.

Another issue is the limited optimization on multicore and massively multicore system. Still I have to manage to compile it under cluster. In my dual socket workstation with 20 cores HT Tensorflow insists in using only at most (but rarely) 4-5 cores...

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/phme2432/public_html/wp-content/plugins/latex/latex.php on line 47

In a two days coding challenge over workers day holiday, I decided to play with maps as means to plot information. What I decided to are use are the widely available shapefiles and python matplotlib packages, using mainly pandas and numpy to analyse data.

You can find everything in this git repository.

First of all you can start familiarising with shapefiles by plotting the pure patches, as in function mP in mapPlot.py.

You need these packages:

import shapefile
from matplotlib.patches import Polygon
import matplotlib
import numpy as np

and you start by reading the file flnm in this way:

sf = shapefile.Reader(flnm)
shapes = sf.shapes()
Nshp = len(shapes)

records all the properties of your map, in particular the shapes attribute (records attribute contains the dataset that varies from file to file) and the length of the shapefile. And you are just need to cycle over the patches to build a Polygon array in matplotlib,

ptchs   = []
for nshp in xrange(Nshp):
    pts     = np.array(shapes[nshp].points)
    prt     = shapes[nshp].parts
    par     = list(prt) + [pts.shape[0]]

    for pij in xrange(len(prt)):

that can be finally plotted in the usual matplotlib way,

fig     = plt.figure()
ax      = fig.add_subplot(111)

ax.add_collection(PatchCollection(patches,facecolor='0.75', edgecolor='w', linewidths=.2))
ax.axis('auto'); ax.axis('off')

to get the final map.

Map of England divided by postcodes sections. From https://borders.ukdataservice.ac.uk/

However, just juxtaposing patches is not ideal. Libraries like basemap have inclusive and sophisticated routines to correcting for coma, geodetic aberrations and have more interesting options regarding plotting scales, legends...etc...

One has to import a little bit more stuff,

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from shapely.geometry import Point, Polygon, MultiPoint, MultiPolygon
from matplotlib.collections import PatchCollection
from descartes import PolygonPatch
import fiona

but the generalization and flexibility gained is worth it.

Use mP_Basemap open the file using fiona,

shp = fiona.open(flnm)
bds = shp.bounds

fiona enables to extract the bounds of map simply with .bounds command, then the bounds can be used as latitude and longitude extremes in basemap plotting routine

m = Basemap(
    ellps = 'WGS84',
    llcrnrlon=bounds[0] - extra * (bounds[2]-bounds[0]),
    llcrnrlat=bounds[1] - extra + 0.01 * (bounds[3]-bounds[1]),
    urcrnrlon=bounds[2] + extra * (bounds[2]-bounds[0],
    urcrnrlat=bounds[3] + extra + 0.01 * (bounds[3]-bounds[1]),

WGS84 is the chosen geodetic coordinate, have a look on the README.md and on the script inside the Map/ directory for further information on how to convert coordinate systems.

After this you can build a dataframe containing not only frames, but also the data of the shapefile, for example the ward name or properties in the case the map is containing those data,

# set up a map dataframe
df_map = pd.DataFrame({
    'poly': [Polygon(xy) for xy in m.map]
    ,'properties': [ward['name'] for ward in m.map_info]
df_map['area_m'] = df_map['poly'].map(lambda x: x.area)
df_map['area_km'] = df_map['area_m'] / 10000.

# draw ward patches from polygons
df_map['patches'] = df_map['poly'].map(lambda x: PolygonPatch(
    ec='#787878', lw=.75, alpha=.9,

to plot, use df_map['patches'] which contains the array of patches.

You can use the ratio between bounds to have a correct figure scaling, and the ellps guarantees you the correct coordinate representation.

Have a look in the function files on github for the complete functions.

After having obtained the dataframe, you can start by using it. You can print m.map_info to realise what type of labels are inside the map properties. More in general I used to import the dictionary keys related to map_info, inside a dataframe in this fashion

for dicti in m.map_info:
    temp_df = temp_df.append(pd.Series(dicti),ignore_index=True)

and with colnames = list(df.columns.values you can eventually get a full list of column names to elaborate and automatise your shapefile analysis.

The map that I use has postcode sectors throughout the whole England, in the column 'label'.  It joins the DataFrame of this map with an imported DataFrame of data, which has postcode sectors  in the column 'Sector',  keeping only the data that appears in both

    i1 = temp_df.set_index('label').index
    i2 = df.set_index('Sector').index

    temp_df = temp_df[i1.isin(i2)]

#Select only the part that corresonds to the imported dataframe of data
    df_map = pd.concat([df_map, temp_df], axis=1, join='inner')

This new map concerns only the part of the map that is also in the imported dataset.

Using Jenks natural breaks algorithm to color the map, and using the dataset of mortgages selected for postcodes over the Greater London area, the result is the following:

Map of Greater London divided by postcodes sections. Colored using Jenks algorithm regarding mortgages quantities in any postcode in Q3 2016.
Map from https://borders.ukdataservice.ac.uk/
Data from CML

The data has been taken from CML in this case, the .xlsx spreadsheet has been analysed with openpyxl in order to extract a DataFrame containing only rows including a name (e.g. London or Manchester) effectively isolating a geographic area together with the intersection above.

The spreadsheet contains then mortgages totals for every quarters between 2012 and 2016 Q3. One can use this data to synthesise an index indicating  the market health.

With only 16 datapoints in the time series any complicated time series analysis (i.e. any type of self regression or machine learning approach based on the time-series) is a steep road. I started by considering the most elementary condition of the market: an ordinary least squares (OLS) for a linear regression.

Many of the most stable results for the best sectors and districts, were, in fact, very well approximating a growing market.

Naively, best conditions you can have is a market which is already stable and has a good capital to start with, and then grows in a stable and sustainable fashion. If an area is deviating from this regime is more risky to invest in, so if the linear fit has an error on slope an intercept these can be simple accounted by subtraction in the index.

For this reason the first index I tried to build out of these data is simply given by the parameters of the OLS: slope , intercept , and errors on slope and intercept .

The index follows the OLS,

where indicates the average value of the slope across the dataset.

This model is not deprived of defects and deficiencies, but is good first approximation and try to this dataset yielding sensible results over a vast geographical area.

The final result (with a primitive scale and the correct aspect ratio enabled) for the London area is in the following figure.

Map of Greater London divided by postcodes sections. Colored using Jenks algorithm regarding index between Q1 2012 and Q3 2016. Map from https://borders.ukdataservice.ac.uk/
Data from

Again, I remind you about the github repository for the complete and hopefully working code (I understand it needs refactoring and debugging, is only a two days challenge) and further details. Please write me for any comment or insult.

Andrea Idini.

Github returns

I am setting up and using the public github repository after many years. I decided to start releasing some old projects publicly, since seems apparent I will never have time to write anymore papers, nor to polish the code.

Eventually, at least for these little "didactic" subprojects, I will start to do little explanatory videos, or blogposts, instead of articles just to save times and make the process more fluid.

So, with the philosophy of releasing as process, not as product, here it is the first project on Pairing Vibration RPA:


Enjoy and let me know.

C# Lists and Dictionary

In C# there are several key differences to C++. Microsoft Website spells them all: https://msdn.microsoft.com/en-us/magazine/cc301520.aspx

Garbage collector instead of deconstructors providing a different approach to micromenagment of memory and lists, jagged arrays and dynamic memory allocation are already available into the language. It supports also negative index to counting from the last element, similarly to Python.

About lists instead of the usual self-made textbook-example class, there are specific embedded classes for Lists and Dictionaries.

List<type> name = new List<type>();

Dictionary<type,type> name = new Dictionary<type,type>();

The Practice of Programming

I started reading "The Practice of Programming" by Kernigan and Pike.

The book hinges on the concepts of simplicity, clarity and generality as foundation of a good code. Eventually automation can bring something to the table generating code from algorithms.

It starts by the usual "obvious" remarks about style: (more…)

The website craft 101

When I kicked on WILT I learned some deal regarding the craft of the website productions.

First of all I struggled with the DNS. I first of all found that exist two type of DNS configurations A and CNAME.

  • A links to an IP-address, in other words the server IP.
  • CNAME links to another http address, connecting via DNS for example http://phme.it to http://www.phme.it.

Then I decided to set up the WordPress network in subdirectory instead of subdomain, and I struggled with the infamous .htaccess
.htaccess is a file that manage the redirects to pages from the server. Playing with .htaccess can guarantee you a nicely addressed (and thus indexable) website, or send you straight to the "circular redirect" hell.
The sintax is still quite obscure to me, but is based on the REGEX expressions.

Finally I had to deal with the graphic presentation of the website, and thus a tiny fraction of the interesting world of CSS. Changing for example the CSS parameters of the header (but not the html) I managed to resize the header and give to the "Pinzolo theme" a more clean and svelte look.

The semantic of Machine Learning

Today I learned again that most things in life are a matter of semantics... After some online lectures in Machine Learning techniques I discovered that what I call "Ordinary Least Squares" is generalized as a "cost function" and a simplified version of the "Newton Method" is refferred to as "Gradient Descent".

So, basically, the core of a supervised learned algorithm seems to be the choose of an appropriate "cost function" and the application of the most effective minimization algorithm.

I learn and forget every tuesday afternoon the power of du and other Linux command line hacks. Hopefully writing this it will stick better in my brain lacking of real estate...
To visualize the dimension of directories without opening the whole tree (and relative nests), the useful command is

du -hs */

with the wildcard that can be substituted by the **/*/ to include a single level of subdirectories (or analogues for including multiple levels and files).

Linfo website is a good manual page source if you want more details.

Do not trust Google Analytics

I learned that Google needs some hours in order to track your website in Analytics.

Even if in the "Tracking Code" section it says "Tracking Code is not Working" reporting a check performed in a given minute, actually sometimes you just have to wait some hours to start "Receiving Data" without furthermore tinkering with your web code.

So just check the source code of your page, and if the Google Analytics snippet is included just give Google the time to update the tracking, without trusting too much the checking section.