The goal of this module is to show you how to construct feature vectors from the raw event sequences data through Hadoop Pig, a high-level data processing tool which runs on top of Hadoop MapReduce. Instead of writing a Java program, you will write a high level script using Pig Latin and let the framework translate it into MapReduce jobs for you.
Throughout this training, you will learn how to run Pig both interactively and in script (batch) mode. We will first demonstrate basic knowledge of Pig's interactive shell and data types, then show how to complete the feature construction task step by step. The high-level process of feature construction is depicted below:
Pig provides a shell to manipulate data interactively. Let's start a shell and run that in local mode for demo purpose
and you will see the prompt as
Next, you can input a Pig Latin statement, the basic construct for using Pig. For example,
Here we call the case_events
a relation in Pig Latin. In this statement, we load data from the data/case.csv
file into the case_events
relation. We also specify the schema of the data as
which defines a four-field tuple with names and types of each field corresponding to our raw data. Here we use PigStorage
, the most common adapter in Pig to load/save data from/into a file system (including HDFS). Of course you can load data from other sources (such as databases) using other Storage
interfaces.
You can check the schema using the DESCRIBE
operator
and collect display data with DUMP
Sometimes, DUMP
will generate a lot of output but you may just want to see a few examples. Pig itself doesn't have an operator like head; instead you can run a command like
to print the top 10 items in relation A
.
Pig will not run immediately after you input a statement. Only when you need to save
or dump
will Pig actually run. The good part of this property is that internally Pig can be optimized. A potential problem is that you may not realize you have made a mistake until you reach a later statement that has output. If you are not sure, on a small data set, you can dump
frequently.
The shell also provides other commands. Important ones include (but are not limited to)
fs
: serves the same purpose as hdfs dfs
, so that you can type fs -ls
directly in the Pig shell instead of hdfs dfs -ls
.pwd
: prints the present working directory (useful in debugging 'file not found' errors).Type help
in the Pig shell to learn more about these commands. Pig operators covered in later examples are listed in the table below; please refer to Pig Official Documentation to learn more.
Operator | Explanation |
---|---|
DISTINCT | Removes duplicate tuples in a relation |
FILTER | Selects tuples from a relation based on some condition |
FOREACH | Generates data transformations based on columns of data |
GROUP | Groups the data in one or more relations |
JOIN (inner) | Performs an inner join of two or more relations based on common field values |
LIMIT | Limits the number of output tuples |
LOAD | Loads data from the file system |
ORDER BY | Sorts a relation based on one or more fields |
RANK | Returns each tuple with the rank within a relation |
SPLIT | Partitions a relation into two or more relations |
STORE | Stores or saves results to the file system |
UNION | Computes the union of two or more relations, does not eliminate duplicate tuples |
REGISTER | Registers a JAR file so that the UDFs in the file can be used |
Finally, type quit
to leave the shell.
In this section, we briefly describe data types. Pig can work with simple types like int
, double
. More important types are tuple
and bag
.
Tuple is usually represented by ()
. For example,
In Pig Latin, we can either fetch fields by index (like $0
) or by name (like patientid
). With index we can also fetch a range of fields. For example $2..
means "all fields from the 2-nd to the last."
Bag is usually denoted with {}
. From the result of DESCRIBE case_events
we can see that case_events
itself is a bag. You can regard a bag as a special unordered set
that doesn't check for duplicates (a multiset).
Check out the official documentation about data types for more. You will find examples of different types in the samples below. Pay attention to the result of DESCRIBE
where you will find types and names of fields.
Next, you will learn by practicing how to construct features for predictive modeling. You will learn how to use built-in operators like GROUP BY
and JOIN
as well as how to create User Defined Functions (UDFs) in python. The result of feature construction will be a feature matrix that can be used by a lot of machine learning packages.
Feature construction works as shown below, where sample data format of each step is depicted.
We start by loading the raw data. Then we extract the prediction target (i.e. whether the patient will have heart failure or not). Next, we filter and aggregate events of patient into features. After that we need to associate the prediction target and feature vectors to generate complete training/testing samples. Finally we split the data into training and testing sets and save.
First, make sure you are in bigdata-bootcamp/sample/pig
folder and check availability of the raw data file with:
Then, let's load the data as a relation
:
Our data set can be used for predicting heart failure (HF), and we want to predict heart failure one year before it happens. This one-year delay between our observations and our prediction target is the prediction window, as illustrated in the figure below.
We will need to find the HF diagnostic date and use that date to filter out events within the prediction window. We do this using the heart failure event date. All patients will have an HF event, but for case
patients the event value will be 1
(meaning HF occurred), while for control
patients the event value is 0 as there's no HF.
After JOIN
we have some redundant fields that we will no longer need, so we can project filtered_events
into a simpler format:
Notice that as dateoffset is no longer useful after filtering, we dropped it from the projection.
Our raw data is an event sequence. In order to aggregate that into features suitable for machine learning, we can sum up values for each event type into a single feature value corresponding to the given event. We will directly use the event name as the name of the corresponding feature. For example, given the raw event sequence for a patient with ID FBFD014814507B5C
below:
We can get a (feature name, value) pair for this patient as
The code below will aggregate filtered_events
from the previous filter step into tuples in (patientid, feature name, feature value)
format:
In a machine learning setting, we want to assign an index to each different feature rather than using its name directly. For example, DIAG38845 corresponds to feature-id=1 and DIAGV6546 corresponds to feature-id=2.
The code below is used to extract unique feature names using the DISTINCT
operator and assign an index to each feature name with RANK
operator.
Next, we can update feature_name_values
to use the feature index rather than the feature name.
Now, we are approaching the final step. We need to create a feature vector for each patient. Our ultimate result will represent each patient as a feature vector associated with the target we want to predict. We already have target in the targets
relation. Our final representation is shown below:
For example, given a patient 2363A06EF118B098
with the following features who doesn't have heart failure (target value is 0)
we will encode the patient features as:
Notice that the featureid
is in increasing order and this is required by a lot of machine learning packages. We call such target (aka label) and feature vector pairs a sample
.
Let's group feature_id_values
by patientid and check the structure:
We find that feature_id_values
is a bag and want to convert it into a string like 1:60 4:30 9:60 23:10 45:90
as mentioned above. Here we will employ a UDF defined in utils.py
as
The script simply enumerates all tuples from input
and forms id-value pairs then joins them. @outputSchema("feature:chararray")
specifies the return value name and tuple. In order to use the script file, we need to register it first:
Next, we can join targets
and feature_vectors
to associate each feature vector with the corresponding target:
We are almost there -- now just save the samples
. In a machine learning setting it is common practice to split data into training and testing sets. We can do that by associating each sample with a random key and splitting on that random key.
Then, we can save the resulting sets:
Running commands interactively is efficient, but sometimes we want to save the commands for future reuse. We can save the commands we run into a script file (e.g. features.pig
) and run the entire script in batch mode.
You can check this out in sample/pig folder. Navigate to there and simply run the script with
Additional conditions can be applied together with the 1 year prediction window, e.g.,