Commit 985c549b authored by Ngocson's avatar Ngocson

RP

parent a602d513
# -*- coding: utf-8 -*-
from imports import *
print("Step 1:\n Load data:")
WineTestData = genfromtxt('DATA/WineTestData.csv', delimiter=',')
WineTestLabel = genfromtxt('DATA/WineTestLabel.csv', delimiter=',')
WineTrainData = genfromtxt('DATA/WineTrainData.csv', delimiter=',')
WineTrainLabel = genfromtxt('DATA/WineTrainLabel.csv', delimiter=',')
print("Wine: Loaded")
MNISTtrainData = genfromtxt('DATA/MNISTtrainData.csv', delimiter=',')
MNISTtrainLabel = genfromtxt('DATA/MNISTtrainLabel.csv', delimiter=',')
MNISTtestData = genfromtxt('DATA/MNISTtestData.csv', delimiter=',')
MNISTtestLabel = genfromtxt('DATA/MNISTtestLabel.csv', delimiter=',')
print("MNIST: loaded")
datasets = [("MNIST",MNISTtestData,MNISTtrainData,MNISTtestLabel,MNISTtrainLabel),
("Wine",WineTestData,WineTrainData,WineTestLabel,WineTrainLabel)]
transformedDatasets = []
name,testData, trainData,testLabel,trainLabel = datasets[1]
print("Step 2: \nFeature reduction:")
N = 30
te_scores = []
tr_scores = []
for n in range(1,N):
print(n)
LDA = LinearDiscriminantAnalysis(n_components = n)
LDA.fit(trainData,trainLabel)
tr_score = LDA.score(trainData,trainLabel)
te_score = LDA.score(testData,testLabel)
te_scores.append(te_score)
tr_scores.append(tr_score)
plt.figure()
plt.plot(range(1,N),te_scores,'r.-',label = "error on the test")
plt.plot(range(1,N),tr_scores,'b.-',label = "error on the train")
plt.title("LDA score")
plt.legend()
plt.savefig("RESULTS/LDA"+name+"score.png")
# -*- coding: utf-8 -*-
from imports import *
print("Step 1:\n Load data:")
WineTestData = genfromtxt('DATA/WineTestData.csv', delimiter=',')
WineTestLabel = genfromtxt('DATA/WineTestLabel.csv', delimiter=',')
WineTrainData = genfromtxt('DATA/WineTrainData.csv', delimiter=',')
WineTrainLabel = genfromtxt('DATA/WineTrainLabel.csv', delimiter=',')
print("Wine: Loaded")
MNISTtrainData = genfromtxt('DATA/MNISTtrainData.csv', delimiter=',')
MNISTtrainLabel = genfromtxt('DATA/MNISTtrainLabel.csv', delimiter=',')
MNISTtestData = genfromtxt('DATA/MNISTtestData.csv', delimiter=',')
MNISTtestLabel = genfromtxt('DATA/MNISTtestLabel.csv', delimiter=',')
print("MNIST: loaded")
datasets = [("MNIST",MNISTtestData,MNISTtrainData,MNISTtestLabel,MNISTtrainLabel),
("Wine",WineTestData,WineTrainData,WineTestLabel,WineTrainLabel)]
transformedDatasets = []
name,testData, trainData,testLabel,trainLabel = datasets[1]
print("Step 2: \nFeature reduction:")
N = 30
te_totscores = np.zeros(N-1).astype(np.float)
tr_totscores = np.zeros(N-1).astype(np.float)
for j in range(50):
te_scores = []
tr_scores = []
for n in range(1,N):
print(n)
rp = random_projection.GaussianRandomProjection(n_components = n)
rp.fit(trainData)
X = testData
Y = rp.transform(X)
X_ = np.matrix(Y)*np.matrix(rp.components_)
te_score = np.linalg.norm(X-X_)/np.linalg.norm(X)
te_scores.append(te_score)
X = trainData
Y = rp.transform(X)
X_ = np.matrix(Y)*np.matrix(rp.components_)
tr_score = np.linalg.norm(X-X_)/np.linalg.norm(X)
tr_scores.append(tr_score)
te_totscores += np.array(te_scores)
tr_totscores += np.array(tr_scores)
plt.figure()
plt.plot(range(1,N),te_totscores/50,'r.-',label = "error on the test")
plt.plot(range(1,N),tr_totscores/50,'b.-',label = "error on the train")
plt.title("ICA error")
plt.legend()
plt.savefig("RESULTS/RP"+name+"error.png")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment