-
Notifications
You must be signed in to change notification settings - Fork 0
/
space_reduction.py
73 lines (67 loc) · 2.42 KB
/
space_reduction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import random
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import umap
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from sklearn.neighbors import KNeighborsClassifier
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
from sklearn import preprocessing
import icnn
def normalize(X): #Min Max Scale (Normalize vectors between 0 and 1).
scaler = MinMaxScaler()
scaler.fit(X)
return scaler.transform(X)
def evplot(Xp,y,k=11,p=1,q=1,r=1): #Evaluate transformation using icnnscore + knn classification score
neigh = KNeighborsClassifier(n_neighbors=7)
neigh.fit(Xp, y)
icnnscore = icnn.score(Xp,y,k,p,q,r)
neigscore = round(neigh.score(Xp,y),2)
fscore = np.round((icnnscore+neigscore)/2,2)
print("graph score", fscore)
return round((icnnscore + neigscore)/2,3)
def umapTransfromData(X,y,dim=3): #Straight UMAP function for data transformation
reducer = umap.UMAP(random_state=42, transform_seed=42, n_components=dim)
reducer.fit(X)
return reducer.transform(X)
def umapICNNTransfromData(X,y,dim=3): #Transform and Evaluate UMAP Transformation
#Not supervised UMAP
#metrics = ["euclidean", "manhattan", "chebyshev", "minkowski"]#, "mahalanobis"]
metrics = ["euclidean", "manhattan"]#, "mahalanobis"]
nn = [3,7,17,42]
bestScore = 0
worstScore = 1
tested = []
for i in range(50):
m = random.choice(metrics)
n = random.choice(nn)
if([m,n] in tested):
continue
tested.append([m,n])
reducer = umap.UMAP(random_state=42, transform_seed=42, n_components=dim, metric=m, n_neighbors=n)
reducer.fit(X)
X2 = reducer.transform(X)
score = evplot(X2,y)
if(score > bestScore):
bestScore = score
bestParams = [m,n]
XB = X2.copy()
return XB
def dataPrePro(dataset): #Read Dataset and extract X, y (and related labels to y).
df=pd.read_csv(dataset)
#df=df.drop(df.index[0:1])
#df = df.sample(frac=1, random_state=42) #Shuffle rows
data = df.values
#data = data[0:500,:]
X = data[:,2:]
classes = data[:,0]
le = preprocessing.LabelEncoder()
le.fit(classes)
y = le.transform(classes)
limg = list(data[:,1])
X = normalize(X)
return X,y,classes,le,limg