Predicting the Cellular Localization Sites of Proteins in Yest

Machine Learning Project Predicting the Cellular Localization Sites of Proteins in Yest based on the available attributes

Data Set Information

  1. Sequence Name: Accession number for the SWISS-PROT database
  2. mcg: McGeoch’s method for signal sequence recognition.
  3. gvh: von Heijne’s method for signal sequence recognition.
  4. alm: Score of the ALOM membrane spanning region prediction program.
  5. mit: Score of discriminant analysis of the amino acid content of the N-terminal region (20 residues long) of mitochondrial and non-mitochondrial proteins.
  6. erl: Presence of “HDEL” substring (thought to act as a signal for retention in the endoplasmic reticulum lumen). Binary attribute.
  7. pox: Peroxisomal targeting signal in the C-terminus.
  8. vac: Score of discriminant analysis of the amino acid content of vacuolar and extracellular proteins.
  9. nuc: Score of discriminant analysis of nuclear localization signals of nuclear and non-nuclear proteins.
  10. class(ME3,ERL,ME1,EXC,VAC,MIT,ME2,POX,NUC,CYT)

Technology Used

  1. Apache Spark
  2. Spark SQL
  3. Apache Spark MLLib
  4. Scala
  5. DataFrame-based API
  6. Databricks Notebook

Challenges

Process .data file (ie file with .data as Extensions) with user define a schema for data

Convert String data to Numeric format so we can process the data in Apache Spark ML Library.

Introduction

Welcome to this project on predicting the Cellular Localization Sites of Proteins in Yest in Apache Spark Machine Learning using Databricks platform community edition server which allows you to execute your spark code, free of cost on their server just by registering through email id.

In this project, we explore Apache Spark and Machine Learning on the Databricks platform.

I am a firm believer that the best way to learn is by doing. That’s why I haven’t included any purely theoretical lectures in this tutorial: you will learn everything on the way and be able to put it into practice straight away. Seeing the way each feature works will help you learn Apache Spark machine learning thoroughly by heart.

We’re going to look at how to set up a Spark Cluster and get started with that. And we’ll look at how we can then use that Spark Cluster to take data coming into that Spark Cluster, a process that data using a Machine Learning model, and generate some sort of output in the form of a prediction. That’s pretty much what we’re going to learn about the predictive model.

In this project, we will be Predicting the Cellular Localization Sites of Proteins in Yest using a Decision Tree Classification algorithm.

We will learn:

  1. Preparing the Data for Processing.
  2. Basics flow of data in Apache Spark, loading data, and working with data, this course shows you how Apache Spark is perfect for Machine Learning job.
  3. Learn basics of Databricks notebook by enrolling into Free Community Edition Server
  4. Define the Machine Learning Pipeline
  5. Train a Machine Learning Model
  6. Testing a Machine Learning Model
  7. Evaluating a Machine Learning Model (i.e. Examine the Predicted and Actual Values)

The Goal is to provide you with practical tools that will be beneficial for you in the future. While doing that, you’ll develop a model with a real use opportunity.

I am really excited you are here, I hope you are going to follow all the way to the end of the Project. It is fairly straight forward fairly easy to follow through the article we will show you step by step each line of code & we will explain what it does and why we are doing it.

Free Account creation in Databricks

Creating a Spark Cluster

Basics about Databricks notebook

Loading Data into Databricks Environment

Download Data

Defining User Define Schema to Load Data in Dataframe

%%scala

import org.apache.spark.sql.Encoders

case class Yest(sequencename: String, 
                mcg: Double,
                gvh: Double, 
                alm: Double,	
                mit: Double,
                erl: Double,
                pox: Double,
                vac: Double,
                nuc: Double,
                classes: String
                )

val YestSchema = Encoders.product[Yest].schema

val YestDF = spark.read.schema(YestSchema).option("header", "false").option("delimiter", "  ").csv("/FileStore/tables/yeast-1.data")

YestDF.show();

Output:

