multilayer perceptron classifier code example

Example: Classifier trainer based on the Multilayer Perceptron

# Classifier trainer based on the Multilayer Perceptron

from pyspark.ml.linalg import Vectors
df = spark.createDataFrame([
  (0.0, Vectors.dense([0.0, 0.0])),
  (1.0, Vectors.dense([0.0, 1.0])),
  (1.0, Vectors.dense([1.0, 0.0])),
  (0.0, Vectors.dense([1.0, 1.0]))], ["label", "features"])
mlp = MultilayerPerceptronClassifier(maxIter=100, layers=[2, 2, 2], blockSize=1, seed=123)
model = mlp.fit(df)
model.layers
# [2, 2, 2]
model.weights.size
# 12
testDF = spark.createDataFrame([
  (Vectors.dense([1.0, 0.0]),),
  (Vectors.dense([0.0, 0.0]),)], ["features"])
model.transform(testDF).show()
# +---------+----------+
# | features|prediction|
# +---------+----------+
# |[1.0,0.0]|       1.0|
# |[0.0,0.0]|       0.0|
# +---------+----------+
# ...
mlp_path = temp_path + "/mlp"
mlp.save(mlp_path)
mlp2 = MultilayerPerceptronClassifier.load(mlp_path)
mlp2.getBlockSize()
# 1
model_path = temp_path + "/mlp_model"
model.save(model_path)
model2 = MultilayerPerceptronClassificationModel.load(model_path)
model.layers == model2.layers
# True
model.weights == model2.weights
# True
mlp2 = mlp2.setInitialWeights(list(range(0, 12)))
model3 = mlp2.fit(df)
model3.weights != model2.weights
# True
model3.layers == model.layers
# True