title: How to get weights from the bestModel of CrossValidator MultilayerPerceptronClasiffier - Spark mllib - Pyspark
body: <p>I have managed to obtain the best model and the best parameters of a model in which I have used <strong>CrossValidator</strong> in <strong>MultilayerPerceptronClassifier</strong>, but I cannot achieve the <strong>weights</strong>, I have tried in some ways but I cannot. I am using Zeppelin and Spark 2.3.2.</p>
  
  <pre><code>layers=[4,4,4]
  
  mlpc = MultilayerPerceptronClassifier(layers=layers, seed=12345L, blockSize=624)
  evaluator = MulticlassClassificationEvaluator(metricName="accuracy")
  
  pipelineMLPC = Pipeline(stages=[mlpc])
  
  paramGrid = ParamGridBuilder() \
      .addGrid(mlpc.maxIter, range(135,136)) \
      .addGrid(mlpc.tol, [1E-3,1E-4]) \
      .addGrid(mlpc.solver, ["l-bfgs"]) \
      .build()
  
  cvPipeline = CrossValidator(estimator = pipelineMLPC,
                      estimatorParamMaps = paramGrid,
                      evaluator = MulticlassClassificationEvaluator(metricName="accuracy"),
                      numFolds = 2)
  
  cvModelPipeline = cvPipeline.fit(train);
  
  
  #Make predictions on test
  prediction = cvModelPipeline.transform(test)
  
  #Obtiene el Accuracy del mejor modelo
  predictionAndLabels = prediction.select("prediction", "label")
  accuracy_test = evaluator.evaluate(predictionAndLabels)
  print("Accuracy test = " + str(accuracy_test))
  
  #Obtiene el mejor modelo 
  bestModelMLPC = cvModelPipeline.bestModel.stages[-1]._java_obj.parent()
  
  #Obtiene los mejores parámetros del mejor modelo
  bestMaxIter = bestModelMLPC.getMaxIter()
  bestTol = bestModelMLPC.getTol()
  bestSolver = bestModelMLPC.getSolver()
  bestBlockSize = bestModelMLPC.getBlockSize()
  bestSeed = bestModelMLPC.getSeed()
  
  
  bestWeights = bestModelMLPC.weights
  print(bestWeights)
  #bestModelMLPC.weights 
  #bestModelMLPC.weight
  #bestModelMLPC.getClassifier().weights
  #bestModelMLPC.getClassifier().weight
  
  print("Best Params: maxIter = " + str(bestMaxIter) + "; tol = " + str(bestTol) + "; solver = " + str(bestSolver) + "; blockSize = " + str(bestBlockSize) + ";  seed = " + str(bestSeed) + "; initialWeights = " + str(bestWeights))
  </code></pre>
  
  <p><strong>With this I get some java but I don't get the weight values that is what I need. Anyone have any idea how I can get it.</strong></p>
  
  <pre><code>Accuracy test = 0.392935019625
  &lt;py4j.java_gateway.JavaMember object at 0x7efe75d45190&gt;
  Best Params: maxIter = 135; tol = 0.001; solver = l-bfgs; blockSize = 624;  seed = 12345; initialWeights = &lt;py4j.java_gateway.JavaMember object at 0x7efe75d45190&gt;
  </code></pre>