+------------+----+----+----+----+---+---+----+----+-------+
|sequencename| mcg| gvh| alm| mit|erl|pox| vac| nuc|classes|
+------------+----+----+----+----+---+---+----+----+-------+
|  ADT1_YEAST|0.58|0.61|0.47|0.13|0.5|0.0|0.48|0.22|    MIT|
|  ADT2_YEAST|0.43|0.67|0.48|0.27|0.5|0.0|0.53|0.22|    MIT|
|  ADT3_YEAST|0.64|0.62|0.49|0.15|0.5|0.0|0.53|0.22|    MIT|
|  AAR2_YEAST|0.58|0.44|0.57|0.13|0.5|0.0|0.54|0.22|    NUC|
|  AATM_YEAST|0.42|0.44|0.48|0.54|0.5|0.0|0.48|0.22|    MIT|
|  AATC_YEAST|0.51| 0.4|0.56|0.17|0.5|0.5|0.49|0.22|    CYT|
|  ABC1_YEAST| 0.5|0.54|0.48|0.65|0.5|0.0|0.53|0.22|    MIT|
|  BAF1_YEAST|0.48|0.45|0.59| 0.2|0.5|0.0|0.58|0.34|    NUC|
|  ABF2_YEAST|0.55| 0.5|0.66|0.36|0.5|0.0|0.49|0.22|    MIT|
|  ABP1_YEAST| 0.4|0.39| 0.6|0.15|0.5|0.0|0.58| 0.3|    CYT|
|  ACE1_YEAST|0.43|0.39|0.54|0.21|0.5|0.0|0.53|0.27|    NUC|
|  ACE2_YEAST|0.42|0.37|0.59| 0.2|0.5|0.0|0.52|0.29|    NUC|
|  ACH1_YEAST| 0.4|0.42|0.57|0.35|0.5|0.0|0.53|0.25|    CYT|
|  ACON_YEAST| 0.6| 0.4|0.52|0.46|0.5|0.0|0.53|0.22|    MIT|
|  ACR1_YEAST|0.66|0.55|0.45|0.19|0.5|0.0|0.46|0.22|    MIT|
|   ACT_YEAST|0.46|0.44|0.52|0.11|0.5|0.0| 0.5|0.22|    CYT|
|  ACT2_YEAST|0.47|0.39| 0.5|0.11|0.5|0.0|0.49| 0.4|    CYT|
|  ACT3_YEAST|0.58|0.47|0.54|0.11|0.5|0.0|0.51|0.26|    NUC|
|  ACT5_YEAST| 0.5|0.34|0.55|0.21|0.5|0.0|0.49|0.22|    NUC|
|  ADA2_YEAST|0.61| 0.6|0.55|0.21|0.5|0.0| 0.5|0.25|    NUC|
+------------+----+----+----+----+---+---+----+----+-------+
only showing top 20 rows

Collecting all String Columns into an Array

%%scala

var StringfeatureCol = Array("sequencename", "classes" );

StringIndexer encodes a string column of labels to a column of label indices.

Example of StringIndexer

import org.apache.spark.ml.feature.StringIndexer

val df = spark.createDataFrame(
  Seq((0, "a"), (1, "b"), (2, "c"), (3, "a"), (4, "a"), (5, "c"))
).toDF("id", "category")

df.show()

val indexer = new StringIndexer()
  .setInputCol("category")
  .setOutputCol("categoryIndex")

val indexed = indexer.fit(df).transform(df)

indexed.show()

Output:

+---+--------+
| id|category|
+---+--------+
|  0|       a|
|  1|       b|
|  2|       c|
|  3|       a|
|  4|       a|
|  5|       c|
+---+--------+

+---+--------+-------------+
| id|category|categoryIndex|
+---+--------+-------------+
|  0|       a|          0.0|
|  1|       b|          2.0|
|  2|       c|          1.0|
|  3|       a|          0.0|
|  4|       a|          0.0|
|  5|       c|          1.0|
+---+--------+-------------+

Define the Pipeline

A predictive model often requires multiple stages of feature preparation.

A pipeline consists of a series of transformer and estimator stages that typically prepare a DataFrame for modeling and then train a predictive model.

In this case, you will create a pipeline with stages:

  1. A StringIndexer estimator that converts string values to indexes for categorical features
  2. A VectorAssembler that combines categorical features into a single vector
%%scala

import org.apache.spark.ml.attribute.Attribute
import org.apache.spark.ml.feature.{IndexToString, StringIndexer}
import org.apache.spark.ml.{Pipeline, PipelineModel}

val indexers = StringfeatureCol.map { colName =>
  new StringIndexer().setInputCol(colName).setHandleInvalid("skip").setOutputCol(colName + "_indexed")
}

val pipeline = new Pipeline()
                    .setStages(indexers)      

val YestFinalDF = pipeline.fit(YestDF).transform(YestDF)

Print Schema

%%scala

YestFinalDF.printSchema()

Output:

root
 |-- sequencename: string (nullable = true)
 |-- mcg: double (nullable = true)
 |-- gvh: double (nullable = true)
 |-- alm: double (nullable = true)
 |-- mit: double (nullable = true)
 |-- erl: double (nullable = true)
 |-- pox: double (nullable = true)
 |-- vac: double (nullable = true)
 |-- nuc: double (nullable = true)
 |-- classes: string (nullable = true)
 |-- sequencename_indexed: double (nullable = false)
 |-- classes_indexed: double (nullable = false)

Display Data

%%scala

YestFinalDF.show()

Output:

