Welcome back! In our previous lesson, we explored essential preprocessing techniques to prepare the Iris dataset for machine learning using PySpark's powerful MLlib
framework. Now, we are ready to take the next step in our journey by training a classification model — specifically, a logistic regression model. By the end of this lesson, you'll be equipped to train your own logistic regression model and understand its parameters.
As we continue our journey with the Iris dataset, let's first set up the Spark session and load the preprocessed data. Remember, in the last lesson, we meticulously prepared the dataset — splitting it into training and testing sets and ensuring that all features and labels are ready for model training.
Here’s how to initialize the Spark session and retrieve our preprocessed data:
Python1from pyspark.sql import SparkSession 2from preprocess_data import preprocess_data 3 4# Initialize a Spark session 5spark = SparkSession.builder.appName("ModelTraining").getOrCreate() 6 7# Load the preprocessed dataset 8train_data, test_data = preprocess_data(spark, "iris.csv")
With the Spark environment ready and data prepared, we're all set to begin training our classification model.
PySpark's MLlib
provides a diverse range of machine learning models to tackle various data analysis challenges. For our classification task, we'll use the logistic regression model, which is well-suited for classification problems, such as predicting the species of iris flowers. This model expects the features to be combined into a single vector column, that consolidates all the individual feature values. In our earlier preprocessing steps, we've successfully structured the dataset with this vectorized "features" column, ensuring that our input perfectly matches the model’s requirements and facilitating a smoother data handling process.
Python1from pyspark.ml.classification import LogisticRegression 2 3# Initialize the logistic regression model with specified feature and label columns 4lr = LogisticRegression(featuresCol="features", labelCol="label")
Although logistic regression is traditionally used for binary classification tasks, where there are only two classes, the LogisticRegression
model in PySpark is versatile enough to automatically perform multinomial logistic regression, also known as softmax regression, when there are more than two classes. This is ideal for our Iris dataset, which contains three classes, allowing the model to seamlessly handle multiple categories without any additional configuration.
With our logistic regression model initialized, the next step is to train it using the preprocessed training data. This involves using the fit
method, which refines the model's internal parameters by learning patterns from the data.
Python1# Fit the logistic regression model to the training data and store the model 2lr_model = lr.fit(train_data)
The fit
method leverages the "features" column, which contains the vectorized input attributes, and the "label" column, which holds the corresponding target labels set earlier during model initialization. This method returns a LogisticRegressionModel
object containing the trained model with its learned parameters. Storing this object as lr_model
is essential because it allows us to use the trained model for making predictions on new data and assessing its performance.
Once you've trained the logistic regression model, examining its parameters helps you understand how it makes predictions. You can view the model's coefficient matrix and intercept vector using the code below:
Python1# Display model parameters 2print("Coefficient Matrix:\n", lr_model.coefficientMatrix) 3print("Intercept Vector:\n", lr_model.interceptVector)
Here's the output you'll see, which reveals the weights (coefficients) for each feature and the intercept values:
Plain text1Coefficient Matrix: 2 DenseMatrix([[ -6.37538389, 32.85084788, -11.07798291, -24.10632798], 3 [ 4.27600161, -13.03274354, 1.15984598, 3.80712585], 4 [ 2.09938228, -19.81810434, 9.91813694, 20.29920214]]) 5Intercept Vector: 6 [3.5534570131111076, 17.040297371527437, -20.593754384638544]
-
Coefficient Matrix: This matrix indicates how much each feature (like petal length or width) affects the prediction. Larger values signify a stronger impact of that feature on the outcome.
-
Intercept Vector: This vector acts as a baseline to adjust the decision boundary before considering any features. It provides an initial value that helps in making accurate predictions.
Understanding these parameters enables you to identify which features significantly influence the prediction of an iris flower's species.
In this lesson, you have successfully trained a logistic regression model using PySpark's MLlib
, built upon the robust preprocessing we accomplished earlier. We've covered setting up the Spark environment, initializing and fitting the model, and examining the trained model's essential parameters.
As you move forward, the practice exercises will give you the opportunity to apply what you've learned, providing hands-on experience to solidify your understanding. These activities will prepare you for future lessons where we'll explore making predictions and evaluating model performance. Keep honing these skills, as mastering them will significantly enhance your proficiency in the field of machine learning with PySpark. Great work today, and let's continue on this exciting journey!