K = min(train_count, kernel_count) centroids, _ = kmeans(train_x, K) groups, _ = vq(train_x, centroids) mu = np.zeros((K, train_x.shape[1])) sigma = np.zeros((K, train_x.shape[1], train_x.shape[1])) t3 = time() print '완료 (%f seconds)' % (t3 - t2) # # Training # print '평균, 표준편차, Weight 행렬 계산', for j in range(K): selected = train_x[groups[j]] mu[j] = sum(selected) / selected.shape[0] sigma[j] = np.cov(selected.transpose()) + 0.1 * np.eye(train_x.shape[1]) H = tool.get_H(train_x, mu, sigma) Ht = H.transpose() Y = tool.get_Y(train_y) W = np.linalg.lstsq(Ht.dot(H), Ht.dot(Y))[0] tEnd = time() print '완료 (%f seconds)' % (tEnd - t3) print '' print '학습에 쓰인 총 시간: %f seconds' % (tEnd - t0) print ''
centroids, _ = kmeans(train_x, K) groups, _ = vq(train_x, centroids) mu = np.zeros((K, train_x.shape[1])) sigma = np.zeros((K, train_x.shape[1], train_x.shape[1])) t3 = time() print '완료 (%f seconds)' % (t3 - t2) # # Training # print '평균, 표준편차, Weight 행렬 계산', for j in range(K): selected = train_x[groups[j]] mu[j] = sum(selected)/selected.shape[0] sigma[j] = np.cov(selected.transpose()) + 0.1 * np.eye(train_x.shape[1]) H = tool.get_H(train_x, mu, sigma) Ht = H.transpose() Y = tool.get_Y(train_y) W = np.linalg.lstsq(Ht.dot(H), Ht.dot(Y))[0] tEnd = time() print '완료 (%f seconds)' % (tEnd - t3) print '' print '학습에 쓰인 총 시간: %f seconds' % (tEnd - t0) print ''
test_x, test_y = (test_set[0][:test_count], test_set[1][:test_count]) # PCA test_x = learn.pca.dot(test_x.transpose()).transpose() test_count = learn.dimension t1 = time() print '완료 (%f seconds)' % (t1 - t0) # # Test # print '퍼포먼스', H = tool.get_H(test_x, learn.mu, learn.sigma) Y = tool.get_Y(test_y) f = H.dot(learn.W) max_val = f.max(1) max_idx = f.argmax(1) t2 = time() print '완료 (%f seconds)' % (t2 - t1) # # Grade # print '채점', confusion_matrix = np.zeros((10, 10)) for i in range(test_y.shape[0]): confusion_matrix[test_y[i], max_idx[i]] += 1
test_x, test_y = (test_set[0][:test_count], test_set[1][:test_count]) # PCA test_x = learn.pca.dot(test_x.transpose()).transpose() test_count = learn.dimension t1 = time() print '완료 (%f seconds)' % (t1 - t0) # # Test # print '퍼포먼스', H = tool.get_H(test_x, learn.mu, learn.sigma) Y = tool.get_Y(test_y) f = H.dot(learn.W) max_val = f.max(1) max_idx = f.argmax(1) t2 = time() print '완료 (%f seconds)' % (t2 - t1) # # Grade # print '채점', confusion_matrix = np.zeros((10,10)) for i in range(test_y.shape[0]): confusion_matrix[test_y[i], max_idx[i]] += 1