importorg.apache.spark.ml.classification.{BinaryLogisticRegressionSummary,LogisticRegression}// Extract the summary from the returned LogisticRegressionModel instance trained in the earlier
// example
valtrainingSummary=lrModel.summary// Obtain the objective per iteration.
valobjectiveHistory=trainingSummary.objectiveHistoryobjectiveHistory.foreach(loss=>println(loss))// Obtain the metrics useful to judge performance on test data.
// We cast the summary to a BinaryLogisticRegressionSummary since the problem is a
// binary classification problem.
valbinarySummary=trainingSummary.asInstanceOf[BinaryLogisticRegressionSummary]// Obtain the receiver-operating characteristic as a dataframe and areaUnderROC.
valroc=binarySummary.rocroc.show()println(binarySummary.areaUnderROC)// Set the model threshold to maximize F-Measure
valfMeasure=binarySummary.fMeasureByThresholdvalmaxFMeasure=fMeasure.select(max("F-Measure")).head().getDouble(0)valbestThreshold=fMeasure.where($"F-Measure"===maxFMeasure).select("threshold").head().getDouble(0)lrModel.setThreshold(bestThreshold)

importorg.apache.spark.ml.classification.MultilayerPerceptronClassifierimportorg.apache.spark.ml.evaluation.MulticlassClassificationEvaluator// Load the data stored in LIBSVM format as a DataFrame.
valdata=sqlContext.read.format("libsvm").load("data/mllib/sample_multiclass_classification_data.txt")// Split the data into train and test
valsplits=data.randomSplit(Array(0.6,0.4),seed=1234L)valtrain=splits(0)valtest=splits(1)// specify layers for the neural network:
// input layer of size 4 (features), two intermediate of size 5 and 4
// and output of size 3 (classes)
vallayers=Array[Int](4,5,4,3)// create the trainer and set its parameters
valtrainer=newMultilayerPerceptronClassifier().setLayers(layers).setBlockSize(128).setSeed(1234L).setMaxIter(100)// train the model
valmodel=trainer.fit(train)// compute precision on the test set
valresult=model.transform(test)valpredictionAndLabels=result.select("prediction","label")valevaluator=newMulticlassClassificationEvaluator().setMetricName("precision")println("Precision:"+evaluator.evaluate(predictionAndLabels))

importorg.apache.spark.examples.mllib.AbstractParamsimportorg.apache.spark.ml.classification.{OneVsRest,LogisticRegression}importorg.apache.spark.ml.util.MetadataUtilsimportorg.apache.spark.mllib.evaluation.MulticlassMetricsimportorg.apache.spark.mllib.linalg.Vectorimportorg.apache.spark.sql.DataFramevalinputData=sqlContext.read.format("libsvm").load(params.input)// compute the train/test split: if testInput is not provided use part of input.
valdata=params.testInputmatch{caseSome(t)=>{// compute the number of features in the training set.
valnumFeatures=inputData.first().getAs[Vector](1).sizevaltestData=sqlContext.read.option("numFeatures",numFeatures.toString).format("libsvm").load(t)Array[DataFrame](inputData,testData)}caseNone=>{valf=params.fracTestinputData.randomSplit(Array(1-f,f),seed=12345)}}valArray(train,test)=data.map(_.cache())// instantiate the base classifier
valclassifier=newLogisticRegression().setMaxIter(params.maxIter).setTol(params.tol).setFitIntercept(params.fitIntercept)// Set regParam, elasticNetParam if specified in params
params.regParam.foreach(classifier.setRegParam)params.elasticNetParam.foreach(classifier.setElasticNetParam)// instantiate the One Vs Rest Classifier.
valovr=newOneVsRest()ovr.setClassifier(classifier)// train the multiclass model.
val(trainingDuration,ovrModel)=time(ovr.fit(train))// score the model on test data.
val(predictionDuration,predictions)=time(ovrModel.transform(test))// evaluate the model
valpredictionsAndLabels=predictions.select("prediction","label").map(row=>(row.getDouble(0),row.getDouble(1)))valmetrics=newMulticlassMetrics(predictionsAndLabels)valconfusionMatrix=metrics.confusionMatrix// compute the false positive rate per label
valpredictionColSchema=predictions.schema("prediction")valnumClasses=MetadataUtils.getNumClasses(predictionColSchema).getvalfprs=Range(0,numClasses).map(p=>(p,metrics.falsePositiveRate(p.toDouble)))println(s" Training Time ${trainingDuration} sec\n")println(s" Prediction Time ${predictionDuration} sec\n")println(s" Confusion Matrix\n ${confusionMatrix.toString}\n")println("label\tfpr")println(fprs.map{case(label,fpr)=>label+"\t"+fpr}.mkString("\n"))