Complete the code to create a Linear Regression model in Spark.
from pyspark.ml.regression import [1] lr = [1]()
The LinearRegression class is used to create a linear regression model in Spark MLlib.
Complete the code to fit the Linear Regression model on the training data.
model = lr.[1](trainingData)transform instead of fit to train the model.predict which is not a method in Spark MLlib models.The fit method trains the model on the given training data.
Fix the error in the code to evaluate the regression model using RMSE metric.
from pyspark.ml.evaluation import RegressionEvaluator evaluator = RegressionEvaluator(labelCol="label", predictionCol="prediction", metricName="[1]")
The rmse metric stands for Root Mean Squared Error, which is used to evaluate regression models.
Fill both blanks to create a DataFrame with features and label columns for regression.
from pyspark.ml.linalg import Vectors trainingData = spark.createDataFrame([ (1.0, Vectors.dense([1])), (0.0, Vectors.dense([2])) ], ["label", "features"])
The features vectors must be numeric lists. Here, the first example uses [0.0, 1.1, 0.1] and the second uses [2.0, 1.3, 0.5] as feature values.
Fill both blanks to extract the coefficients, intercept, and RMSE of the trained model.
coefficients = model.coefficients intercept = model.[1] rmse = evaluator.evaluate(model.[2](testData)) print(f"Coefficients: {coefficients}, Intercept: {intercept}, RMSE: {rmse}")
predict which is not a Spark MLlib method.fit on test data instead of transform.The model's intercept is accessed by intercept. To get predictions on test data, use transform. The RMSE is computed by evaluating these predictions.
