Source code for pyrltr.analyzer.Metrics

import numpy as np
import scipy.stats as stats


[docs]class Metrics: def __init__(self, dataset, normalizer): self.dataset = np.array(dataset) self.normalizer = np.array(normalizer) self.normalizedData = self.normalizeData()
[docs] def normalizeData(self): assert self.dataset.ndim == 3 assert self.normalizer.ndim == 3 assert self.dataset.shape == self.normalizer.shape return self.dataset.astype(float) / self.normalizer
def _learnedBehaviour(self, dataset): sumOfData = np.sum(dataset, axis=2) return sumOfData
[docs] def learnedBehaviour(self, numberOfSteps): return self._learnedBehaviour(self.dataset[:, :, -numberOfSteps:])
[docs] def learnedNormalizedBehaviour(self, numberOfSteps): return self._learnedBehaviour(self.normalizedData[:, :, -numberOfSteps:])
[docs] def meanVar(self, data): assert data.ndim == 2, "%s == 2" % data.ndim mean = np.mean(data, axis=1) std = np.std(data, axis=1) return np.array(zip(mean, std))
[docs] def meanVarConfidence(self, data, confidenceLevel): assert data.ndim == 2, "%s == 2" % data.ndim print data.shape results = map(lambda x: stats.bayes_mvs(x, alpha=confidenceLevel)[0], data) results = [[row[0], row[1][0], row[1][1]] for row in results] return np.array(results)
[docs] def calculateKSData(self, firstDataset, secondDataset): print firstDataset.shape results = map(self._calculateSingleKSDatapoint, firstDataset, secondDataset) return np.array(results)
def _calculateSingleKSDatapoint(self, firstDataset, secondDataset): """Calculates the Kolomogorov-Smirnov data for one datapoint.""" ks = stats.ks_2samp(firstDataset, secondDataset) ks_d = ks[0] ks_p = ks[1] return ks_d, ks_p def _calculateFilteredLengths(self, data): assert data.ndim == 3, "%s == 3" % data.ndim fastReached = map(lambda x: map(lambda y: filter(lambda z: z < 4, y), x), data) lengths = np.array(map(lambda x: map(lambda y: len(y) + 1, x), fastReached)) return lengths
[docs] def calculateLearningSpeed(self): return self._calculateFilteredLengths(self.dataset)
[docs] def calculateNormalizedLearningSpeed(self): return self._calculateFilteredLengths(self.normalizedData)