-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSet.java
More file actions
149 lines (95 loc) · 2.6 KB
/
DataSet.java
File metadata and controls
149 lines (95 loc) · 2.6 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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class DataSet {
protected List<Data> dataValues = new ArrayList<>();
public DataSet()
{}
public DataSet(List<Data> dataValues)
{
this.dataValues = dataValues;
}
public static DataSet parseTrainData(String filePath) throws IOException
{
DataSet trainData = new DataSet();
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line;
int i=0;
while ((line = br.readLine()) != null)
{
if(nullCheck(line)) {
Data data=Data.parseTrainData(line);
// Data.setType(DataSet.DataType.intToType(i));
trainData.dataValues.add(data);
}
}
return trainData;
}
public static List parseTestData(int j) throws IOException
{
List a3=Data.a2;
if(j==13)
{
Double min=(Double) a3.get(0);
Double max=(Double) a3.get(0);
for(int i=1;i<a3.size();i++) {
if((Double)a3.get(i)>max)
max=(Double)a3.get(i);
else if((Double)a3.get(i)<min){
min=(Double)a3.get(i);
}
}
Double avg=(max+min)/2;
for(int i=0;i<a3.size();i++) {
if((Double)a3.get(i)>avg)
Data.a2.set(i, 1.0);
else if((Double)a3.get(i)<avg)
Data.a2.set(i, 0.0);
}
}
return a3;
}
public static boolean nullCheck(String line) {
String s2[]= {"!","@","]","#","$","%","^","&","*","?","["};
String[] fields=line.split(",");
for(int i=0;i<fields.length;i++) {
for(String sd:s2) {
if(fields[i].equals(sd))
return false;
}
if(fields[i].equals(" "))
return false;
}
return true;
}
public void normalize(int i)
{
List<Double> max = new ArrayList<>(dataValues.get(0).getAttributes());
Double maxAttribute=max.get(i);
List<Double> min = new ArrayList<>(dataValues.get(0).getAttributes());
Double minAttribute=min.get(i);
for (Data currentExample : dataValues)
{
List<Double> currentAttributes = currentExample.getAttributes();
Double currentAttribute = currentAttributes.get(i);
// Update max:
if (currentAttribute > maxAttribute)
maxAttribute= currentAttribute;
// Update min:
if (currentAttribute < minAttribute)
minAttribute= currentAttribute;
}
// Normalize:
for (Data currentExample : dataValues)
{
List<Double> currentAttributes = currentExample.getAttributes();
Double currentAttribute = currentAttributes.get(i);
Double newValue = (currentAttribute - minAttribute) / (maxAttribute - minAttribute);
currentAttributes.set(i, newValue);
}
}
}