def main(): c = 10 fs = 4 table = {} table[c - 1] = set(f(c - 1)) table[c - 2] = set(f(c - 2)) table[c - 3] = set(f(c - 3)) while 1: print(c) if not c in table: table[c] = set(f(c)) s1, s2, s3, s4 = table[c], table[c - 1], table[c - 2], table[c - 3] if len(s1) == fs and len(s2) == fs and len(s3) == fs and len(s4) == fs: print(c - 3) break c += 1
def main(): c = 10 fs = 4 table = {} table[c-1] = set(f(c-1)) table[c-2] = set(f(c-2)) table[c-3] = set(f(c-3)) while 1: print(c) if not c in table: table[c] = set(f(c)) s1,s2,s3,s4 = table[c],table[c-1],table[c-2],table[c-3] if len(s1)==fs and len(s2)==fs and len(s3)==fs and len(s4)==fs: print(c-3) break c+=1
def train_linear(train_fea, train_tar, test_fea, test_tar, eta, lamb, maxIter, debug) : dataNum = train_fea.shape[0] dimNum = train_fea.shape[1] beta = np.zeros([dimNum+1, 1]) # beta = np.zeros([dimNum, 1]) # Pad leading 1s to the beginging of training data train_fea = padOne(train_fea, 1) test_fea = padOne(test_fea, 1) converged = False curIter = 0 while not converged : curIter = curIter + 1 tmp = f(train_fea, beta) - train_tar new_beta = beta - 2*eta*np.dot(np.transpose(train_fea), tmp) + lamb * beta diff = np.linalg.norm(new_beta - beta, 2) beta = new_beta if debug == 1 : print('Iter: ', curIter, '\t change: ', diff) # evaluate training resutls if curIter % 1000 == 0 : train_res = f(train_fea, beta) test_res = f(test_fea, beta) # calErr(test_res, test_tar) print('Iter: ', curIter, '\t Train Err: ', calErr(train_res, train_tar), '\t Test Err: ', calErr(test_res, test_tar)) if curIter > maxIter : converged = True # train_res = f(train_fea, beta) # for ct1 in range(test_fea.shape[0]) : # print (test_fea[ct1,:]) # test_res = f(test_fea, beta) # print(calErr(test_res, test_tar)) return beta
def to_imgs(body, wheel1, wheel1_center, wheel1_offset, wheel2, wheel2_center, wheel2_offset): iterations = 100 for i in range(iterations): wheel1_rot = rotateImage(wheel1, wheel1_center, 1000 / iterations * i) wheel2_rot = rotateImage(wheel2, wheel2_center, 1000 / iterations * i) wheel1_frame = np.zeros((body.shape[0], body.shape[1], 4), dtype=np.uint8) wheel1_frame[wheel1_offset[0]:wheel1_offset[0] + wheel1_rot.shape[0], wheel1_offset[1]:wheel1_offset[1] + wheel1_rot.shape[1]] = wheel1_rot merged = blend_transparent(body, wheel1_frame) wheel2_frame = np.zeros((body.shape[0], body.shape[1], 4), dtype=np.uint8) wheel2_frame[wheel2_offset[0]:wheel2_offset[0] + wheel2_rot.shape[0], wheel2_offset[1]:wheel2_offset[1] + wheel2_rot.shape[1]] = wheel2_rot merged = blend_transparent(merged, wheel2_frame) shifted = shift_left(merged, 1000 / iterations * i) cv2.imwrite(f('animation/rot_{}.png').format(i), shifted)
beta = np.reshape(beta, (len(tmp), 1)) tmp = mf.readline().rstrip('\n') tmp = tmp.split(',') m = np.asarray(list(map(float, tmp))) tmp = mf.readline().rstrip('\n') tmp = tmp.split(',') s = np.asarray(list(map(float, tmp))) feature = parseTestData(tstFile) feature = normalizePara(feature, m, s) feature = padOne(feature, dim=1) # feature = normalize(feature, axis=0) gndHat = f(feature, beta) # for ct1 in range(gndHat.shape[0]) : # data = np.rint(gndHat[ct1]) # print(int(np.asscalar(data))) submit = open(outFile, 'w') header = ['id', 'label'] submit.write(','.join(header) + '\n') for ct1 in range(gndHat.shape[0]): data = np.rint(gndHat[ct1]) data = int(np.asscalar(data)) cont = [str(ct1 + 1), str(data)] submit.write(','.join(cont) + '\n')
""" Main iteration of the algorithm """ f = f.himmelblau swarm = Swarm(f, param.NPARTICLE) swarm.initialize() g = getGlobalBest(swarm) avg = np.zeros(param.NITERATION) std = np.zeros(param.NITERATION) bfit = np.zeros(param.NITERATION) # plot the level curves r = np.arange(param.RANGE[0] - .2, param.RANGE[1] + .2, 0.05) x, y = np.meshgrid(r, r) z = copy.deepcopy(x) # temporarily for i in range(x.shape[0]): z[i, :] = [f([x[i, j], y[i, j]]) for j in range(x.shape[1])] fig, ax = plt.subplots() ax.contour(x, y, z, 60) plt.xlabel("Variable X") plt.ylabel("Variable Y") # plot the swarm and the global best particle pos = np.matrix([[swarm.particles[j].x[0], swarm.particles[j].x[1]] for j in range(param.NPARTICLE)]) h = ax.plot(pos[:, 0], pos[:, 1], 'ob') # swarm hg = ax.plot(g.x[0], g.x[1], 'xr', markersize=8) # global best for i in range(param.NITERATION): plt.title("Iteration: " + str(i + 1)) for j in range(param.NPARTICLE):
""" 第13章 模块 """ """ 模块就是程序 封装的几个级别: 容器(list等, 是对数据的封装)->函数(对语句的封装)->类(对属性和方法的封装)->模块 保存的每一个.py都是一个独立的模块 模块的作用: 1. 组织代码更有条理; 2. 代码重用 """ # 导入方法1, 不推荐 import util # 同一目录下可直接导入, 不需要当前目录是个package(即没__init__)也行 print(util.x) util.f() o = util.C() o.f() # 起个别名, 强推 import util as ut print(ut.x) ut.f() o = ut.C() o.f() # 容易和当前文件命名冲突, 不建议 from util import C o = C()