def __init__(self, dataset, test_size, metrics, early_stopping_patience, loss, first_data_column=1): self.dataset = dataset self.first_data_column = first_data_column self.test_size = test_size self.metrics = metrics self.early_stopping_patience = early_stopping_patience self.loss = loss data = dataset["data"] self.x = true_divide(data, 255) y = dataset["target"] le = preprocessing.LabelEncoder() le.fit(y) y_numeric = le.transform(y) self.y_hot_encoding = to_categorical(y_numeric).astype(float) self.n_in = len(self.x[1]) self.n_out = len(self.y_hot_encoding[1])
def _compute_IDN(self): denom = 1 + ma.true_divide(np.abs(self._ii - self._jj), self._p.shape[0]) tmp = np.sum(ma.true_divide(self._p, denom)) if not is_mask_constant(tmp, 'IDN'): self._idn = tmp
def _compute_inverse_var(self): tmp = np.sum(ma.true_divide(self._p, (self._ii - self._jj)**2)) if not is_mask_constant(tmp, 'inverse_var'): self._inverse_var = tmp
def _compute_IDMN(self): denom = 1 + ma.true_divide( (self._ii - self._jj)**2, self._p.shape[0]**2) tmp = np.sum(ma.true_divide(self._p, denom)) if not is_mask_constant(tmp, 'IDMN'): self._idmn = tmp
def _compute_homogeneity2(self): tmp = np.sum(ma.true_divide(self._p, (1 + (self._ii - self._jj)**2))) if not is_mask_constant(tmp, 'homogeneity2'): self._homogeneity2 = tmp
def _compute_homogeneity1(self): tmp = np.sum(ma.true_divide(self._p, 1 + np.abs(self._ii - self._jj))) if not is_mask_constant(tmp, 'homogeneity1'): self._homogeneity1 = tmp
def _compute_correlation(self): tmp = ma.true_divide( (np.sum(self._ii * self._jj * self._p) - self._ux * self._uy), self._sigx * self._sigy) if not is_mask_constant(tmp, 'correlation'): self._correlation = tmp
def compute_basic_stats(self): if np.count_nonzero(self._p) == 0: print '::O_O::GLCM matrix is all ZEROS! no need for any further computation!' self._is_p_zeros = True else: #TODO: make all math opearation working with 3D glcm matrix instead of 2D #Ensure the glcm matrix is normalized to probability matrix self._p = ma.true_divide(self._p, np.sum(self._p)) self._px = np.sum(self._p, axis=1) self._py = np.sum(self._p, axis=0) self._ii, self._jj = np.ogrid[0:self._p.shape[0], 0:self._p.shape[1]] self._ux = np.sum(self._ii * self._p) self._uy = np.sum(self._jj * self._p) # print 'after ux, uy' self._sigx = np.sum(((self._ii - self._ux)**2) * self._p) self._sigy = np.sum(((self._jj - self._uy)**2) * self._p) # print 'after sigx sigy' self._k1 = np.arange(0, 2 * self._p.shape[0] - 1, 1) self._p_xplusy = np.zeros(self._k1.shape) for kk in self._k1: tmp = (self._ii + self._jj == kk) self._p_xplusy[kk] = ma.sum(self._p[tmp]) del tmp # print 'after p_xplusy' self._k2 = np.arange(0, self._p.shape[0], 1) self._p_xminusy = np.zeros(self._k2.shape) for kk in self._k2: tmp = (np.abs(self._ii - self._jj) == kk) self._p_xminusy[kk] = np.sum(self._p[tmp]) del tmp # print 'after p xminusy' tmp = -np.sum(self._px * ma.log2(self._px)) if not is_mask_constant(tmp, 'Hx'): self._Hx = tmp del tmp # print 'after Hx' tmp = -np.sum(self._py * ma.log2(self._py)) if not is_mask_constant(tmp, 'Hy'): self._Hy = tmp del tmp # print 'after Hy' tmp = -np.sum(self._p * ma.log2(self._p)) if not is_mask_constant(tmp, 'H'): self._H = tmp del tmp pxx, pyy = np.meshgrid(self._px, self._py, indexing='ij') tmp = -np.sum(self._p * ma.log(pxx * pyy)) if is_mask_constant(tmp, 'Hxy1'): self._Hxy1 = tmp del tmp # print 'after Hxy1' tmp = -np.sum(pxx * pyy * ma.log(pxx * pyy)) if not is_mask_constant(tmp, 'Hxy2'): self._Hxy2 = tmp del tmp