-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomplete.py
More file actions
150 lines (111 loc) · 4.29 KB
/
Copy pathcomplete.py
File metadata and controls
150 lines (111 loc) · 4.29 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
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
144
145
146
147
148
149
150
import math
import numpy as np
import pandas as pd
import tensorflow as tf
from utils import utils
from scipy import stats
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LayerNormalization, Dropout
tf.compat.v1.disable_eager_execution()
NUM_EPOCHS = 4000
TITILE = 'ad7'
COMPLETE_USING_MEAN = False
COMPLETE_USING_MODE = False
COMPLETE_USING_REGRESSION = True
def checkIfThereIsNan(array):
tmp = False
for i in range(array.shape[1]):
if (pd.isna(array[:,i]).any(axis=0)):
print('\nn/a data in column', i)
tmp = True
return tmp
data = pd.read_excel('data_files/' + TITILE + '.xlsx', header=0)
data = np.array(data)
#data = np.shuffle(data)
x = data[:,0:13]
y = data[:,13:17]
#x = x[:,4:21] # removed (index, date, location, type of coarse aggregate)
#x = np.delete(x, 1, 1) #removed type of fine aggregate
#x = np.delete(x, 9, 1) #removed additive name
#x = np.delete(x, 9, 1) #removed additive group letter
print('\nX[0]:', x[0,:], '\nY[0]:', y[0,:])
if(checkIfThereIsNan(x)):
print('\nsome input data is missing')
else:
print('\nall input data is good')
if(checkIfThereIsNan(y)):
print('\nsome output data is missing')
else:
print('\nall output data is good')
print('\n')
print('final data shape:', 'input:', x.shape, 'output:', y.shape)
if COMPLETE_USING_MEAN:
print('\nCompleting data using the average')
x_2 = np.zeros(x.shape)
for i in range(x.shape[1]):
tmp = x[:,i]
mean = np.mean(tmp[~pd.isna(tmp)])
correct = np.where(pd.isna(tmp), mean, tmp)
x_2[:,i] = correct
df = pd.DataFrame(x_2)
filepath = 'missing_data/completed_' + TITILE + '_mean.xlsx'
df.to_excel(filepath, index=False)
print('saved completed data to excel file (using mean method)')
if COMPLETE_USING_MODE:
print('\nCompleting data using the most appearing value')
x_2 = np.zeros(x.shape)
for i in range(x.shape[1]):
tmp = x[:,i]
mode = stats.mode(tmp[~pd.isna(tmp)])[0]
correct = np.where(pd.isna(tmp), mode, tmp)
x_2[:,i] = correct
df = pd.DataFrame(x_2)
filepath = 'missing_data/completed_' + TITILE + '_mode.xlsx'
df.to_excel(filepath, index=False)
print('saved completed data to excel file (using the most appearing value method)')
if COMPLETE_USING_REGRESSION:
print('\nCompleting data using linear regression model')
print('befor: x:', x.shape)
tmp = pd.DataFrame(x).dropna(subset=[0,1,4,5,6,7,8,9,10,11,12])
x_2 = np.array(tmp)
x_2_full = np.empty((0,x_2.shape[1]))
x_2_missing = np.empty((0,x_2.shape[1]))
for i in range(x_2.shape[0]):
if(pd.isna(x_2[i,:]).any()):
x_2_missing = np.append(x_2_missing, [x_2[i,:]], axis=0)
else:
x_2_full = np.append(x_2_full, [x_2[i,:]], axis=0)
print('after: x_2:', x_2.shape, '\nx_2_full', x_2_full.shape, '\nx_2_missing', x_2_missing.shape)
x_3 = np.copy(x_2_full)
x_3 = np.delete(x_3, [2,3], 1)
y_3 = np.copy(x_2_full[:,2:4])
x_4 = np.copy(x_2_missing)
x_4 = np.delete(x_4, [2,3], 1)
y_4 = np.copy(x_2_missing[:,2:4])
model = utils.newSeqentialModel(11,2)
print('\n\nstarting training ...')
model.compile(loss='mean_squared_error', optimizer='adam', metrics=[tf.keras.metrics.MeanSquaredError()])
model.fit(x_3, y_3, epochs=NUM_EPOCHS, batch_size=32, verbose=1)
print('trainning finished')
y_5 = np.zeros(y_4.shape)
predictions = np.around(model.predict(x_4),1)
for i in range(x_4.shape[0]):
if(pd.isna(y_4[i,0])):
if(predictions[i,0] >= 30):
y_5[i,0] = 40
else:
y_5[i,0] = 20
else:
y_5[i,0] = y_4[i,0]
if(pd.isna(y_4[i,1])):
y_5[i,1] = predictions[i,1]
else:
y_5[i,1] = y_4[i,1]
complete = np.concatenate([x_3[:,0:2], y_3, x_3[:,2:17]], axis=1)
missing = np.concatenate([x_4[:,0:2], y_5, x_4[:,2:17]], axis=1)
full_data = np.concatenate([complete, missing], axis= 0)
final_data = np.concatenate([full_data, y], axis= 1)
df = pd.DataFrame(final_data)
filepath = 'missing_data/completed_' + TITILE + '_regression.xlsx'
df.to_excel(filepath, index=False)
print('saved completed data to excel file (using linear regression method)')