signaflo / java-timeseries

Time series analysis in Java
MIT License
195 stars 49 forks source link

Save trained model #22

Closed B87 closed 2 months ago

B87 commented 6 years ago

Hi, thanks for your implementation it has been very useful for me.

I just wanna know if there is a way to save an ARIMA model on disk for future predictions.

signaflo commented 6 years ago

@B87, there's nothing specific to this library to make that possible. I would personally use GSON because it's easy to use, especially when working with immutable objects as many of this library's objects are. I'll see if I can get a working example together.

Inqus commented 4 years ago

@B87, there's nothing specific to this library to make that possible. I would personally use GSON because it's easy to use, especially when working with immutable objects as many of this library's objects are. I'll see if I can get a working example together.

Since the Arima class is an interface, it is not possible to recreate the model object from the json string

 Arima model  = gson.fromJson(new FileReader("models/model_1.json"), Arima.class);

ArimaModel class is final...

Exception in thread "main" java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.github.signaflo.timeseries.model.arima.Arima. Registering an InstanceCreator with Gson for this type may fix this problem.
    at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:228)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212)
    at com.google.gson.Gson.fromJson(Gson.java:932)
    at com.google.gson.Gson.fromJson(Gson.java:870)
    at arima.Predict.predict(Predict.java:51)
    at arima.Predict.main(Predict.java:27)
Caused by: java.lang.UnsupportedOperationException: Interface can't be instantiated! Interface name: com.github.signaflo.timeseries.model.arima.Arima
    at com.google.gson.internal.UnsafeAllocator.assertInstantiable(UnsafeAllocator.java:117)
    at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:49)
    at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:225)
    ... 5 more

I also can’t save ArimaOrder, when I try to save I get an empty file without errors

 ArimaOrder modelOrder = ArimaOrder.order(0, 1, 1, 0, 1, 1); 
Gson gson = new Gson();
gson.toJson(modelOrder, new FileWriter("models/model_1.json"));