This article was published as a part of the Data Science Blogathon.
In this article, we will be predicting the famous machine learning problem statement, i.e. Titanic Survival Prediction, using PySpark’s MLIB. This is one of the best datasets to get started with new concepts as we being machine learning enthusiasts, already are well aware of this particular dataset, and we are gonna do everything from scratch, i.e., from data preprocessing steps, dealing with categorical variables (converting them) and building and evaluating the model using MLIB.
As discussed in the introduction section, we will be predicting which passenger survived the Titanic ship crash, and for that, we will be using PySpark’s MILB library. For doing so, we need first to set up an environment to start the Spark Session, and this will enable us to use all the required libraries we need for the prediction.
from pyspark.sql import SparkSession spark = SparkSession.builder.appName('Titanic_project').getOrCreate() spark
Code breakdown:
So by far, we have set up our Spark Session. Now it’s time to read the legendary Titanic dataset, and for that, we will be using the read.csv method of PySpark, but before heading towards the coding part, let’s first look at the features that the dataset holds.
data = spark.read.csv('titanic.csv',inferSchema=True,header=True)
Inference: By far, we are well aware of the fact that read.csv will read the dataset, but here we will discuss the parameters of this method
Inference: Okay! So now, from the above output, we got the original data type of each column and the information that the particular column will be able to hold the NULL value. Apart from these inferences, we should notice that features like Sex, and Embarked are in the string format, so we need to change them in categorical features.
data.columns
Output:
['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked']
Inference: If one needs to find out what columns are present in the dataset, then he/she can use the columns object corresponding to the dataset.
my_cols = data.select(['Survived', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked'])
Inference: In the previous DataFrame, we got everything (the target column as well, which is not required); hence here, we are filtering the columns to only have features (dependent variables) using the select statement.
There are various methods to deal with null values. We can either impute it with central tendency methods like mean/median/mode depending on the nature of the data, or we can simply drop all the null values here we are dropping all of them as we don’t have many of them; hence it is the better option to get rid of all at once.
Note: NA. drop() method is used to drop all the NA values from the features DataFrame, and then we assign it to a new variable which would be the final data.
my_final_data = my_cols.na.drop()
As we discussed dealing with the categorical columns which are now in the String state, but as we know, String type is not accepted by any ML algorithm, so we need to deal with it, and for that, we have to go through a set of operations/steps in PySpark.
So, let’s break down each step and convert the necessary features columns.
Vector Assembler: From the name itself it is indicating that it kind of puts together columns in a collective vectorized format i.e., all the features get stacked up as a single unit in the form of a vector, and this is one of the rules as well that MLIB library takes all the features as a single unit only.
One Hot Encoder: There are multiple ways of dealing with categorical variables this time going with One hot encoder where each categorical value is separated into an independent column and gets the binary value i.e. either 0 or 1.
from pyspark.ml.feature import (VectorAssembler,VectorIndexer, OneHotEncoder,StringIndexer) gender_indexer = StringIndexer(inputCol='Sex',outputCol='SexIndex') gender_encoder = OneHotEncoder(inputCol='SexIndex',outputCol='SexVec') embark_indexer = StringIndexer(inputCol='Embarked',outputCol='EmbarkIndex') embark_encoder = OneHotEncoder(inputCol='EmbarkIndex',outputCol='EmbarkVec') assembler = VectorAssembler(inputCols=['Pclass', 'SexVec', 'Age', 'SibSp', 'Parch', 'Fare', 'EmbarkVec'],outputCol='features')
Code breakdown:
from pyspark.ml.classification import LogisticRegression
So we are good to go with the model development phase, and the first thing we need to import is the ML algorithm; for this particular problem statement, we have to predict the categorical data; hence classification machine learning algorithm should be accessed, i.e.,, Logistic Regression.
Sometimes coping with the whole process of model development is complex. We get stuck to choosing the right flow if the execution in this type of problem Pipelines from PySpark comes in to rescue us as it helps maintain the execution cycle flow so that each step should be performed at its best given stage neither before nor soon.
from pyspark.ml import Pipeline log_reg_titanic = LogisticRegression(featuresCol='features',labelCol='Survived') pipeline = Pipeline(stages=[gender_indexer,embark_indexer, gender_encoder,embark_encoder, assembler,log_reg_titanic]) train_titanic_data, test_titanic_data = my_final_data.randomSplit([0.7,.3]) fit_model = pipeline.fit(train_titanic_data) results = fit_model.transform(test_titanic_data)
Code breakdown:
Okay! So we are now in the Model Evaluation phase which means development is already done and now we should evaluate it from evaluating, we mean that it should be working as per our requirement with good results i.e., good accuracy over testing data.
from pyspark.ml.evaluation import BinaryClassificationEvaluator my_eval = BinaryClassificationEvaluator(rawPredictionCol='prediction', labelCol='Survived') results.select('Survived','prediction').show()
Output:
Code breakdown:
Here we come in the final section of this article, where we will allow ourselves to go along with whatever we did so far in this article, i.e. from starting the spark session to building and evaluating the model, we will discuss each step in brief.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.