def otherScikitImpl(data, orig_dimension, new_dimension): rp = GaussianRandomProjection(n_components=new_dimension) m = rp._make_random_matrix(new_dimension, orig_dimension) m = np.mat(m) reduced = m * np.mat(data).transpose() reduced = reduced.transpose() return reduced
def otherScikitImpls(data): rp = GaussianRandomProjection(n_components=new_dimension) m = rp._make_random_matrix(new_dimension, orig_dimension) m = np.mat(m) reduced = m * np.mat(data).transpose() reduced = reduced.transpose() return reduced
# ============================================================================= # ---------------------------------------------------------------------------- # ============================================================================= # plt.title("Activities Count") # ax = sns.countplot(x = "Activity", data = df_train_test) # ax.set_xticklabels(ax.get_xticklabels(), rotation = 20, ha="right") # plt.show() # ============================================================================= # ============================================================================ y_pred = [] for j in range(2, 21): creator = GaussianRandomProjection(n_components=j) X = creator._make_random_matrix(j, X_train_df.shape[1]) transformer = CustomGaussianRandomProjection(j, 0.1, X) X_train = transformer.fit_transform(X_train_df) X_test = transformer.fit_transform(X_test_df) k_min = 15 neighbors = np.arange(k_min, 25) train_accuracy = np.empty(len(neighbors)) test_accuracy = np.empty(len(neighbors)) for i, k in enumerate(neighbors): knn = KNeighborsClassifier(n_neighbors=k) knn.fit(X_train, y_train) #Compute accuracy on the training set
def generate(originalDimension, k, epsilon): transformer = GaussianRandomProjection(n_components=k, eps=epsilon) return Matrix(transformer._make_random_matrix(k, originalDimension))
def tf_flat_projection(input_data, proj_idx, size_proj, translation=None): """ Computes a projection of the whole input data flattened over channels and also computes the inverse projection. It samples `size_proj` random directions for the projection using the given `proj_idx`, then translates the affine subspace using the input data centroid. :param input_data: high dimensional input data, type=tf.tensor, shape=(batch_size, rows, cols, channels) :param proj_idx: projection seed, type=int :param size_proj: size of a projection, type=int :return: :param projection: random projection of input_data, type=tf.tensor, shape=(batch_size, size_proj, size_proj, channels) :param projection: inverse projection of input_data given by the Moore-Penrose pseudoinverse of the projection matrix, type=tf.tensor, shape=(batch_size, size, size, channels) """ input_data = tf.cast(input_data, tf.float32) batch_size, rows, cols, channels = input_data.get_shape().as_list() n_features = rows * cols * channels n_components = size_proj * size_proj * channels if translation is None: translation = np.zeros(shape=[1, rows, cols, channels], dtype=np.float32) # projection matrices projector = GaussianRandomProjection(n_components=n_components, random_state=proj_idx) proj_matrix = np.float32( projector._make_random_matrix(n_components, n_features)) pinv = np.linalg.pinv(proj_matrix) # compute projections flat_images = tf.reshape(input_data, shape=[batch_size, n_features]) flat_translation = tf.dtypes.cast(tf.reshape(translation, shape=[1, n_features]), dtype=np.float32) projected_flat_translation = tf.matmul(a=flat_translation, b=proj_matrix, transpose_b=True) projection = tf.matmul(a=flat_images, b=proj_matrix, transpose_b=True) + projected_flat_translation inverse_projection = tf.matmul(a=projection - projected_flat_translation, b=pinv, transpose_b=True) # # check shapes # print(flat_images.shape, proj_matrix.shape, projection.shape) # print(flat_translation.shape, proj_matrix.shape, projected_flat_translation.shape) # exit() # reshape projection = tf.reshape(projection, shape=tf.TensorShape( [batch_size, size_proj, size_proj, channels])) inverse_projection = tf.reshape(inverse_projection, shape=tf.TensorShape( [batch_size, rows, cols, channels])) # inverse_projection = tf.cast(inverse_projection, tf.float32) inverse_projection = mod_invproj(tf.cast(inverse_projection, tf.float32)) return projection, inverse_projection