Rana M Zubair
Rana M Zubair

Reputation: 37

Why the code is not producing the output as it is supposed to be in the logic implemented?

Following is My DataFrame column(predcition) in the value Prediction

val PredictionModel = model.transform(testDF)
PredictionModel.select("features","label","prediction")

I have created a list as follows

val listOfPrediction = PredictionModel.select("prediction").collect.toList

The ouput of the list when I do listOfPrediction.foreach(println) is:

[1.0] [1.0] [0.0] [0.0] [2.0] so on

The logic I am trying to implement is

 val header: List[String] =  List("ID", "predict_0", "predict_1", "predict_2")

  var Id : List[String]= Idcol
  var Predict0  = List[String]()
  var Predict1  = List[String]()
  var Predict2  = List[String]()


 for(x<-listOfPrediction) 
 {
    if(x==0.0)
    {
      Predict0=1.toString() :: Predict0
      Predict1=0.toString():: Predict1
      Predcit2=0.toString():: Predict2
    }
    else if(listOfPrediction==1.0)
    {
      Predict1=1.toString():: Predcit1
      Predict0=0.toString() :: Predict0
      Predcit2= 0.toString():: Predict2

    }

    else if(listOfPrediction==2.0)
    {
      Predict2 =1.toString():: Predict2
      Predict0=0.toString() :: Predict0
      Precict1=0.toString():: Predict1
    }

    else
   {
      Predict2 =0.toString():: Predict2
      Predict0=0.toString() :: Predict0
      Precict1=0.toString():: Predict1
   }
 }

and then I am using the columns values to write them in a CSV file the problem is every time the else part of the condition executes. Why is this happening?

Upvotes: 0

Views: 57

Answers (2)

user11815094
user11815094

Reputation: 1

listOfPrediction is type of List(Row). [1.0] [1.0] [0.0] [0.0] [2.0] these are Row types. Row is nothing but array of columns, so use Indexes to access. Use Index if(x(0)==0.0)

Upvotes: 0

Raphael Roth
Raphael Roth

Reputation: 27373

your code contains lines like listOfPrediction==1.0 which are always false because listOfPrediction is a list and not a number.

You should change it to x.getDouble(0)==1.0 etc (x is a Row)

Upvotes: 0

Related Questions