-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.js
126 lines (114 loc) · 2.15 KB
/
data.js
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
export function dynamicLabels (dataset, labels) {
let labelsList = {};
labels.map((label) => {
labelsList[label] = {};
let datas = dataset.map((curr) => curr[label]);
const uniqDatas = [ ...new Set(datas) ];
labelsList[label].selfMapping = true;
labelsList[label].datas = uniqDatas.reduce((acc, curr) => {
acc[curr] = Object.keys(acc).length;
return acc;
}, {});
})
console.log(labelsList)
return labelsList;
}
function getAltitude(data)
{
let value;
if(data < 1 )
{
value = 0;
}else if(data < 400)
{
value = 1;
}else if(data < 900)
{
value = 2;
}else if(data < 1300)
{
value = 3;
}else if(data < 1700)
{
value = 4;
}else
{
value = 5;
}
return value;
}
function getConstructionYear(data)
{
let value;
if(data < 1 )
{
value = 0;
}else if(data < 1980)
{
value = 1;
}else if(data < 1990)
{
value = 2;
}else if(data < 2000)
{
value = 3;
}else if(data < 2010)
{
value = 4;
}else
{
value = 5;
}
return value;
}
function getPopulation(data)
{
let value;
if(data < 1 )
{
value = 0;
}else if(data < 2)
{
value = 1;
}else if(data < 101)
{
value = 2;
}else if(data < 201)
{
value = 3;
}else if(data < 301)
{
value = 4;
}else if(data < 501)
{
value = 5;
}
else
{
value = 6;
}
return value;
}
export function preprocess(dataset, labels) {
const X = [];
const y = [];
for (let i = 0; i < dataset.length; i++) {
const row = dataset[i];
// Handling the target data; nothing to preprocess, it should be either 0 or 1
y.push(labels["status_group"].datas[row.status_group]);
// Handle the training data
const newRow = [];
Object.keys(labels)
.filter((key) => key !== 'status_group')
.map((key) => {
if(labels[key].selfMapping) {
newRow.push(labels[key].datas[row[key]])
}
})
newRow.push(getAltitude(row['gps_height']));
newRow.push(getConstructionYear(row['construction_year']));
newRow.push(getPopulation(row['population']));
X.push(newRow);
}
return { X, y };
}