def DrawRBFContour(centers, w_lin, data, xmin, xmax, ymin, ymax): start_time = time.time() xlist = np.linspace(xmin, xmax, 250) ylist = np.linspace(ymin, ymax, 250) X, Y = np.meshgrid(xlist, ylist) Z = [] for x_row, y_row in zip(X, Y): z_row = zip(x_row, y_row) Z.append(z_row) new_Z = [] k = len(centers) r = 2 / (np.sqrt(k)) for z_row in Z: row = [ RBF_Classify(w_lin, centers, test_point, r) for test_point in z_row ] new_Z.append(row) #plt.figure() cp = plt.contourf(X, Y, new_Z) plt.colorbar(cp) plt.title('RBF Plot') plt.xlabel('X1') plt.ylabel('X2') cls.draw_data(data) print("--- %s seconds ---" % (time.time() - start_time)) plt.show()
def DrawNNContour(k, data, trans, xmin, xmax, ymin, ymax, use_sort): start_time = time.time() xlist = np.linspace(xmin, xmax, 250) ylist = np.linspace(ymin, ymax, 250) X, Y = np.meshgrid(xlist, ylist) Z=[] for x_row, y_row in zip(X,Y): z_row=zip(x_row,y_row) Z.append(z_row) new_Z=[] if trans: trans_data = [(Transform(x),y) for x,y in data] for z_row in Z: row=[KNearestNeighbor(k, trans_data, Transform(test_point), use_sort) for test_point in z_row] new_Z.append(row) else: for z_row in Z: row=[KNearestNeighbor(k, data, test_point, use_sort) for test_point in z_row] new_Z.append(row) #plt.figure() cp = plt.contourf(X, Y, new_Z) plt.colorbar(cp) plt.title('NN Plot') plt.xlabel('X1') plt.ylabel('X2') cls.draw_data(data) print("--- %s seconds ---" % (time.time() - start_time)) plt.show()
def DrawGGContour(data, xmin, xmax, ymin, ymax): xlist = np.linspace(xmin, xmax, 100) ylist = xlist**3 plt.plot(xlist, ylist, "g-") x_1 = [0, 0.00001] y_1 = [ymin, ymax] plt.xlim(-1.2, 1.2) plt.ylim(-1.2, 1.2) plt.plot(x_1, y_1, "b-") cls.draw_data(data) plt.show()
def DrawLinearContour(data, xmin, xmax, ymin, ymax): start_time = time.time() xlist = np.linspace(xmin, xmax, 250) ylist = np.linspace(ymin, ymax, 250) X, Y = np.meshgrid(xlist, ylist) Z=np.sign(X+2*Y+2.23) #plt.figure() cp = plt.contourf(X, Y, Z) plt.colorbar(cp) plt.title('NeuralNet Plot') plt.xlabel('X1') plt.ylabel('X2') cls.draw_data(data) print("--- %s seconds ---" % (time.time() - start_time)) plt.show()
def DrawLegendreContour(order, g, train_data, test_data): ##Draw contour============================== print "Drawing Contour..." x1_list = np.arange(-1.2, 1.2, 0.01) x2_list = np.arange(-1.2, 1.2, 0.01) X1, X2 = np.meshgrid(x1_list, x2_list) Z = [] transformed = [LegendreTransform(order, x1, x2) for x1, x2 in zip(X1, X2)] for x in transformed: weighted = sum([coef * feature for coef, feature in zip(g, x)]) Z.append(weighted) ##============================================= cls.draw_data(train_data) plt.contour(X1, X2, Z, [0]) plt.axis([-1.1, 1.1, -1.1, 1.1]) plt.show() cls.draw_data(test_data) plt.contour(X1, X2, Z, [0]) plt.axis([-1.1, 1.1, -1.1, 1.1]) plt.show()
max_iteration = 10000 accuracy_threshold = 0.999 ##================================================ ## Data preprocessing===================================== training_data = extractDigits("ZipDigits.train") test_data = extractDigits("ZipDigits.test") raw_train_labeled_data = labelImageData(training_data, d1, d2, features.horiAsymmetry, features.vertAsymmetry) raw_test_labeled_data = labelImageData(test_data, d1, d2, features.horiAsymmetry, features.vertAsymmetry) if see_train: cls.draw_data(raw_train_labeled_data) else: cls.draw_data(raw_test_labeled_data) if not tran: trans_train_data = raw_train_labeled_data trans_test_data = raw_test_labeled_data else: trans_train_data = thirdOrderTransfrom(raw_train_labeled_data) trans_test_data = thirdOrderTransfrom(raw_test_labeled_data) ##=========================================================== ##Learning========================================================= g = regressionAndPocket(trans_train_data, accuracy_threshold, max_iteration) ##===========================================================