def Log_regression_our_method(X_train, X_test, y_train, y_test): for k in xrange(1): # Reduce dimensions in the data from distanceHDR import dim_reduction, dim_reduction_test Level, Train = dim_reduction(X_train, i_dim=X_train.shape[1], o_dim=2, g_size=2) Test = dim_reduction_test(X_test, Level, i_dim=X_train.shape[1], o_dim=2, g_size=2) y_train = y_train.reshape(-1) y_test = y_test.reshape(-1) # Classification inputs = Train.shape[1] classes = int(max(y_train)) y = tflearn.data_utils.to_categorical(y_train - 1, classes) yT = tflearn.data_utils.to_categorical(y_test - 1, classes) # classification model = classification(Train, y, Test, yT, iterate=400, classes=classes)
return C from scipy import stats # Gather the normal and the test samples from the data N = extract_samples(train_dataset, train_labels, 1); T = extract_samples(train_dataset, train_labels, 4); temp_scalar = preprocessing.StandardScaler(with_mean = True, with_std = True).fit(N) N = temp_scalar.transform(N) T = temp_scalar.transform(T) import time start = time.time() # # 1 -- Dimension reduction # Ref, Tree = initialize_calculation(T = None, Data = Xtr_s, gsize = 2,\ # par_train = 0, output_dimension = 4) # # Test, Tree = initialize_calculation(T = Tree, Data = Xte_s, gsize = 2,\ # # par_train = 1, output_dimension = 4) # 2 - Second type of dimension reduction from distanceHDR import dim_reduction, dim_reduction_test start = time.time() Level, Ref = dim_reduction(N, i_dim=N.shape[1], o_dim=5, g_size=2) # print(stats.describe(Ref)) Test = dim_reduction_test(T, Level, i_dim=T.shape[1], o_dim=5, g_size=2) print("The time elapsed is", time.time()-start) print("\nRef", Ref.shape, "Test", Test.shape) print("\nref-ref", traditional_MTS(Ref, Ref, 0).mean()) print("\nref-test", traditional_MTS(Ref, Test, 0).mean())
from sklearn import preprocessing train_dataset = preprocessing.scale(train_dataset) valid_dataset = preprocessing.scale(valid_dataset) test_dataset = preprocessing.scale(test_dataset) print('Training set', train_dataset.shape, train_labels.shape) print('Validation set', valid_dataset.shape, valid_labels.shape) print('Test set', test_dataset.shape, test_labels.shape) # 1 - Nonlinear Dimension reduction from distanceHDR import dim_reduction, dim_reduction_test Train = train_dataset Test = test_dataset print("New dimension reduction") start = time.time() Level, Train = dim_reduction(train_dataset, i_dim=train_dataset.shape[1], o_dim=20, g_size=2) print("Time elapsed", start - time.time()) print("Dimension reduced shape", Train.shape) ## Linear Discriminant Analysis from sklearn.discriminant_analysis import LinearDiscriminantAnalysis from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis lda = LinearDiscriminantAnalysis(solver="svd", store_covariance=True) y_pred = lda.fit(Train, train_labels).predict(Train) print("1 -- LDA") from sklearn.metrics import accuracy_score print(accuracy_score(train_labels, y_pred, normalize=True, sample_weight=None)) qda = QuadraticDiscriminantAnalysis(store_covariances=True) y_pred = qda.fit(Train, train_labels).predict(Train)
dataset = "sensorless" No, y_train, T, y_test = Paper_res_v1.import_pickled_data(dataset) # Transform the train data-set scaler = preprocessing.StandardScaler(with_mean = True,\ with_std = True).fit(No) X_train = scaler.transform(No) X_test = scaler.transform(T) N = 1 sco = np.zeros((1, 9)) print("DR is NDR") n_comp = 4 g_size = 2 for i in tqdm(xrange(N)): #from distanceHDR import dim_reduction, dim_reduction_test Level, Train = dim_reduction(X_train, i_dim=X_train.shape[1], o_dim=n_comp, g_size=g_size) Test = dim_reduction_test(X_test, Level, i_dim=X_train.shape[1], o_dim=n_comp, g_size=g_size) sco[i, :] = comparison_class(Train, y_train, Test, y_test) print("p-values are") print(sco)
def dim_reduction_comparison(dataset, n_comp, g_size): N, y_train, T, y_test = import_pickled_data(dataset) name_1 = ["PCA", "ISOMAP", "LLE", "FA", "KPCA"] dims =[PCA(n_components=n_comp, \ copy=False, whiten=True, \ svd_solver='auto', tol=0.00001, iterated_power='auto', random_state=None), Isomap(n_neighbors=n_comp, n_components=10, eigen_solver='auto',\ tol=0, max_iter=None, path_method='auto', neighbors_algorithm='auto', n_jobs=1), LocallyLinearEmbedding(n_neighbors=5, \ n_components=n_comp, reg=0.001, eigen_solver='auto', tol=1e-06, max_iter=100, \ method='standard', hessian_tol=0.0001, modified_tol=1e-12, \ neighbors_algorithm='auto', random_state=None, n_jobs=1), FactorAnalysis(n_components= n_comp, tol=0.01, \ copy=True, max_iter=1000, noise_variance_init=None,\ svd_method='randomized', iterated_power=3, random_state=0), KernelPCA(n_components= n_comp, kernel='linear', gamma=None, degree=3, \ coef0=1, kernel_params=None, alpha=1.0, \ fit_inverse_transform=False, eigen_solver='auto', tol=0, max_iter=None,\ remove_zero_eig=False, random_state=None, copy_X=True, n_jobs=1), ] # Transform the train data-set scaler = preprocessing.StandardScaler(with_mean = True,\ with_std = True).fit(N) X_train = scaler.transform(N) X_test = scaler.transform(T) N = 1 for n, clf in zip(name_1, dims): scores = np.zeros((N, 9)) print("DR is", n) for i in tqdm(xrange(N)): Train = clf.fit_transform(X_train) Test = clf.transform(X_test) names, scores[i, :] = comparison_class(Train, y_train, Test, y_test) np.savetxt(str(n) + str(dataset) + ".csv", scores) print("score is", scores) scores = np.zeros((N, 9)) print("DR is NDR") for i in tqdm(xrange(N)): #from distanceHDR import dim_reduction, dim_reduction_test Level, Train = dim_reduction(X_train, i_dim=X_train.shape[1], o_dim=n_comp, g_size=g_size) Test = dim_reduction_test(X_test, Level, i_dim=X_train.shape[1], o_dim=n_comp, g_size=g_size) names, scores[i, :] = comparison_class(Train, y_train, Test, y_test) print(scores) np.savetxt("NDR" + str(dataset) + ".csv", scores)
def comparson_dataset(): ## Lets define define an array that can define dimensions D = [800, 1000, 1600, 3200, 6400, 10000] name_1 = ["PCA", "ISOMAP", "LLE", "FA", "KPCA"] dims =[PCA(n_components=9, \ copy=False, whiten=True, \ svd_solver='auto', tol=0.00001, iterated_power='auto', random_state=None), Isomap(n_neighbors=9, n_components=10, eigen_solver='auto',\ tol=0, max_iter=None, path_method='auto', neighbors_algorithm='auto', n_jobs=1), LocallyLinearEmbedding(n_neighbors=5, \ n_components=9, reg=0.001, eigen_solver='auto', tol=1e-06, max_iter=100, \ method='standard', hessian_tol=0.0001, modified_tol=1e-12, \ neighbors_algorithm='auto', random_state=None, n_jobs=1), FactorAnalysis(n_components= 9, tol=0.01, \ copy=True, max_iter=1000, noise_variance_init=None,\ svd_method='randomized', iterated_power=3, random_state=0), KernelPCA(n_components= 9, kernel='linear', gamma=None, degree=3, \ coef0=1, kernel_params=None, alpha=1.0, \ fit_inverse_transform=False, eigen_solver='auto', tol=0, max_iter=None,\ remove_zero_eig=False, random_state=None, copy_X=True, n_jobs=1), ] for element in D: X_train, X_test, y_train, y_test = generate_new_data( (1000 + (element * 2)), element, n_inf=4) start = time.time() # Transform the train data-set scaler = preprocessing.StandardScaler(with_mean = True,\ with_std = True).fit(X_train) X_train = scaler.transform(X_train) X_test = scaler.transform(X_test) N = 2 p = 0 Time = np.zeros((N, len(dims) + 1)) for n, clf in zip(name_1, dims): scores = np.zeros((N, 9)) print("DR is", n) for i in tqdm(xrange(N)): start = time.time() Train = clf.fit_transform(X_train) Test = clf.transform(X_test) names, scores[i, :] = comparison_class(Train, y_train, Test, y_test) Time[i, p] = time.time() - start p = p + 1 np.savetxt(str(n) + str(element) + "acc.csv", scores) print("The value of p after the first set", p) names.append("NDR") scores = np.zeros((N, 9)) for i in tqdm(xrange(N)): #from distanceHDR import dim_reduction, dim_reduction_test start = time.time() Level, Train = dim_reduction(X_train, i_dim=X_train.shape[1], o_dim=4, g_size=2) Test = dim_reduction_test(X_test, Level, i_dim=X_train.shape[1], o_dim=4, g_size=2) print("Train shape", Train.shape, "Test shape", Test.shape) names, scores[i, :] = comparison_class(Train, y_train, Test, y_test) Time[i, p] = time.time() - start np.savetxt(str(element) + "Time.csv", Time)