+------------+----+----+----+----+---+---+----+----+-------+--------------------+---------------+
|sequencename| mcg| gvh| alm| mit|erl|pox| vac| nuc|classes|sequencename_indexed|classes_indexed|
+------------+----+----+----+----+---+---+----+----+-------+--------------------+---------------+
|  ADT1_YEAST|0.58|0.61|0.47|0.13|0.5|0.0|0.48|0.22|    MIT|                54.0|            2.0|
|  ADT2_YEAST|0.43|0.67|0.48|0.27|0.5|0.0|0.53|0.22|    MIT|                55.0|            2.0|
|  ADT3_YEAST|0.64|0.62|0.49|0.15|0.5|0.0|0.53|0.22|    MIT|                56.0|            2.0|
|  AAR2_YEAST|0.58|0.44|0.57|0.13|0.5|0.0|0.54|0.22|    NUC|                24.0|            1.0|
|  AATM_YEAST|0.42|0.44|0.48|0.54|0.5|0.0|0.48|0.22|    MIT|                26.0|            2.0|
|  AATC_YEAST|0.51| 0.4|0.56|0.17|0.5|0.5|0.49|0.22|    CYT|                25.0|            0.0|
|  ABC1_YEAST| 0.5|0.54|0.48|0.65|0.5|0.0|0.53|0.22|    MIT|                27.0|            2.0|
|  BAF1_YEAST|0.48|0.45|0.59| 0.2|0.5|0.0|0.58|0.34|    NUC|               122.0|            1.0|
|  ABF2_YEAST|0.55| 0.5|0.66|0.36|0.5|0.0|0.49|0.22|    MIT|                28.0|            2.0|
|  ABP1_YEAST| 0.4|0.39| 0.6|0.15|0.5|0.0|0.58| 0.3|    CYT|                29.0|            0.0|
|  ACE1_YEAST|0.43|0.39|0.54|0.21|0.5|0.0|0.53|0.27|    NUC|                30.0|            1.0|
|  ACE2_YEAST|0.42|0.37|0.59| 0.2|0.5|0.0|0.52|0.29|    NUC|                31.0|            1.0|
|  ACH1_YEAST| 0.4|0.42|0.57|0.35|0.5|0.0|0.53|0.25|    CYT|                33.0|            0.0|
|  ACON_YEAST| 0.6| 0.4|0.52|0.46|0.5|0.0|0.53|0.22|    MIT|                35.0|            2.0|
|  ACR1_YEAST|0.66|0.55|0.45|0.19|0.5|0.0|0.46|0.22|    MIT|                38.0|            2.0|
|   ACT_YEAST|0.46|0.44|0.52|0.11|0.5|0.0| 0.5|0.22|    CYT|                42.0|            0.0|
|  ACT2_YEAST|0.47|0.39| 0.5|0.11|0.5|0.0|0.49| 0.4|    CYT|                39.0|            0.0|
|  ACT3_YEAST|0.58|0.47|0.54|0.11|0.5|0.0|0.51|0.26|    NUC|                40.0|            1.0|
|  ACT5_YEAST| 0.5|0.34|0.55|0.21|0.5|0.0|0.49|0.22|    NUC|                41.0|            1.0|
|  ADA2_YEAST|0.61| 0.6|0.55|0.21|0.5|0.0| 0.5|0.25|    NUC|                43.0|            1.0|
+------------+----+----+----+----+---+---+----+----+-------+--------------------+---------------+
only showing top 20 rows

Split the Data​

It is common practice when building machine learning models to split the source data, using some of it to train the model and reserving some to test the trained model. In this project, you will use 70% of the data for training, and reserve 30% for testing.

%%scala

val splits = YestFinalDF.randomSplit(Array(0.7, 0.3))
val train = splits(0)
val test = splits(1)
val train_rows = train.count()
val test_rows = test.count()
println("Training Rows: " + train_rows + " Testing Rows: " + test_rows)

Prepare the Training Data

To train the Classification model, you need a training data set that includes a vector of numeric features, and a label column. In this project, you will use the VectorAssembler class to transform the feature columns into a vector, and then rename the types of an animal column to the label.

VectorAssembler()

VectorAssembler(): is a transformer that combines a given list of columns into a single vector column. It is useful for combining raw features and features generated by different feature transformers into a single feature vector, in order to train ML models like logistic regression and decision trees.

VectorAssembler accepts the following input column types: all numeric types, boolean type, and vector type.

In each row, the values of the input columns will be concatenated into a vector in the specified order.

%%scala

import org.apache.spark.ml.feature.VectorAssembler

val assembler = new VectorAssembler().setInputCols(Array("mcg", "gvh", "alm", "mit", "erl", "pox", "vac", "nuc")).setOutputCol("features")

