import random # make a random sequence of data of length n that has regular peaks def make_random_sequence(n, period = 10): L = [] for i in range(n): if (i % period == 0): L.append(10*(.5+random.random())) else: L.append(random.random()) return L # make a collection of m random data sequences, each of length n def make_sequence_collection(m, n, period = 10): sequences = [] for i in range(m): sequences.append(make_random_sequence(n, period)) return sequences def get_outliers(S): # compute the average mean_S = sum(S)/len(S) print "the mean is %f" % mean_S for i in range(len(S)): if (S[i] >= 2*mean_S): print "outlier detected at position %d" % i def printList(S): for item in S: print "%0.2f" % item, print n = 15 S = make_random_sequence(n,5) printList(S) get_outliers(S) m = 3 L = make_sequence_collection(m, n, 5) print L for list in L: printList(list) print # compute the average at each of the n positions in m datasets profile = [] for j in range(n): sum_j = 0.0 for i in range(m): sum_j += L[i][j] profile.append(sum_j/m) for item in profile: print "%0.2f" % item, print get_outliers(profile)