-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
280 lines (225 loc) · 9.42 KB
/
Copy pathMain.py
File metadata and controls
280 lines (225 loc) · 9.42 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
import org.apache.spark.sql.SparkSession
import spark.implicits._
import org.apache.spark.sql.types._
import org.apache.spark.sql.functions._
import org.apache.spark.sql.Row
import org.apache.spark.sql.functions.input_file_name
import org.apache.spark.sql.functions.udf
import org.apache.spark.mllib.util.MLUtils
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.evaluation.MulticlassMetrics
import org.apache.spark.ml.classification.MultilayerPerceptronClassifier
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
import org.apache.spark.ml.feature.VectorAssembler
import org.apache.spark.ml.feature.StringIndexer
import org.apache.spark.ml.feature.IndexToString
import org.apache.spark.ml.feature.VectorIndexer
import org.apache.spark.ml.feature.StandardScaler
import org.apache.spark.ml.Pipeline
/* Path of data files */
val path_Dataset = "../HAPT Data Set/RawData"
/*
* Setting Neural Network
*/
val numFeatures = 6
val numClasses = 12
val trainSet = 0.7
val testSet = 0.3
/* Create a SparkSession */
val spark = SparkSession.
builder().
appName("Project BDP").
getOrCreate()
/*
* Function for cleaning the name of files
*/
def cleanNameFile(x: String): String = {
try{
val str = x.split("/")(9)
return str.split("_")(1).
concat("_").
concat(str.split("_")(2)).
dropRight(4).
toString
} catch {
case e: Exception => ""
}
}
val getNameClean = udf(cleanNameFile _)
val schemaAcceleration = StructType(Array(
StructField("acceleration_x", DoubleType, nullable = true),
StructField("acceleration_y", DoubleType, nullable = true),
StructField("acceleration_z", DoubleType, nullable = true),
StructField("file_name_A", StringType, nullable = true)
))
val schemaGyroscope = StructType(Array(
StructField("gyroscope_x", DoubleType, nullable = true),
StructField("gyroscope_y", DoubleType, nullable = true),
StructField("gyroscope_z", DoubleType, nullable = true),
StructField("file_name_B", StringType, nullable = true)
))
val schemaLabelsReady = StructType(Array(
StructField("Id_Experiment", IntegerType, nullable = true),
StructField("Index", IntegerType, nullable = true),
StructField("label", DoubleType, nullable = true)
))
println("-------------------------------------------------------")
println("----------------- READING DATA FILES ------------------")
println("-------------------------------------------------------")
val dfAcceleration_tmp = spark.read.
option("header", false).
option("inferSchema", false).
option("delimiter", " ").
schema(schemaAcceleration).
csv(path_Dataset + "/acc*").
select("acceleration_x","acceleration_y","acceleration_z").
withColumn("file_name_A", getNameClean(input_file_name()))
val dfAcceleration = dfAcceleration_tmp.
orderBy($"file_name_A").
withColumn("indexA",monotonically_increasing_id+1)
val dfGyroscope_tmp = spark.read.
option("header", false).
option("inferSchema", false).
option("delimiter", " ").
schema(schemaGyroscope).
csv(path_Dataset + "/gyro*").
select("gyroscope_x","gyroscope_y","gyroscope_z").
withColumn("file_name_B", getNameClean(input_file_name()))
val dfGyroscope = dfGyroscope_tmp.
orderBy($"file_name_B").
withColumn("indexB", monotonically_increasing_id+1)
val dfLabelsReady = spark.read.
option("header", false).
option("inferSchema", false).
option("delimiter", " ").
schema(schemaLabelsReady).
csv("../tmp/labels_ready.txt").
select("Id_Experiment", "Index","label")
/*
* Join dfLabelsReady with dfGyroscope and dfAcceleration
*/
val dfNuevo = dfLabelsReady.
join(dfAcceleration,
dfAcceleration("indexA") === dfLabelsReady("Index"),
"inner")
val dfReady_tmp = dfNuevo.join(dfGyroscope,
dfNuevo("indexA") === dfGyroscope("indexB"), "inner").
drop(dfNuevo("file_name_A")).
drop(dfGyroscope("file_name_B")).
drop(dfNuevo("indexA")).
drop(dfNuevo("Id_Experiment")).
drop(dfGyroscope("indexB"))
/*
* Final dataset with the features and target value. I filtered label > 0
*/
val dfReady = dfReady_tmp.
filter($"label" > 0 ).
orderBy($"indexA").
drop($"Index")
println("-------------------------------------------------------")
println("-------------- STARTING MACHINE LEARNING --------------")
println("-------------------------------------------------------")
/*
* Using VectorAssembler(), To create a vector with all features.
*/
val assembler = new VectorAssembler().
setInputCols(Array("acceleration_x", "acceleration_y",
"acceleration_z", "gyroscope_x", "gyroscope_y", "gyroscope_z")).
setOutputCol("features")
val features = assembler.transform(dfReady)
/*
* Using StandardScaler(), To Standard all the features using
* Standard Deviation.
*/
val scaler = new StandardScaler().
setInputCol("features").
setOutputCol("scaledFeatures").
setWithStd(true).
setWithMean(false)
val scalerModel = scaler.fit(features)
val scaledData = scalerModel.transform(features)
/*
* I created a Index to label and features columns using
* StringIndexer() and VectorIndexer().
*/
val labelIndexer = new StringIndexer().
setInputCol("label").
setOutputCol("indexedLabel").
fit(scaledData)
val featureIndexer = new VectorIndexer().
setInputCol("scaledFeatures").
setOutputCol("indexedFeatures").
setMaxCategories(numFeatures).
fit(scaledData)
/*
* To split the data between train and test. For that I used
* two global variables.
*/
val splits = scaledData.
randomSplit(Array(trainSet, testSet))
val trainingData = splits(0)
val testData = splits(1)
/*
* This function is to create a model given an architecture of neural
* network. Return the predictions to testData.
*/
def CreateModel(layers: Array[Int]): org.apache.spark.sql.Dataset[_] = {
val trainer = new MultilayerPerceptronClassifier().
setLayers(layers).
setLabelCol("indexedLabel").
setFeaturesCol("indexedFeatures").
setBlockSize(128).
setSeed(System.currentTimeMillis).
setMaxIter(200)
val labelConverter = new IndexToString().
setInputCol("prediction").
setOutputCol("predictedLabel").
setLabels(labelIndexer.labels)
val pipeline = new Pipeline().
setStages(Array(labelIndexer, featureIndexer, trainer, labelConverter))
val model = pipeline.fit(trainingData)
return model.transform(testData)
}
println("-------------------------------------------------------")
println("--------------- CREATING NEURAL NETWORK ---------------")
println("-------------------------------------------------------")
/* Neural network configuration */
val layers = Array[Int](numFeatures, 15, 20, 15, numClasses)
/* Called a function CreateModel and return test prediction */
val predictions = CreateModel(layers)
println("-------------------------------------------------------")
println("----------------------- RESULTS -----------------------")
println("-------------------------------------------------------")
/* Multilayer Perceptron Classifier Evaluation */
val evaluator = new MulticlassClassificationEvaluator().
setLabelCol("indexedLabel").
setPredictionCol("prediction").
setMetricName("accuracy")
val accuracy = evaluator.evaluate(predictions)
println("Test Error = " + (1.0 - accuracy))
/*
* --------------------------------------------
* SOME RESULTS
* --------------------------------------------
val layers = Array[Int](6, 20, 20, 20, 12)
accuracy: Double = 0.6840659340659341
val layers = Array[Int](6, 18, 20, 18, 12)
accuracy: Double = 0.6911586596689544
val layers = Array[Int](6, 17, 20, 17, 12) ***** BEST ******
accuracy: Double = 0.703482382031733
with noise = accuracy: Double = 0.5847007722007722
val layers = Array[Int](6, 16, 20, 16, 12)
accuracy: Double = 0.7063686466625843
val layers = Array[Int](6, 30, 40, 30, 12)
accuracy: Double = 0.6524953789279113
val layers = Array[Int](6, 30, 40, 40, 30, 12)
accuracy: Double = 0.6638655462184874
val layers = Array[Int](6, 15, 20, 15, 12)
accuracy: Double = 0.689877300613497
val layers = Array[Int](6, 8, 8, 12)
accuracy: Double = 0.6477379095163807
val layers = Array[Int](6, 7, 12)
accuracy: Double = 0.62701062215478
val layers = Array[Int](6, 8, 12)
accuracy: Double = 0.6122257053291537
*/