-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetConcatenate.py
146 lines (113 loc) · 3.61 KB
/
getConcatenate.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
import concatenate_results as CR
import numpy as np
import scipy as sp
import math
import csv
import copy
import glob
import pickle
import os.path
from scipy import optimize
import statsmodels.api as sm
import pickle
import pandas as pd
folder = '/n/regal/TSC/Ensemble_method3/'
[F_ids, F_periods, Scores, X_out_I] = CR.concatenate_results(folder)
print 'F_ids', F_ids
print 'X_out_I',X_out_I
[eta, mean_train, std_train] = pickle.load(open('/n/regal/TSC/Ensemble_method3/eta_F1_newDelta.pk', 'rb'))
n_exp = 5
#calculate g for each point
X_comb_I = np.nan_to_num((X_out_I - mean_train)/ (std_train+0.0000000001))
ndotx = np.dot(X_comb_I,eta)
print "el primer minimo de ndotx es", np.min(ndotx)
count = 0
#for idx, row in enumerate(ndotx):
# if np.max(row) > 709.0:
# if np.max(row) - np.min(row) < 1418:
# delta = (np.max(row)+np.min(row))/2
# ndotx[idx, :] = row - delta
# print "I found a maximum"
# else:
# where = np.argmax(row)
# if count == 0:
# who = [idx,where]
# count = count + 1
# else:
# who = np.vstack((who,[idx,where]))
# print "NOOOOOO MAX", row
# if np.min(row)< -709.0:
# if np.max(row) - np.min(row) < 1418:
# delta = (np.max(row)+np.min(row))/2
# ndotx[idx, :] = row - delta
# print "I found a minimum"
# else:
# where = np.argmax(row)
# if count == 0:
# who = [idx,where]
# count = count + 1
# else:
# who = np.vstack((who,[idx,where]))
# print "NOOOOOO MIN", row
#ndotxmax = np.max(ndotx)
#ndotxmin = np.min(ndotx)
#R = np.exp(ndotx- ndotxmax)
R = np.exp(ndotx)
Q = np.sum(R, axis = 1)
Qh = np.tile(Q, (n_exp,1)).T
g = R/Qh
#print "gggg", g, "g_shape", g.shape
#print "WHO", who, "who_shape", len(who)
#if len(who) > 2:
# for a in who:
# g[a[0],:] = 0.0
# g[a[0],a[1]] =0.0
#else:
# g[who[0],:] = 0.0
# g[who[0],who[1]] = 0.0
print "el minimo de ndotx es", np.min(ndotx)
print "el minimo de Qh es", np.min(Qh)
print "el minimo de g es", np.min(g)
print "el minimo de R es", np.min(R)
if len(np.where(np.isnan(R[:,:])==True)[0])>0:
print "Hay NAN en R"
normalized = (g*Scores).sum(axis=1)
index = np.argsort(normalized);
index2 =index[::-1];
Outliers_ids = F_ids[index2];
Outliers_periods = F_periods[index2];
Outliers_features = X_out_I[index2,:];
Outliers_scores = normalized[index2];
g2 = g[index2,:]
#print 'diff', (ndotx- ndotxmax)[0:100,:]
#print 'ndotxmax', ndotxmax, 'max minus min', ndotxmax - ndotxmin
print 'R', R[:100,:]
print 'X_comb_I', X_comb_I[0:100,:]
print 'Scores', normalized[0:100]
print 'ndotx', ndotx[0:100,:]
print 'g', g2[0:100,:]
Outliers_ids = Outliers_ids[0:50000];
Outliers_periods = Outliers_periods[0:50000];
Outliers_features = Outliers_features[0:50000,:];
Outliers_scores = Outliers_scores[0:50000];
pickle.dump(Outliers_ids, open('IDS_1.pk', 'wb'))
pickle.dump(Scores[index2,:], open('Scores_1.pk', 'wb'))
feats = pd.DataFrame(Outliers_features)
feats.to_csv("outliersF_features_F1.csv")
resultf = open("outliersF1.csv",'wb')
wr = csv.writer(resultf, dialect='excel')
for item in Outliers_ids:
wr.writerow([item,])
resultf = open("outliersF_periods_F1.csv",'wb')
wr = csv.writer(resultf, dialect='excel')
for item in Outliers_periods:
wr.writerow([item,])
#resultf = open("outliersF_features_F1.csv",'wb')
#wr = csv.writer(resultf, dialect='excel')
#for item in Outliers_features:
# wr.writerow([item,])
resultf = open("outliersF_scores_F1.csv",'wb')
wr = csv.writer(resultf, dialect='excel')
for item in Outliers_scores:
wr.writerow([item,])