def BAHSICRaw(self, x, y, kernelx, kernely, flg3, flg4): assert len(x.shape) == 2, 'Argument 1 has wrong shape' assert len(y.shape) == 2, 'Argument 2 has wrong shape' assert x.shape[0] == y.shape[0], \ 'Argument 1 and 2 have different number of data points' print '--initializing...' hsic = CHSIC() L = kernely.Dot(y, y) setdiag0(L) sL = numpy.sum(L, axis=1) ssL = numpy.sum(sL) n = x.shape eliminatedI = [] selectedI = set(numpy.arange(n[1])) kernelx.CreateCacheKernel(x) while True: selectedI = selectedI - set(eliminatedI) sI = numpy.array([j for j in selectedI]) m = len(sI) print m if (m == 1): eliminatedI.append(selectedI.pop()) break objj = [] for j in selectedI: K = kernelx.DecDotCacheKernel(x, x[:,[j]]) setdiag0(K) objj.append(hsic.UnBiasedHSICFast(K, L, sL, ssL)) if m > flg3: maxj = numpy.argsort(objj) num = int(flg4 * m)+1 if m-num <= flg3: num = m - flg3 maxj = maxj[m:m-num-1:-1] else: maxj = numpy.array([numpy.argmax(objj)]) j = numpy.take(sI,maxj) eliminatedI.extend(j) kernelx.DecCacheKernel(x, x[:,j]) kernelx.ClearCacheKernel(x) return eliminatedI
def BAHSICRaw(self, x, y, kernelx, kernely, flg3, flg4): assert len(x.shape) == 2, 'Argument 1 has wrong shape' assert len(y.shape) == 2, 'Argument 2 has wrong shape' assert x.shape[0] == y.shape[0], \ 'Argument 1 and 2 have different number of data points' print '--initializing...' hsic = CHSIC() L = kernely.Dot(y, y) setdiag0(L) sL = numpy.sum(L, axis=1) ssL = numpy.sum(sL) n = x.shape eliminatedI = [] selectedI = set(numpy.arange(n[1])) kernelx.CreateCacheKernel(x) while True: selectedI = selectedI - set(eliminatedI) sI = numpy.array([j for j in selectedI]) m = len(sI) print m if (m == 1): eliminatedI.append(selectedI.pop()) break objj = [] for j in selectedI: K = kernelx.DecDotCacheKernel(x, x[:, [j]]) setdiag0(K) objj.append(hsic.UnBiasedHSICFast(K, L, sL, ssL)) if m > flg3: maxj = numpy.argsort(objj) num = int(flg4 * m) + 1 if m - num <= flg3: num = m - flg3 maxj = maxj[m:m - num - 1:-1] else: maxj = numpy.array([numpy.argmax(objj)]) j = numpy.take(sI, maxj) eliminatedI.extend(j) kernelx.DecCacheKernel(x, x[:, j]) kernelx.ClearCacheKernel(x) return eliminatedI
def UnBiasedHSIC(self, x, y, kernelx=vector.CLinearKernel(), \ kernely=vector.CLinearKernel()): nx = x.shape ny = y.shape assert nx[0] == ny[0], \ "Argument 1 and 2 have different number of data points" kMat = kernelx.Dot(x,x) setdiag0(kMat) lMat = kernely.Dot(y,y) setdiag0(lMat) sK = kMat.sum(axis=1) ssK = sK.sum() sL = lMat.sum(axis=1) ssL = sL.sum() return ( kMat.__imul__(lMat).sum() + \ (ssK*ssL)/((nx[0]-1)*(nx[0]-2)) - \ 2 * sK.__imul__(sL).sum() / (nx[0]-2) \ ) / (nx[0]*(nx[0]-3))
def UnBiasedHSIC(self, x, y, kernelx=vector.CLinearKernel(), \ kernely=vector.CLinearKernel()): nx = x.shape ny = y.shape assert nx[0] == ny[0], \ "Argument 1 and 2 have different number of data points" kMat = kernelx.Dot(x, x) setdiag0(kMat) lMat = kernely.Dot(y, y) setdiag0(lMat) sK = kMat.sum(axis=1) ssK = sK.sum() sL = lMat.sum(axis=1) ssL = sL.sum() return ( kMat.__imul__(lMat).sum() + \ (ssK*ssL)/((nx[0]-1)*(nx[0]-2)) - \ 2 * sK.__imul__(sL).sum() / (nx[0]-2) \ ) / (nx[0]*(nx[0]-3))
def BAHSICOpt(self, x, y, kernelx, kernely, flg3, flg4): assert len(x.shape) == 2, 'Argument 1 has wrong shape' assert len(y.shape) == 2, 'Argument 2 has wrong shape' assert x.shape[0] == y.shape[0], \ 'Argument 1 and 2 have different number of data points' print '--initializing...' hsic = CHSIC() L = kernely.Dot(y, y) setdiag0(L) sL = numpy.sum(L, axis=1) ssL = numpy.sum(sL) n = x.shape eliminatedI = [] selectedI = set(numpy.arange(n[1])) kernelx.CreateCacheKernel(x) sga = kernelx._typicalParam sgaN = sga.shape sgaN = sgaN[0] while True: selectedI = selectedI - set(eliminatedI) sI = numpy.array([j for j in selectedI]) m = len(sI) print m if (m == 1): eliminatedI.append(selectedI.pop()) break sgaMat = [] hsicMat = [] for k in range(sgaN): ## bfgs in scipy is not working here retval = optimize.fmin_cg(hsic.ObjUnBiasedHSIC, \ sga[[k],].ravel(), \ hsic.GradUnBiasedHSIC,\ args=[x, kernelx, L, sL, ssL], \ gtol=1e-6, maxiter=100, \ full_output=True, disp=False) sgaMat.append(retval[0]) hsicMat.append(retval[1]) k = numpy.argmin(hsicMat) sga0 = sgaMat[k] objj = [] for j in selectedI: K = kernelx.DecDotCacheKernel(x, x[:,[j]], sga0) setdiag0(K) objj.append(hsic.UnBiasedHSICFast(K, L, sL, ssL)) if m > flg3: maxj = numpy.argsort(objj) num = int(flg4 * m)+1 if m - num <= flg3: num = m - flg3 maxj = maxj[m:m-num-1:-1] else: maxj = numpy.array([numpy.argmax(objj)]) j = numpy.take(sI,maxj) eliminatedI.extend(j) kernelx.DecCacheKernel(x, x[:,j]) kernelx.ClearCacheKernel(x) return eliminatedI
def BAHSICOpt(self, x, y, kernelx, kernely, flg3, flg4): assert len(x.shape) == 2, 'Argument 1 has wrong shape' assert len(y.shape) == 2, 'Argument 2 has wrong shape' assert x.shape[0] == y.shape[0], \ 'Argument 1 and 2 have different number of data points' print '--initializing...' hsic = CHSIC() L = kernely.Dot(y, y) setdiag0(L) sL = numpy.sum(L, axis=1) ssL = numpy.sum(sL) n = x.shape eliminatedI = [] selectedI = set(numpy.arange(n[1])) kernelx.CreateCacheKernel(x) sga = kernelx._typicalParam sgaN = sga.shape sgaN = sgaN[0] while True: selectedI = selectedI - set(eliminatedI) sI = numpy.array([j for j in selectedI]) m = len(sI) print m if (m == 1): eliminatedI.append(selectedI.pop()) break sgaMat = [] hsicMat = [] for k in range(sgaN): ## bfgs in scipy is not working here retval = optimize.fmin_cg(hsic.ObjUnBiasedHSIC, \ sga[[k],].ravel(), \ hsic.GradUnBiasedHSIC,\ args=[x, kernelx, L, sL, ssL], \ gtol=1e-6, maxiter=100, \ full_output=True, disp=False) sgaMat.append(retval[0]) hsicMat.append(retval[1]) k = numpy.argmin(hsicMat) sga0 = sgaMat[k] objj = [] for j in selectedI: K = kernelx.DecDotCacheKernel(x, x[:, [j]], sga0) setdiag0(K) objj.append(hsic.UnBiasedHSICFast(K, L, sL, ssL)) if m > flg3: maxj = numpy.argsort(objj) num = int(flg4 * m) + 1 if m - num <= flg3: num = m - flg3 maxj = maxj[m:m - num - 1:-1] else: maxj = numpy.array([numpy.argmax(objj)]) j = numpy.take(sI, maxj) eliminatedI.extend(j) kernelx.DecCacheKernel(x, x[:, j]) kernelx.ClearCacheKernel(x) return eliminatedI