You shouldn't forget PCA

PCA is one of the most basic but not less important data analysis techniques. Before entering the topic in a technical and mathematical sense, let's highlight some real-life applications and use cases.

So first of all, let's consider a daily use case for applied data science and data engineering: Once in a while, even the laziest cat brings their owner dead mouses and other small animals from their hunting adventures. Without any intention to destroy that very kind natural behavior, preventing your cat from entering the flat without its quarry would be quite a nice feature for your door.

This means, before entering the house through the catflap, the cat has to be checked if it has some suspicious thing in its mouth. This is the most straightforward approach to solve that task. And that is why images are our data analysis basis for further processing of that problem.

To come back to the initial point: PCA can be used as kind of analysis strategy to identify those crititcal properties in the data, or more precisely here, in the images.

So what is an image? An image can be interpreted in various ways, but a matrix interpretation might be the most evident one. In a very simple form: as a two-dimensional matrix. Referring to image vocabulary this corresponds to height and width, where the latter ones are symbolizing the number of rows (~ height) and the number of columns (~ width). As a consequence the number of an image's pixels is the same as number of rows times number of columns. To sum up this part, figure 1 shows a data sample with 200 x 200 pixels for both, the number of rows and columns.

Grayscaled cat image
Fig.1 - Grayscaled image of a cat.

By introducing this, a single data sample is defined as a matrix. In this case, a grayscale image is expected. This means an image's pixel itself is something between black and white. In other words, its numerical value is something between 0 and 1, where 0 also means black and 1 means white, commonly known as grayscale image. This data modeling results in a two-dimensional plot looks like in figure 1 - basically: an image. Counting starts at the left top corner of the image. Here the grayscale value is almost white, corresponding to value almost 1.0 at the very top left in figure 2. Contrary, the right bottom pixel is quite dark, therefore heading towards 0.0 on the right in the surface plot in figure 2.

Fig.2 - 3 dimensional representation of the image from figure 1.

Another way of visualizing the same data sample is just a three-dimensional plot through reaching the third dimension, extending the grayscale values from figure 1 onto the z-axis, like in figure 2. Obviously, the human eye prefers the more natural presentation on the top.

In comparison: The data sample in figure 3 contains almost the same information. Like the background or the cat itself occupies quite an fullfilling part of the image. But there is an important difference in it, which covers roughly 10 percent of the image. Without any further investigation on what the cat has in its mouth, we surely do not want to have that entering. To conclude, we turning the given problem into a classification task: pass/no pass (OK/NOK, 0/1).

Fig.3 - Grayscaled image of a cat, with NOK content inside.

The question now is how to detect and consecuentially react on the detection's result? There are multiple ways to handle this. A straightforward approach could be just preprocessing the incoming image by extracting a smaller image range in the middle and count the number of dark pixels. When reaching some threshold, let's say 50% of the centric cutted image's pixels are dark, where dark is defined as pixel value greater than 0.5, an (NOK, 1) event is detected and we shut the door. Besides the fact the actually shown image is already preprocessed, this might fail with the slightest movement of the cat's head into any direction.

Another even further preprocessing-free solution then could look like just summing up the number of pixel values, in case of of exceeding some threshold. Due to the assumption that the hunted quarry is always dark, we might shut the door again. The problem here is, that variances in lighting might heavily lead to performance issues. This could be counteracted by adopting weather conditions and time to the threshold. Nevertheless, this means creating a lot of thresholds and check for multiple conditions manually. Something that sounds instable by defintion and therefore no valid approach neither.

A lot of approaches are possible here and applied PCA is one of them. Applied means, PCA is integrated into an approach called Eigenfaces. The concept will be highlighted in the upcoming articles. The Eigenface-Algorithm will be used to classify the image into two categories: OK/NOK, 0/1