It has been shown the Andrews curves are able to preserve means, distance (up to a constant) and variances. Which means that Andrews curves that are represented by functions close together suggest that the corresponding data points will also be close together. Now, we will demonstrate the effectiveness of the Andrew curves on the iris dataset (which we already used here). Let's create a function to compute the values of the functions give a single sample:

import numpy as np def andrew_curve4(x,theta): # iris has 4 four dimensions base_functions = [lambda x : x[0]/np.sqrt(2.), lambda x : x[1]*np.sin(theta), lambda x : x[2]*np.cos(theta), lambda x : x[3]*np.sin(2.*theta)] curve = np.zeros(len(theta)) for f in base_functions: curve = curve + f(x) return curveAt this point we can load the dataset and plot the curves for a subset of samples:

samples = np.loadtxt('iris.csv', usecols=[0,1,2,3], delimiter=',') #samples = samples - np.mean(samples) #samples = samples / np.std(samples) classes = np.loadtxt('iris.csv', usecols=[4], delimiter=',',dtype=np.str) theta = np.linspace(-np.pi,np.pi,100) import pylab as pl for s in samples[:20]: # setosa pl.plot(theta, andrew_curve4(s,theta), 'r') for s in samples[50:70]: # versicolor pl.plot(theta, andrew_curve4(s,theta), 'b') for s in samples[100:120]: # virginica pl.plot(theta, andrew_curve4(s,theta), 'g') pl.xlim(-np.pi,np.pi) pl.show()

In the plot above, the each color used represents a class and we can easily note that the lines that represent samples from the same class have similar curves.

This way is faster, especially with lot of data or data with large dimensionality

ReplyDeletedef andrew_curve4_vec(x,theta):

# iris has 4 four dimensions

tarr=np.array([1/np.sqrt(2.), np.sin(theta), np.cos(theta), np.sin(2. * theta)])

return x.dot(tarr)

...

for s in samples[:20]: # setosa

pl.plot(theta, np.array([andrew_curve4_vec(s,t) for t in theta]), 'r')

...

Hi, I know I'm coming late to the party (almost 2 years later, jeje). I've found some cool stuffs on your blog (this post being one of them).

ReplyDeleteI needed to use Andrew's Curves and made a generalized implementation in Python:

. https://gist.github.com/ryuzakyl/12c221ff0e54d8b1ac171c69ea552c0a

I thought it could be helpful for you. Any comments will be highly appreciated ;).

thank you!

DeleteGood Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

ReplyDeletePython Training in electronic city

DataScience with Python Training in electronic city

AWS Training in electronic city

Big Data Hadoop Training in electronic city

Devops Training in electronic city