Showing posts from April, 2012

Python tidbits: inverting the nesting of a nested list.

More than once I came across the problem of rearranging a nested list. I had a nested list of the form X = [['a', 'b', 'c'], ['d', 'e', 'f']] And I want Y = [['a', 'd'], ['b', 'e'], ['c', 'f']] without having to resort to an ugly list comprehension over 3 lines. A friend told me to use Y = zip(*X) So easy! Kind of obvious but I didn't find it on the web. So I thought I'd write it down. Enjoy!

Learning Gabor filters with ICA and scikit-learn

My colleague Hannes works in deep learning and started on a new feature extraction method this week.
As with all feature extraction algorithms, it was obviously of utmost importance to be able to learn Gabor filters.

Inspired by his work and Natural Image Statistics, a great book on the topic of feature extraction from images, I wanted to see how hard it is to learn Gabor filters with my beloved scikit-learn.

I chose independent component analysis, since this is discusses to some depth in the book.

Luckily mldata had some image patches that I could use for the task.

Here goes:

import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import fetch_mldata from sklearn.decomposition import FastICA # fetch natural image patches image_patches = fetch_mldata("natural scenes data") X = # 1000 patches a 32x32 # not so much data, reshape to 16000 patches a 8x8 X = X.reshape(1000, 4, 8, 4, 8) X = np.rollaxis(X, 3, 2).reshape(-1, 8 * 8) # perform I…