def class_loss(d): r''' Calculate discrete class loss for every instance in d. The resulting array contains 1 for instance incorrectly classified, 0 otherwise: >>> d = DataSet(X=helpers.to_one_of_n([0, 0, 0, 1, 1, 2]), ... Y=helpers.to_one_of_n([0, 0, 1, 0, 1, 2])) >>> class_loss(d) array([ 0., 0., 1., 1., 0., 0.]) ''' assert d.nfeatures == d.nclasses return np.any( helpers.hard_max(d.X) != helpers.hard_max(d.Y), axis=0).astype(float)
def auc(d): ''' Calculate area under curve of the ROC for a dataset d. Expects the predictions for a two-class problem. ''' assert d.nclasses == 2 and d.nfeatures == 2 return stat.auc(np.diff(d.data, axis=0)[0], helpers.hard_max(d.labels)[1])
def ninstances_per_class(self): if self.labels.shape[0] == 1 and self.labels.dtype == np.int: return [len(np.flatnonzero(self.labels[0,:] == l)) for l in self.possible_labels] elif self.labels.dtype == np.bool: return np.sum(self.labels, axis=1).astype(int).tolist() else: return np.sum(helpers.hard_max(self.labels), axis=1).astype(int).tolist()
def class_loss(d): r''' Calculate discrete class loss for every instance in d. The resulting array contains 1 for instance incorrectly classified, 0 otherwise: >>> from psychic import * >>> d = DataSet(data=helpers.to_one_of_n([0, 0, 0, 1, 1, 2]), ... labels=helpers.to_one_of_n([0, 0, 1, 0, 1, 2])) >>> perf.class_loss(d) array([ 0., 0., 1., 1., 0., 0.]) ''' assert d.nfeatures == d.nclasses if d.labels.shape[0] == 1 and d.labels.dtype == np.int: return (np.argmax(d.data, axis=0) != d.labels[0,:]).astype(np.float) else: return np.any( helpers.hard_max(d.data) != helpers.hard_max(d.labels), axis=0).astype(float)
def ninstances_per_class(self): return np.sum(helpers.hard_max(self.Y), axis=1).astype(int).tolist()
def conf_mat(d): ''' Make a confusion matrix. Rows contain the label, columns the predictions. ''' return np.dot(helpers.hard_max(d.labels), helpers.hard_max(d.data).T)