7.13 kmeans example wine dataset


We can illustrate the application of k-means to a new dataset which also requires normalisation. The sample dataset contains observations of the chemical analysis of wine. The dataset is available from the UCI Machine Learning repository.

The source dataset is not in our usual csv format so we will need to do a little pre-processing to get it into the form we need.

First download the data from the repository where it consists of two files, the actual data and a file containing a description of the columns.

wget https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data -O wine.data
wget https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.names -O wine.names

The data is in csv format but without a header, and so some command line tools will help transform it. First we get the variable names from the names file.

egrep '^\s+[0-9][0-9]?)' wine.names |
  cut -d')' -f2 |
  tr 'A-Z' 'a-z' |
  awk '{printf("%s,", $1)}' |
  sed 's|,$||' |
  awk '{print}' > wine.header

That’s a bit of command line magic, but breaking it down into one command at a time, it should make sense in combination with seeing the contents of the names file. For reference, the standard Linux commands used include egrep, cut, tr, awk, and sed. The resulting single line header is saved to the file wine.header.

Now we can pre-pend that header file to the data file, after removing the first column which is a wine class variable, using a cut and cat and saving the result to a file wine.csv using a tee as well as displaying the head to the console:

cut -d"," -f2- wine.data |
    cat wine.header - |
    tee wine.csv |
    head -4

The top 4 lines are displayed as below, confirming the dataset is now in the right csv format:


Notice the scale of the different variables. They vary considerably. This is not so good for a distance base algorithm like k-means so we will normalise the data.

cat wine.csv |
    ml normalise kmeans |
    tee norm.csv |
    head -4

Compare the resulting data ranges:


This dataset is ready for a cluster analysis.

Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0