The Complete Magazine on Open Source

Using Python for data mining

, / 5561 0

Data Mining illustration

This article presents a few examples on the use of the Python programming language in the field of data mining. The first section is mainly dedicated to the use of GNU Emacs and the other sections to two widely used techniques—hierarchical cluster analysis and principal component analysis.

This article is introductory because some topics such as varimax, oblimin, etc, are not included here and will be discussed in the future. The complete code is too long for a printed article, but is freely available at

The toolbox used in this article is dependent on WinPython and GNU Emacs 24.5 on Windows. My Emacs configuration for the Python language is very simple. The following lines are added to the dot emacs file:

(setq python-indent-guess-indent-offset nil)
    ‘((python . t)))
(add-to-list ‘exec-path “C:\\WinPython-32bit-\\python-3.4.4”)
(global-set-key (kbd “<f8>”) (kbd “C-u C-c C-c”))
(setenv “PYTHONIOENCODING” “utf-8”)
(setenv “LANG”             “en_US.UTF-8”)

The first line is useful to avoid the warning message: ‘Can’t guess python-indent-offset, using defaults: 4’ from Emacs. The next three lines are to use Python in the org-mode, and the last four lines are to use Emacs as an IDE. In the following org file, text, code, figures and a table are present at the same time. This is not very different from a Jupyter Notebook. Each code section can be evaluated with C-c C-c. The export of the whole file as HTML (C-c C-e h h) produces the output shown in Figure 1.

#+title:   PAH method GCMS
#+options: toc:nil
#+options: num:nil
#+options: html-postamble:nil
#+begin_src python :var filename=”method.png” :results file :exports results
#+begin_src python :var filename=”chromatogram.png” :results file :exports results
[[file:chromatogram.png]]#+attr_html: :frame border :border 1 :class center
Figure 1 Python and org-mode

Figure 1: Python and org-mode

Figure 2 Python and Emacs

Figure 2: Python and Emacs

Figure 3 Spyder IDE

Figure 3: Spyder IDE

To do this, it is necessary that Python is recognised by the system. You can do this by going to (Windows 7) Start → Control panel→System→Advanced system settings → Environment variables → User variables for <your-username>→Create, if not present, or modify the variable path→Add C:\WinPython-32bit-\python-3.4.4;
Another method is to use Emacs as an IDE. A Python file can simply be evaluated by pressing the F8 function key (see the above mentioned kbd “<f8>” option). Figure 2 shows an Emacs session with three buffers opened. On the left side is the Python code, on the right side on the top a dired buffer as file manager and on the right side bottom is the Python console with a tabular output. This is not very different from the Spyder IDE (which is included in the WinPython distribution) shown in Figure 3, with the same three buffers opened.

Hierarchical cluster analysis
This example is about agglomerative hierarchical clustering. The data table is the famous iris flower data set and is taken from It has 150 rows and five columns: sepal length, sepal width, petal length, petal width, species’ name (iris setosa from row 1 to 50, iris versicolor from row 51 to 100, iris virginica from row 101 to 150). The code is short, as shown below:

import scipy.cluster.hierarchy as hca
import xlrd
from pylab import *
rcParams[""]=["DejaVu Sans"]
data=array([[w.cell_value(r,c) for c in range(w.ncols)] for r in range(w.nrows)])
figtext(0.5,0.95,”Iris flower data set”,ha=”center”,fontsize=12)
figtext(0.5,0.91,”Dendrogram (center, euclidean, ward)”,ha=”center”,fontsize=10)
Figure 4 Agglomerative hierarchical clustering

Figure 4: Agglomerative hierarchical clustering

First, the table is read as an array with a nested loop; then, column centring is performed. There are various other scaling techniques, column centring is an example of one of them. The ‘S’ in dataS is for ‘scaled’. In this example, the euclidean metric and the ward linkage method are chosen. Many other metrics are available—for example, canberra, cityblock, mahalanobis, etc. There are also many other linkage methods —for example, average, complete, single, etc. Finally, the dendrogram is plotted as shown in Figure 4. In this example, the clusters are coloured by cutting the dendrogram at a distance equal to 10, using the option color_threshold. To enhance its readability, the dendrogram has been condensed a bit using the option truncate_mode.

Figure 5 Scree plot

Figure 5: Scree plot

Figure 6 Scores - PC 1 vs PC 2

Figure 6: Scores – PC 1 vs PC 2

Figure 7 Loadings - PC 1 vs PC 2

