spark-ts-examples icon indicating copy to clipboard operation
spark-ts-examples copied to clipboard

Added Java Implementation of ARIMA

Open vharissh14 opened this issue 8 years ago • 2 comments

#7 Closed

vharissh14 avatar Dec 19 '16 10:12 vharissh14

Hi this java code to implement ARIMA Model works for me if useful to anyone

public static void main(String args[]) { System.setProperty("hadoop.home.dir", "C:/winutils");

	 SparkSession spark = SparkSession
		      .builder().master("local")
		      .appName("Spark-TS Example")
		      .config("spark.sql.warehouse.dir", "file:///C:/Users/devanshi/Downloads/Spark/sparkdemo/spark-warehouse/")
		      .getOrCreate();
	 
        Dataset<Double> doubleDataset = lines.map(line->Double.parseDouble(line.toString()),
			Encoders.DOUBLE());
	
	List<Double> doubleList = doubleDataset.collectAsList();
			
	Double[] doubleArray = new Double[doubleList.size()];
	doubleArray = doubleList.toArray(doubleArray);
	
	double[] values = new double[doubleArray.length];
	for(int i = 0; i< doubleArray.length; i++)
	{ 
		values[i] = doubleArray[i];
	}
	
	Vector tsvector = Vectors.dense(values);
	
	System.out.println("Ts vector:" + tsvector.toString());
	
	//ARIMAModel arimamodel = ARIMA.fitModel(1, 0, 1, tsvector, true, "css-bobyqa", null);
	ARIMAModel arimamodel = ARIMA.autoFit(tsvector, 1, 1, 1);
	
	Vector forcst = arimamodel.forecast(tsvector, 10);
	
	System.out.println("forecast of next 10 observations: " + forcst);


	

devanshi7 avatar Dec 20 '16 06:12 devanshi7

For those who're having trouble with spark warehouse directory, please give it the path of your input text/csv file. Let's say my file is located in "D://data/test/tesfile.csv", spark warehouse path should be "file:///D://data/test/"

hancelpv avatar Jan 15 '19 07:01 hancelpv