import au.com.bytecode.opencsv.CSVParser
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.regression.LinearRegressionModel
import org.apache.spark.mllib.regression.LinearRegressionWithSGD



object Regression
{
      def main (args:Array[String]){

	val File= "/home/osboxes/Documents/scala/KBS-rawm2.csv"
	val conf = new SparkConf().setMaster("local[*]").setAppName("Regression")
	val sc = new SparkContext(conf)
	val Data = sc.textFile(File).cache()

 	val parsedData = Data.map(line => {
        val parser = new CSVParser(',')
        parser.parseLine(line).mkString(",")
	val parts = line.split(',')
  	LabeledPoint(parts(0).toDouble, Vectors.dense(parts(2).split(' ').map(_.toDouble)))
       }).cache
	val numIterations = 100
	val stepSize = 0.00000001
	val model = LinearRegressionWithSGD.train(parsedData, numIterations, stepSize)
	// Evaluate model on training examples and compute training error
	val valuesAndPreds = parsedData.map { point =>
	  val prediction = model.predict(point.features)
	  (point.label, prediction)
	}
	val MSE = valuesAndPreds.map{ case(v, p) => math.pow((v - p), 2) }.mean()
	println("training Mean Squared Error = " + MSE)
	// Save and load model
	model.save(sc, "/home/osboxes/Documents/scala/scalaLinearRegressionWithSGDModel")
	
}
}



