-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfmnist.py
More file actions
95 lines (67 loc) · 3.23 KB
/
fmnist.py
File metadata and controls
95 lines (67 loc) · 3.23 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from sklearn.model_selection import GridSearchCV
import vectorization as vect
from auxiliary_functions import *
from numpy.random import seed
import pickle
s=1
seed(s)
normalization=False
n_iters = 100
from fashion_mnist import mnist_reader
path_feat = "fashion_mnist/features/"
path_diag= "fashion_mnist/pdiagrams/"
path_results = "results/"
vec_parameters = dict()
vec_parameters['GetPersStats']=(),
vec_parameters['GetAlgebraicFunctions']=(),
vec_parameters['GetEntropySummary'] = [[15,30,50]]
vec_parameters['GetBettiCurveFeature'] = [[15,30,50]]
vec_parameters['GetPersLifespanFeature'] = [[15,30,50]]
vec_parameters['GetAtolFeature'] = [[2,4,8,16]]
vec_parameters['GetPersImageFeature'] = [[0.05,0.5,1],[3,6,12,20]]
vec_parameters['GetPersSilhouetteFeature'] = [[15,30,50], [0,1,2,5]]
vec_parameters['GetComplexPolynomialFeature'] = [[3, 5, 10],['R', 'S', 'T']]
vec_parameters['GetPersLandscapeFeature'] = [[15,30,50], [1,2,3,5]]
vec_parameters['GetTemplateFunctionFeature'] = [[2,3,5,10], [.5, 1, 2]]
vec_parameters['GetPersTropicalCoordinatesFeature'] = [[10,50,250]]
vec_parameters['GetAdaptativeSystemFeature'] = [['gmm'],
[3,4,5,10,15]]
onlyForest = [
{'base_estimator': ['RF'], 'n_estimators': [50,100]},
]
searchG = GridSearchCV(
main_classifier(), param_grid=onlyForest, cv=5,
return_train_score=True, scoring='accuracy'
)
_, y_train = mnist_reader.load_mnist('fashion_mnist/data/fashion', kind='train')
_, y_test = mnist_reader.load_mnist('fashion_mnist/data/fashion', kind='t10k')
train_index = list(range(len(y_train)))
test_index = list(range(len(y_train), len(y_train)+len(y_test)))
index = train_index+test_index
pdiagrams = dict()
for i in index:
#pdiagrams["pdiag_taxi_l_"+str(i)]= safe_load(path_diag + "taxi_l_"+str(i))
pdiagrams["pdiag_taxi_u_"+str(i)]= safe_load(path_diag + "taxi_u_"+str(i))
func_list = [getattr(vect, keys) for keys in vec_parameters.keys()]
for func in func_list:
feature_dictionary = dict()
vec_methods = dict()
vec_methods[func.__name__] = vec_parameters[func.__name__]
feature_dictionary=feature_computation(vec_methods,pdiagrams,"pdiag_taxi_u_",
train_index, test_index)
with open(path_results+'FMNIST_feature_'+func.__name__+'.pkl', 'wb') as f:
pickle.dump(feature_dictionary, f)
best_scores=parameter_optimization(train_index, y_train, vec_methods,
feature_dictionary, searchG, normalization)
print("Parameter optimization:",best_scores)
with open(path_results+'FMNIST_best_scores_'+func.__name__+'.pkl', 'wb') as f:
pickle.dump(best_scores, f)
train_scores, test_scores = scores(train_index, y_train, test_index, y_test,
vec_methods, feature_dictionary, best_scores,
n_iters, normalization)
print("The train accuracy is", train_scores)
print("The test accuracy is", test_scores)
with open(path_results+'FMNIST_train_scores_'+func.__name__+'.pkl', 'wb') as f:
pickle.dump(train_scores, f)
with open(path_results+'FMNIST_test_scores_'+func.__name__+'.pkl', 'wb') as f:
pickle.dump(test_scores, f)