def main(): if len(sys.argv) == 1: print("Ejecuta: python3 example2.py [p].") return p = float(sys.argv[1]) X = utils.gen_points3() (x, y) = (X[:, 0], X[:, 1]) sm_x = np.mean(x) sm_y = np.mean(y) m_d = np.array([sm_x, sm_y], dtype = 'float') gm = optimization.gen_mean(X, m_d, p) Y = X.copy() no_eig = 1 Y[:, 0] -= sm_x Y[:, 1] -= sm_y W = optimization.PCA(X, no_eig) _, V = optimization.PCAGM(X, gm, no_eig, W, p) print("PCA (primera direccion):") print(W) print("PCAGM (primera direccion):") print(V) plt.plot(Y[:, 0], Y[:, 1], 'ro', color = 'g') plt.quiver([0], [0], [W[0, 0]], [W[1, 0]], angles = 'xy', scale_units = 'xy', scale = 1, color = ['black']) plt.quiver([0], [0], [V[0, 0]], [V[1, 0]], angles = 'xy', scale_units = 'xy', scale = 1, color = ['b']) plt.axis([-2, 7, -2, 7]) plt.show()
def main(): if len(sys.argv) == 1: print("Ejecuta: python3 example1.py [p].") return p = float(sys.argv[1]) p1 = np.random.multivariate_normal([0, 0], [[0.5, 0], [0, 0.5]], 100) p2 = np.random.multivariate_normal([5, 5], [[0.3, 0], [0, 0.3]], 10) data = np.concatenate((p1, p2), axis=0) (x, y) = (p1[:, 0], p1[:, 1]) x = np.append(x, p2[:, 0]) y = np.append(y, p2[:, 1]) sm_x = np.mean(x) sm_y = np.mean(y) m_d = np.array([sm_x, sm_y], dtype='float') gm = optimization.gen_mean(data, m_d, p) print("Media muestral:") print(m_d) print("Media generalizada:") print(gm) plt.plot(x, y, 'ro') plt.plot(sm_x, sm_y, 'bs', color='g') plt.plot(gm[0], gm[1], 'bs') plt.axis([-2, 7, -2, 7]) plt.show()
def main(): if len(sys.argv) == 1: print("Ejecuta: python3 example3.py [p].") return p = float(sys.argv[1]) X = utils.gen_points_ex() X_wo = utils.gen_points_no_out() no_eig = 1 (x, y) = (X[:, 0], X[:, 1]) sm_x = np.mean(x) sm_y = np.mean(y) m_d = np.array([sm_x, sm_y], dtype='float') gm = optimization.gen_mean(X, m_d, p) W = optimization.PCA(X, no_eig) W_wo = optimization.PCA(X_wo, no_eig) W_0 = np.matrix('0.86607; 0.49991', dtype='float') _, V = optimization.PCAGM(X, gm, 1, W_0, p) PCA_fn = optimization.PCA_J_fn(X, W) print("Resultados sin datos atipicos (PCA).") print(W_wo) print("Resultados con datos atipicos (PCA).") print(W) print("Resultados con datos atipicos (PCAGM).") print(V) plt.plot(X[:, 0], X[:, 1], 'ro', color='g') plt.quiver([0], [0], [W[0, 0]], [W[1, 0]], angles='xy', scale_units='xy', scale=1, color=['black']) plt.quiver([0], [0], [V[0, 0]], [V[1, 0]], angles='xy', scale_units='xy', scale=1, color=['b']) plt.axis([-3, 5, -2, 7]) plt.show() return 0
def main(): if len(sys.argv) == 1: print("Ejecuta: python3 clustering.py [n] [p].") return n_proj = int(sys.argv[1]) p = float(sys.argv[2]) x_train, t_train, _, _ = mnist.load() n, m = 360, 784 data = np.zeros((n, m), dtype='float') labels = np.zeros((n, 1), dtype='int') cnt, idx = 0, 0 while cnt < 300: if t_train[idx] == 3 or t_train[idx] == 8 or t_train[idx] == 9: data[cnt, :] = (x_train[idx, :] / LA.norm(x_train[idx, :], 1)) labels[cnt] = t_train[idx] cnt += 1 idx += 1 while cnt < 360: if t_train[idx] != 3 and t_train[idx] != 8 and t_train[idx] != 9: data[cnt, :] = (x_train[idx, :] / LA.norm(x_train[idx, :], 1)) labels[cnt] = t_train[idx] cnt += 1 idx += 1 W_PCA = optimization.PCA(data, n_proj) data_proj_PCA = data.dot(W_PCA) data_mean = np.zeros((data.shape[1]), dtype='float') for i in range(data.shape[1]): data_mean[i] = np.mean(data[:, i]) gm = optimization.gen_mean(data, data_mean, p) _, W_PCAGM = optimization.PCAGM(data, gm, n_proj, W_PCA, p) data_proj_PCAGM = data.dot(W_PCAGM) k = 3 prec_1 = make_clustering(data_proj_PCA, labels, 3) prec_2 = make_clustering(data_proj_PCAGM, labels, 3) print("Precision de la clasificacion (PCA): ", prec_1) print("Precision de la clasificacion (PCA GM): ", prec_2)
def main(): if len(sys.argv) == 1: print("Ejecuta: python4 example1.py [p].") return p = float(sys.argv[1]) X = utils.gen_points_ex() Y = X.copy() (x, y) = (X[:, 0], X[:, 1]) sm_x = np.mean(x) sm_y = np.mean(y) m_d = np.array([sm_x, sm_y], dtype = 'float') gm = optimization.gen_mean(X, m_d, p) X[:, 0] -= gm[0] X[:, 1] -= gm[1] meanPoints = np.mean(Y, axis = 0) Y = Y - meanPoints linf, lsup = 0.0, 180.0 n = 150 dx = (lsup - linf) / float(n - 1) angle = np.zeros((n), dtype = 'float') y_PCA = np.zeros((n), dtype = 'float') y_PCAGM = np.zeros((n), dtype = 'float') for i in range(n): angle[i] = float(linf) + i * dx r_aux = np.radians(angle[i]) W_aux = np.zeros((2, 1), dtype = 'float') W_aux[0, 0] = np.cos(r_aux) W_aux[1, 0] = np.sin(r_aux) y_PCA[i] = optimization.PCA_J_fn(Y, W_aux) y_PCAGM[i] = optimization.PCAGM_J_fn(X, W_aux, p) plt.plot(angle, y_PCA, color = 'g', label = 'Funcion de costo de PCA') plt.plot(angle, y_PCAGM, color = 'b', label = 'Funcion de costo de PCA robusto') plt.legend(bbox_to_anchor = (0., 1.02, 1., .102), loc = 3, ncol = 2, mode = "expand", borderaxespad = 0.) plt.xlabel('Angulo') plt.ylabel('Valor de la funcion de costo') plt.show()