Now let's load the dataset. I am assuming that you copied your dataset to the UCI_HAR_Dataset/ directory. Then, also place the other data files as described previously:
val datasetPath = "UCI_HAR_Dataset/" val trainDataPath = s"$datasetPath/train/Inertial Signals" val trainLabelPath = s"$datasetPath/train/y_train.txt"
val testDataPath = s"$datasetPath/test/Inertial Signals" val testLabelPath = s"$datasetPath/test/y_test.txt"
Now it's time to load the training and test set separately. To do this I wrote two methods called loadData() and loadLabels() that are in the Utils.scala file. These two methods and their signatures will be provided soon:
val trainData = Utils.loadData(trainDataPath, "train") val trainLabels = Utils.loadLabels(trainLabelPath) val testData = Utils.loadData(testDataPath, "test") val testLabels = Utils.loadLabels(testLabelPath)
The loadData() method loads and maps the data from each .txt file based on the input signal type defined by the INPUT_SIGNAL_TYPES array in the Array[Array[Array[Float]]] format:
def loadData(dataPath: String, name: String): Array[Array[Array[Float]]] = { val dataSignalsPaths = INPUT_SIGNAL_TYPES.map( signal => s"$dataPath/${signal}${name}.txt" ) val signals = dataSignalsPaths.map { path => Source.fromFile(path).mkString.split("n").map { line => line.replaceAll(" ", " ").trim().split(" ").map(_.toFloat) } } val inputDim = signals.length val numSamples = signals(0).length val timeStep = signals(0)(0).length (0 until numSamples).map { n => (0 until timeStep).map { t => (0 until inputDim).map( i => signals(i)(n)(t) ).toArray }
.toArray }
.toArray }
As stated earlier, the INPUT_SIGNAL_TYPES contains some useful constants: those are separate, normalized input features for the neural network:
private val INPUT_SIGNAL_TYPES = Array( "body_acc_x_", "body_acc_y_", "body_acc_z_", "body_gyro_x_", "body_gyro_y_", "body_gyro_z_", "total_acc_x_", "total_acc_y_", "total_acc_z_")
On the other hand, loadLabels() is also a user-defined method that is used to load only the labels in the training as well as the test set:
def loadLabels(labelPath: String): Array[Float] = {
Source.fromFile(labelPath).mkString.split("n").map(_.toFloat - 1)
}
The labels are defined in another array as shown in the following code:
// Output classes: used to learn how to classify private val LABELS = Array( "WALKING", "WALKING_UPSTAIRS", "WALKING_DOWNSTAIRS", "SITTING", "STANDING", "LAYING")