def cross (pop, cjumps=None, cprob=None): ##returns a new population from crossing cjumps pairs in pop with ##probability cprob ## if not given cjumps, then cjumps = popsize ## if not given cprob, then cprob = 1 #random new pop size [pn,sn] = pop.shape if not cjumps: cjumps = pn if cprob > 0 and cprob <= 1: #if only a percentage of selected parents will produce siblings npn = np.sum(rand.random([cjumps,1]) > cprob) else: #if all selected parents will produce siblings npn = cjumps nupop = np.zeros([npn,sn], dtype=int); #Uniform Crossover for j in np.arange(npn): pi1 = rand.randint(0,pn) pi2 = rand.randint(0,pn) while pi1 == pi2: pi2 = rand.randint(0,pn) p1 = pop[pi1,:] p2 = pop[pi2,:] c = np.array([p1,p2]) sel = rand.random_integers(0,1, [1,sn]); nupop[j,:] = np.choose(sel,c)
def get_batch(X, patchshape, batch_size=200): ix = randint(0, X.shape[1] - patchshape[0]) iy = randint(0, X.shape[2] - patchshape[1]) iz = randint(0, X.shape[3] - patchshape[2]) if patchshape[2] != X.shape[3] else 0 B = X[randint(0, X.shape[0], size=batch_size), ix:ix + patchshape[0], iy:iy + patchshape[1], iz:iz + patchshape[2]].reshape( batch_size, patchshape[0] * patchshape[1] * patchshape[2]) B = B.T B = B - np.mean(B, axis=0) U, S, V = svd(mm(B, B.T)) ZCAMatrix = mm(U, mm(np.diag(1.0 / np.sqrt(S + 1e-5)), U.T)) B = mm(B, ZCAMatrix) return B
def Randint(low, high=None, size = None): assert high is None, 'unimplemented yet' if size is None: #change # global asdf # asdf += 1 # return asdf % low return random.randint(0, low-1) a = np.empty(np.prod(size) if not np.isscalar(size) else size, dtype=int) for i in range(a.size): a[i] = random.randint(0, low-1) #change # global asdf # asdf += 1 # a[i] = asdf % low return a.reshape(size)
# ---------------------------------------------------------------- # # Fourier Transforms # ---------------------------------------------------------------- # np.fft.fft(a) # complex fourier transform of a f = np.fft.fftfreq(len(a)) # fft frequencies np.fft.fftshift(f) # shifts zero frequency to the middle np.fft.rfft(a) # real fourier transform of a np.fft.rfftfreq(len(a)) # real fft frequencies # ---------------------------------------------------------------- # # Rounding # ---------------------------------------------------------------- # np.ceil(a) # rounds to nearest upper int np.floor(a) # rounds to nearest lower int np.round(a) # rounds to neares int # ---------------------------------------------------------------- # # Random Variables # ---------------------------------------------------------------- # from np.random import normal, seed, rand, uniform, randint normal(loc=0, scale=2, size=100) # 100 normal distributed seed(23032) # resets the seed value rand(200) # 200 random numbers in [0, 1) uniform(1, 30, 200) # 200 random numbers in [1, 30) randint(1, 16, 300) # 300 random integers in [1, 16)