Commit 227ce90e authored by Ngocson's avatar Ngocson

ic

parent f14bc732
No preview for this file type
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
#from imports import *
import numpy as np
from numpy import genfromtxt
import csv
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.decomposition import FastICA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn import random_projection
from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture
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)
]
transformedDatasets = []
name,testData, trainData,testLabel,trainLabel = datasets[0]
print("Step 2: \nFeature reduction:")
N = 100
te_scores = []
tr_scores = []
for n in range(2,N):
print(n)
ica = FastICA(n_components = n,
tol = 5e-4,
max_iter = 500)
ica.fit(trainData)
X = testData
Y = ica.transform(X)
X_ = ica.inverse_transform(Y)
te_score = np.linalg.norm(X-X_)/np.linalg.norm(X)
te_scores.append(te_score)
X = trainData
Y = ica.transform(X)
X_ = ica.inverse_transform(Y)
tr_score = np.linalg.norm(X-X_)/np.linalg.norm(X)
tr_scores.append(tr_score)
plt.figure()
plt.plot(range(2,N),te_score,'r.-',label = "error on the test")
plt.plot(range(2,N),tr_score,'b.-',label = "error on the train")
plt.title("ICA error")
plt.legend()
plt.savefig("RESULTS/ICAMNISTerror.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