Using Encog for time series

We have to download the time series data from https://solarscience.msfc.nasa.gov/greenwch/spot_num.txt and save the file in the data folder. In the .java file, we will specify the file path, and then we will indicate the format of the file using the following code block:

File filename = new File("data/spot_num.txt");
CSVFormat format = new CSVFormat('.', ' ');
VersatileDataSource source = new CSVDataSource(filename, true, format);
VersatileMLDataSet data = new VersatileMLDataSet(source);
data.getNormHelper().setFormat(format);
ColumnDefinition columnSSN = data.defineSourceColumn("SSN", ColumnType.continuous);
ColumnDefinition columnDEV = data.defineSourceColumn("DEV", ColumnType.continuous);
data.analyze();
data.defineInput(columnSSN);
data.defineInput(columnDEV);
data.defineOutput(columnSSN);

Now, we will create the feedforward network with the window size 1. When processing a time series, you should keep in mind that it should never be shuffled. We will hold some data back for validation. We will use the following lines of code to do so:

EncogModel model = new EncogModel(data);
model.selectMethod(data, MLMethodFactory.TYPE_FEEDFORWARD);

model.setReport(new ConsoleStatusReportable());
data.normalize();

// Set time series.
data.setLeadWindowSize(1);
data.setLagWindowSize(WINDOW_SIZE);
model.holdBackValidation(0.3, false, 1001);
model.selectTrainingType(data);

The next step is to run the training with five-fold cross-validation using the following line:

MLRegression bestMethod = (MLRegression) model.crossvalidate(5, false);

Now, it's time to display the error and the final model. We will do that by using the following lines of code:

System.out.println("Training error: " + model.calculateError(bestMethod, model.getTrainingDataset()));
System.out.println("Validation error: " + model.calculateError(bestMethod, model.getValidationDataset()));

NormalizationHelper helper = data.getNormHelper();
System.out.println(helper.toString());

// Display the final model.
System.out.println("Final model: " + bestMethod);

The output will be similar to the following screenshot:

Now, we will test the model using the following code block:

while (csv.next() && stopAfter > 0) {
StringBuilder result = new StringBuilder();

line[0] = csv.get(2);// ssn
line[1] = csv.get(3);// dev
helper.normalizeInputVector(line, slice, false);

if (window.isReady()) {
window.copyWindow(input.getData(), 0);
String correct = csv.get(2); // trying to predict SSN.
MLData output = bestMethod.compute(input);
String predicted = helper
.denormalizeOutputVectorToString(output)[0];

result.append(Arrays.toString(line));
result.append(" -> predicted: ");
result.append(predicted);
result.append("(correct: ");
result.append(correct);
result.append(")");

System.out.println(result.toString());
}

window.add(slice);

stopAfter--;
}

The output will be similar to the following screenshot: 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.124