val training = assembler.transform(train).select($"features", $"classes_indexed".alias("label"))

training.show()

Output:

+--------------------+-----+
|            features|label|
+--------------------+-----+
|[0.74,0.53,0.5,0....|  0.0|
|[0.51,0.4,0.56,0....|  0.0|
|[0.5,0.54,0.48,0....|  2.0|
|[0.55,0.5,0.66,0....|  2.0|
|[0.43,0.39,0.54,0...|  1.0|
|[0.42,0.37,0.59,0...|  1.0|
|[0.52,0.5,0.53,0....|  8.0|
|[0.4,0.42,0.57,0....|  0.0|
|[0.43,0.38,0.38,0...|  4.0|
|[0.6,0.4,0.52,0.4...|  2.0|
|[0.46,0.41,0.5,0....|  2.0|
|[0.52,0.53,0.58,0...|  2.0|
|[0.66,0.55,0.45,0...|  2.0|
|[0.47,0.39,0.5,0....|  0.0|
|[0.58,0.47,0.54,0...|  1.0|
|[0.5,0.34,0.55,0....|  1.0|
|[0.46,0.44,0.52,0...|  0.0|
|[0.47,0.41,0.46,0...|  3.0|
|[0.48,0.39,0.51,0...|  0.0|
|[0.43,0.53,0.52,0...|  0.0|
+--------------------+-----+
only showing top 20 rows

Train a Classification Model

Next, you need to train a Classification model using the training data. To do this, create an instance of the Decision Tree Classification algorithm you want to use and use its fit method to train a model based on the training DataFrame. In this project, you will use a Decision Tree Classifier algorithm – though you can use the same technique for any of the regression algorithms supported in the spark.ml API.

%%scala

import org.apache.spark.ml.classification.DecisionTreeClassificationModel
import org.apache.spark.ml.classification.DecisionTreeClassifier
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator

val dt = new DecisionTreeClassifier().setLabelCol("label").setFeaturesCol("features")

val model = dt.fit(training)

println("Model Trained!")

Prepare the Testing Data

Now that you have a trained model, you can test it using the testing data you reserved previously. First, you need to prepare the testing data in the same way as you did the training data by transforming the feature columns into a vector. This time you’ll rename the class column to trueLabel.

%%scala

val testing = assembler.transform(test).select($"features", $"classes_indexed".alias("trueLabel"))
testing.show()

Output:
+--------------------+---------+
|            features|trueLabel|
+--------------------+---------+
|[0.45,0.41,0.49,0...|      0.0|
|[0.58,0.44,0.57,0...|      1.0|
|[0.42,0.44,0.48,0...|      2.0|
|[0.4,0.39,0.6,0.1...|      0.0|
|[0.61,0.6,0.55,0....|      1.0|
|[0.33,0.46,0.53,0...|      1.0|
|[0.51,0.51,0.52,0...|      2.0|
|[0.53,0.54,0.43,0...|      1.0|
|[0.64,0.62,0.49,0...|      2.0|
|[0.7,0.78,0.53,0....|      7.0|
|[0.53,0.31,0.58,0...|      1.0|
|[0.49,0.39,0.52,0...|      6.0|
|[0.64,0.59,0.56,0...|      0.0|
|[0.41,0.46,0.59,0...|      1.0|
|[0.38,0.49,0.53,0...|      0.0|
|[0.6,0.47,0.45,0....|      0.0|
|[0.66,0.62,0.49,0...|      0.0|
|[0.44,0.43,0.34,0...|      3.0|
|[0.58,0.51,0.49,0...|      0.0|
|[0.49,0.6,0.3,0.3...|      3.0|
+--------------------+---------+
only showing top 20 rows

Test the Model

%%scala

val prediction = model.transform(testing)
val predicted = prediction.select("features", "prediction", "trueLabel")
predicted.show(10)

Output:

+--------------------+----------+---------+
|            features|prediction|trueLabel|
+--------------------+----------+---------+
|[0.45,0.41,0.49,0...|       0.0|      0.0|
|[0.58,0.44,0.57,0...|       0.0|      1.0|
|[0.42,0.44,0.48,0...|       2.0|      2.0|
|[0.4,0.39,0.6,0.1...|       0.0|      0.0|
|[0.61,0.6,0.55,0....|       0.0|      1.0|
|[0.33,0.46,0.53,0...|       1.0|      1.0|
|[0.51,0.51,0.52,0...|       2.0|      2.0|
|[0.53,0.54,0.43,0...|       1.0|      1.0|
|[0.64,0.62,0.49,0...|       0.0|      2.0|
|[0.7,0.78,0.53,0....|       0.0|      7.0|
+--------------------+----------+---------+
only showing top 10 rows

By Bhavesh