Scala Machine Learning Projects
上QQ阅读APP看书,第一时间看更新

Running the Scala Play web app

To run the application, just follow these steps:

  1. Download the historical Bitcoin data from https://www.kaggle.com/mczielinski/bitcoin-historical-data. Then unzip and extract the .csv file.
  2. Open your preferred IDE (for example, Eclipse/IntelliJ) and create the Maven or SBT project.
  1. Run the Preprocess.scala script to convert the historical data into a time series. This script should generate two .csv files (that is, scala_test_x.csv and scala_test_y.csv).
  2. Then train the GradientBoostedTree model (use TrainGBT.scala script) using the previously generated files.
  3. Save the best (i.e. cross-validated) Pipeline model containing all the pipelines' steps.
  4. Then download the Scala Play app and all the files (that is, Bitcoin_price_prediction) from the Packt repository or GitHub (see in the book).
  5. Then copy the trained model to Bitcoin_price_prediction/models/.
  6. Then:  $ cd Bitcoin_price_prediction/bitcoin_ml/conf/ and update the parameter values in the application.conf as shown earlier.
  7. Finally, run the project using the $ sudo sbt run command.

After launching with $ sudo sbt run, the application will read all models from the models folder, the etalon model being specified by ml.model_version. Every 30 seconds (specified in constants.frequency = 30 in application.conf), the latest price data is retrieved from the Cryptocompare API. A prediction using the etalon model is made and the results are shown to the user in the form of a log message in the console, with the possibility to trigger an HTTP request to the specified endpoint.

After that, all models from the models folder are used to make a prediction on the previous 22-minute data and use the latest price data for a current minute as a way to check the quality of predictions. All predictions made by each model are stored in a database file. When a user visits http://localhost:9000, a table with a summary of predictions is shown to the user:

  • Model name
  • TPR, (not rate actually, in this case, just raw count) - how many times model predicted that price would increase and how many times that was true
  • FPR,  how many times model has predicted price increase, but price dropped or stayed the same
  • TNR, how many times model predicted non-increase of price and was correct
  • FNR, how many times model predicted non-increase of price and was wrong
  • Total count of predictions made by the model

Alright, here we go, after launching the app using $ sudo sbt run (on a terminal):

Figure 9: Sample signals generated by the model based on historical prices and live data

The preceding figure shows some sample signals generated by our model based on historical prices and live data. Additionally, we can see the raw prediction by the model. When you try to access the app from your browser at http://localhost:9000, you should see this (the count will increase with time, though):

Figure 10: Model performance using the Scala Play2 framework

In the preceding figure, the performance is not satisfactory, but I would suggest that you train the model with the most suitable hyperparameters and for more iterations, for example, 10,000 times. Additionally, in the next section, I tried to provide some more insights and improvement guidelines.

Finally, if you plan to deploy this application after making some extension (if any), then I would suggest to take a quick look at the last section in Chapter 7, Options Trading Using Q-Learning and Scala Play Framework, where you will find deployment guideline on server to be exposed as web app.