-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_fragment_1-at-site-1.sql
More file actions
319 lines (221 loc) · 6.57 KB
/
process_fragment_1-at-site-1.sql
File metadata and controls
319 lines (221 loc) · 6.57 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
SET SERVEROUTPUT ON;
SET VERIFY OFF;
-- FILLING NULL VALUES WITH MEAN
CREATE OR REPLACE FUNCTION get_size_of_data
RETURN NUMBER
IS
C number;
BEGIN
SELECT COUNT(*) INTO c FROM fragment_1;
return C;
END get_size_of_data;
/
CREATE OR REPLACE FUNCTION get_min_val
RETURN NUMBER
IS
C number;
BEGIN
select MIN(numerical_col_1) into c from fragment_1;
return C;
END get_min_val;
/
CREATE OR REPLACE FUNCTION get_max_val
RETURN NUMBER
IS
C number;
BEGIN
select MAX(numerical_col_1) into c from fragment_1;
return C;
END get_max_val;
/
CREATE OR REPLACE FUNCTION get_min_val2
RETURN NUMBER
IS
C number;
BEGIN
SELECT MIN(numerical_col_2) INTO c FROM fragment_1;
return C;
END get_min_val2;
/
CREATE OR REPLACE FUNCTION get_max_val2
RETURN NUMBER
IS
C number;
BEGIN
SELECT MAX(numerical_col_2) INTO c FROM fragment_1;
return C;
END get_max_val2;
/
CREATE OR REPLACE PROCEDURE mean_calculate(A in OUT float, B in out float)
IS
BEGIN
A := A/B;
END mean_calculate;
/
create or replace PACKAGE body fill_missing_values as
PROCEDURE fill_col
IS
mean_val float := 0;
mean_counter INT := 0;
BEGIN
DBMS_OUTPUT.PUT_LINE('Finding Missing Values for Numerical Feature');
FOR r IN (SELECT numerical_col_1 FROM fragment_1)
LOOP
IF r.numerical_col_1 IS NOT NULL then
mean_val := mean_val + r.numerical_col_1;
mean_counter := mean_counter + 1;
END IF;
END LOOP;
mean_calculate(mean_val,mean_counter);
DBMS_OUTPUT.PUT_LINE('Mean Value to fill the missing values : ' || mean_val);
FOR r IN (SELECT id,numerical_col_1 FROM fragment_1)
LOOP
IF r.numerical_col_1 IS NULL then
DBMS_OUTPUT.PUT_LINE('Missing Numerical Value with id : ' || r.id);
UPDATE fragment_1 SET numerical_col_1 = mean_val WHERE id = r.id;
END IF;
END LOOP;
END fill_col;
end fill_missing_values;
/
select * from fragment_1;
DECLARE
BEGIN
fill_missing_values.fill_col;
END;
/
commit;
SELECT id, numerical_col_1 FROM fragment_1;
/* OUTLIER DETECTION */
SELECT id, numerical_col_1 FROM fragment_1;
DECLARE
lower_quartile_index INT := 0;
upper_quartile_index INT := 0;
lower_quartile_value FLOAT := 0;
upper_quartile_value FLOAT := 0;
size_of_data INT := 0;
quartile_counter INT := 0;
upper_outlier_boundary FLOAT := 0;
lower_outlier_boundary FLOAT := 0;
interquartile_range FLOAT := 0;
BEGIN
DBMS_OUTPUT.PUT_LINE('Finding Outliers');
size_of_data := get_size_of_data;
lower_quartile_index := (size_of_data) * (1/4);
upper_quartile_index := (size_of_data) * (3/4);
FOR r IN (SELECT id,numerical_col_1 FROM Outlier_Detector)
LOOP
quartile_counter := quartile_counter + 1;
IF quartile_counter = lower_quartile_index THEN
lower_quartile_value := r.numerical_col_1;
ELSIF quartile_counter = upper_quartile_index THEN
upper_quartile_value := r.numerical_col_1;
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Lower Quartile index : ' || lower_quartile_index || ' Upper Quartile Index : ' || upper_quartile_index);
DBMS_OUTPUT.PUT_LINE('Lower Quartile Value : ' || lower_quartile_value || ' Upper Quartile Value : ' || upper_quartile_value);
interquartile_range := upper_quartile_value - lower_quartile_value ;
DBMS_OUTPUT.PUT_LINE('Interquartile Range : ' || interquartile_range);
lower_outlier_boundary := lower_quartile_value - interquartile_range * 1.5;
DBMS_OUTPUT.PUT_LINE('Lower Outlier Boundary : ' || lower_outlier_boundary);
upper_outlier_boundary := upper_quartile_value + interquartile_range * 1.5;
DBMS_OUTPUT.PUT_LINE('Upper Outlier Boundary : ' || upper_outlier_boundary);
FOR r IN (SELECT id,numerical_col_1 FROM fragment_1)
LOOP
IF r.numerical_col_1 > upper_outlier_boundary or r.numerical_col_1 < lower_outlier_boundary then
DBMS_OUTPUT.PUT_LINE('Detected Outlier : ' || r.numerical_col_1);
END IF;
END LOOP;
END;
/
/*
----- REMOVING OUTLIERS, ADD ABOVE IF REQUIRED
FOR r IN (SELECT id,numerical_col_1 FROM RAW_DATA)
LOOP
IF r.numerical_col_1 > upper_outlier_boundary or r.numerical_col_1 < lower_outlier_boundary then
DBMS_OUTPUT.PUT_LINE('Detected Outlier : ' || r.numerical_col_1);
DELETE FROM raw_data WHERE id = r.id;
END IF;
END LOOP;
*/
commit;
DECLARE
min_val INT := 0;
max_val INT := 0;
val float := 0;
BEGIN
DBMS_OUTPUT.PUT_LINE('Min Max Scaler / Normalization');
min_val := get_min_val;
max_val := get_max_val;
DBMS_OUTPUT.PUT_LINE( min_val || ' ' || max_val);
FOR r IN (SELECT id,numerical_col_1 FROM fragment_1)
LOOP
val := ((r.numerical_col_1 - min_val) / (max_val - min_val));
UPDATE fragment_1 SET numerical_col_1 = ROUND(val,5) WHERE id = r.id;
END LOOP;
END;
/
commit;
SELECT id, numerical_col_1 FROM fragment_1;
SELECT id, numerical_col_2 FROM fragment_1;
DECLARE
min_val INT;
max_val INT;
increment_val int;
no_of_divisions INT := 8;
temp int ;
l_str varchar2(100);
BEGIN
DBMS_OUTPUT.PUT_LINE('Binning');
min_val := get_min_val2;
max_val := get_max_val2;
DBMS_OUTPUT.PUT_LINE('Min and Max value for numerical_col_2');
DBMS_OUTPUT.PUT_LINE(min_val || ' ' || max_val);
increment_val := (max_val - min_val) / no_of_divisions;
DBMS_OUTPUT.PUT_LINE('Increment Value : ' || increment_val);
FOR r IN (SELECT id,numerical_col_2 FROM fragment_1)
LOOP
temp := FLOOR(r.numerical_col_2 / increment_val) + 1;
IF temp > no_of_divisions THEN
temp := no_of_divisions;
END IF;
UPDATE fragment_1 SET numerical_col_2 = temp WHERE id = r.id;
END LOOP;
END;
/
commit;
SELECT id, numerical_col_2 FROM fragment_1;
SELECT * FROM fragment_1;
DECLARE
mean_val float := 0;
diff float := 0;
sum_of_square_of_diff float := 0;
variancee float := 0;
mean_counter INT := 0;
standard_deviation_r float := 0;
standardized_val float := 0;
BEGIN
DBMS_OUTPUT.PUT_LINE('Standard Scaler / Standardizing Numerical Column 3');
FOR r IN (SELECT numerical_col_3 FROM fragment_1)
LOOP
mean_val := mean_val + r.numerical_col_3;
mean_counter := mean_counter + 1;
END LOOP;
mean_calculate(mean_val,mean_counter);
FOR r IN (SELECT numerical_col_3 FROM fragment_1)
LOOP
diff := (r.numerical_col_3 - mean_val);
diff := diff * diff;
sum_of_square_of_diff := sum_of_square_of_diff + diff;
END LOOP;
variancee := sum_of_square_of_diff / mean_counter;
standard_deviation_r := SQRT(variancee);
FOR r IN (SELECT id,numerical_col_3 FROM fragment_1)
LOOP
standardized_val := (r.numerical_col_3 - mean_val) / standard_deviation_r;
UPDATE fragment_1 SET numerical_col_3 = ROUND(standardized_val,5) WHERE id = r.id;
END LOOP;
END;
/
select id, numerical_col_3 from fragment_1;
commit;