Figure 7: Loadings – PC 1 vs PC 2

Principal component analysis
This second example is about three different techniques —matrix algebra, singular value decomposition (SVD) and modular toolkit for data processing (MDP). About the first, the covariance matrix is calculated on the scaled data. Then, Eigenvalues and Eigenvectors are calculated from the covariance matrix. Lastly, the Eigenvalues and Eigenvectors are sorted. The scores are calculated by a dot product with the data scaled and the Eigenvectors. The percentage of variance is explained and its running total is also calculated.

for i in range(0,w.ncols):

The number of components (N), the variance explained by each component (VAR), its percentage (PCT) and the percentage running total (SUM) can be presented as a table. This table can be drawn using the package prettytable. The results are formatted with a certain number of decimal figures and then each column is added to the table.

from prettytable import *
e=[“%.2f” % i for i in eigval]
p=[“%.4f” % i for i in percentage]
r=[“%.2f” % i for i in runtot]

The result is a well-formatted table:

| N  | VAR  |   PCT  |  SUM  |
| 1  | 4.23 | 92.4619| 92.46 |
| 2  | 0.24 | 5.3066 | 97.77 |
| 3  | 0.08 | 1.7103 | 99.48 |
| 4  | 0.02 | 0.5212 | 100.00|

The scree plot is plotted with a simple bar plot type (Figure 5), the scores (Figure 6) and the loadings (Figure 7) with plot. For the scores, the colours are chosen according to the different iris species, because in this example, the data are already categorised.

A bit more complex is the scores plot with clipart, as shown in Figure 8 as an example. The original clipart is taken from, and then processed via ImageMagick. Each clipart is read with imread, zoomed with OffsetImage and then placed on the plot at the scores coordinates with AnnotationBbox, according to the following code:

import matplotlib.image as imread
from matplotlib.offsetbox import AnnotationBbox,OffsetImage
for i,j,o in zip(s1,s2,o):
    if o<51: ib=OffsetImage(i1,zoom=0.75)
    elif o>50 and o<101: ib=OffsetImage(i2,zoom=0.75)
    elif o>100: ib=OffsetImage(i3,zoom=0.75)

The two plots about scores and loadings can be overlapped to obtain a particular plot called the biplot. The example presented here is based on a scaling of the scores as in the following code:

Figure 8 Scores with clipart

Figure 8: Scores with clipart

Figure 9 Biplot

Figure 9: Biplot

Then the loadings are plotted with arrow over the scores, and the result is shown in Figure 9. This solution is based on the one proposed at; it probably is not the best way, but it works.

The 3D plots (Figures 10 and 11) do not present any particular problems, and can be done according to the following code:

from mpl_toolkits.mplot3d import Axes3D
for i,j,h,o in zip(s1,s2,s3,o):
    if o<51: k=”r”
    elif o>50 and o<101: k=”g”
    elif o>100: k=”b”

Using the singular value decomposition (SVD) is very easy—just call pcasvd on the scaled data. The result is shown in Figure 12.

from import pcasvd

The modular toolkit for the data processing (MDP) package (see References 4 and 5) is not included in WinPython; so it’s necessary to download the source MDP-3.5.tar.gz from Then open the WinPython control panel and go to the install/upgrade packages tab. Drag the source file and drop it there. Click on ‘Install packages’. Last, test the installation with the following command:

import mdp

This is a bit time consuming; another test is the following command:

import bimdp

In the following example, the scores are calculated using the singular value decomposition; so the Figures 12 and 13 are equal among them but rotated compared with Figure 6. This has been explained at, where, to quote user Hong Ooi: “The signs of the Eigenvectors are arbitrary. You can flip them without changing the meaning of the result, only their direction matters.”

import mdp
Figure 10 Scores 3D

Figure 10: Scores 3D

Figure 11 Loadings 3D

Figure 11: Loadings 3D

Figure 12 Scores SVD

Figure 12: Scores SVD

Figure 13 Scores MDP

Figure 13: Scores MDP

The MDP package is more complex than described here. Many other things can be done with it. It’s also well documented; for example, the tutorial is more than 200 pages long.

The examples presented here are also typical applications for another, very widely used, free and open source software, R. An interesting comparison of Python and R for data analysis was published some time ago (Reference 7). I can’t make a choice, because I like them both. Currently, I use Python almost exclusively, but in the past, R was my preferred language. It’s useful to develop the same script for both and then compare the results.