In this example, we build a machine learning application to predict turbofan engine degradation. This application is structured into three important steps:
Prediction Engineering
Feature Engineering
Machine Learning
In the first step, we generate new labels from the data by using Compose. In the second step, we generate features for the labels by using Featuretools. In the third step, we search for the best machine learning pipeline by using EvalML. After working through these steps, you will learn how to build machine learning applications for real-world problems like predictive maintenance. Let’s get started.
[1]:
from demo.turbofan_degredation import load_sample from matplotlib.pyplot import subplots import composeml as cp import featuretools as ft import evalml
We will use a dataset provided by NASA simulating turbofan engine degradation. In this dataset, we have engines which are monitored over time. Each engine had operational settings and sensor measurements recorded for each cycle. The remaining useful life (RUL) is the amount of cycles an engine has left before it needs maintenance. What makes this dataset special is that the engines run all the way until failure, giving us precise RUL information for every engine at every point in time.
[2]:
df = load_sample() df.head()
5 rows × 27 columns
Which range is the RUL of a turbofan engine in?
In this prediction problem, we want to group the RUL into ranges. Then, predict which range the RUL is in. We can make variations of the ranges to create different prediction problems. For example, the ranges can be manually defined (0 - 150, 150 - 300, etc.) or based on the quartiles from historical observations. These variations can be done by simply binning the RUL. This helps us explore different scenarios which is crucial for making better decisions.
Let’s start by defining the labeling function of an engine that calculates the RUL. Given that engines run all the way until failure, the RUL is just the remaining number of observations. Our labeling function will be used by a label maker to extract the training examples.
[3]:
def rul(ds): return len(ds) - 1
Then, let’s represent the prediction problem by creating a label maker with the following parameters:
The target_entity as the column for the engine ID, since we want to process records for each engine.
target_entity
The labeling_function as the function we defined previously.
labeling_function
The time_index as the column for the event time.
time_index
[4]:
lm = cp.LabelMaker( target_entity='engine_no', labeling_function=rul, time_index='time', )
Now, let’s run a search to get the training examples by using the following parameters:
The records sorted by the event time, since the search will expect the records to be sorted chronologically, otherwise an error will be raised.
num_examples_per_instance as the number of training examples to find for each engine.
num_examples_per_instance
minimum_data as the amount of data that will be used to make features for the first training example.
minimum_data
gap as the number of rows to skip between examples. This is done to cover different points in time of an engine.
gap
We can easily tweak these parameters and run more searches for training examples as the requirements of our model changes.
[5]:
lt = lm.search( df.sort_values('time'), num_examples_per_instance=20, minimum_data=5, gap=20, verbose=False, ) lt.head()
The output from the search is a label times table with three columns:
The engine ID associated to the records. There can be many training examples generated from each engine.
The event time of the engine. This is also known as a cutoff time for building features. Only data that existed beforehand is valid to use for predictions.
The value of the RUL. This is calculated by our labeling function.
At this point, we only have continuous values of the RUL. As a helpul reference, we can print out the search settings that were used to generate these labels.
[6]:
lt.describe()
Settings -------- gap 20 minimum_data 5 num_examples_per_instance 20 target_column rul target_entity engine_no target_type continuous window_size None Transforms ---------- No transforms applied
We can also get a better look at the values by plotting the distribution and the cumulative count across time.
[7]:
%matplotlib inline fig, ax = subplots(nrows=2, ncols=1, figsize=(6, 8)) lt.plot.distribution(ax=ax[0]) lt.plot.count_by_time(ax=ax[1]) fig.tight_layout(pad=2)
With the continuous values, we can explore different ranges without running the search again. In this case, we will just use quartiles to bin the values into ranges.
[8]:
lt = lt.bin(4, quantiles=True, precision=0)
When we print out the settings again, we can now see that the description of the labels has been updated and reflects the latest changes.
[9]:
Label Distribution ------------------ (111.0, 153.0] 6 (38.0, 74.0] 5 (5.0, 38.0] 6 (74.0, 111.0] 5 Total: 22 Settings -------- gap 20 minimum_data 5 num_examples_per_instance 20 target_column rul target_entity engine_no target_type discrete window_size None Transforms ---------- 1. bin - bins: 4 - labels: None - precision: 0 - quantiles: True - right: True
Let’s have a look at the new label distribution and cumulative count across time.
[10]:
fig, ax = subplots(nrows=2, ncols=1, figsize=(6, 8)) lt.plot.distribution(ax=ax[0]) lt.plot.count_by_time(ax=ax[1]) fig.tight_layout(pad=2)
In the previous step, we generated the labels. The next step is to generate the features.
Let’s start by representing the data with an entity set. This way, we can generate features based on the relational structure of the dataset. We currently have a single table of records where one engine can have many records. This one-to-many relationship can be represented by normalizing an engine entity. The same can be done for other one-to-many relationships. We want to make predictions based on the engine, so we will use this engine entity as the target entity for generating features.
[11]:
es = ft.EntitySet('observations') es.entity_from_dataframe( dataframe=df.reset_index(), entity_id='records', index='id', time_index='time', ) es.normalize_entity( base_entity_id='records', new_entity_id='engines', index='engine_no', ) es.normalize_entity( base_entity_id='records', new_entity_id='cycles', index='time_in_cycles', ) es.plot()
Now, we can generate features by using a method called Deep Feature Synthesis (DFS). This will automatically build features by stacking and applying mathematical operations called primitives across relationships in an entity set. The more structured an entity set is, the better DFS can leverage the relationships to generate better features. Let’s run DFS using the following parameters:
entity_set as the entity set we structured previously.
entity_set
target_entity as the engine entity.
cutoff_time as the label times that we generated previously. The label values are appended to the feature matrix.
cutoff_time
[12]:
fm, fd = ft.dfs( entityset=es, target_entity='engines', agg_primitives=['sum'], trans_primitives=[], cutoff_time=lt, cutoff_time_in_index=True, include_cutoff_time=False, verbose=False, ) fm.head()
5 rows × 25 columns
There are two outputs from DFS: a feature matrix and feature definitions. The feature matrix is a table that contains the feature values with the corresponding labels based on the cutoff times. Feature definitions are features in a list that can be stored and reused later to calculate the same set of features on future data.
In the previous steps, we generated the labels and features. The final step is to build the machine learning pipeline.
Let’s start by extracting the labels from the feature matrix and splitting the data into a training set and a holdout set.
[13]:
y = fm.pop('rul').cat.codes splits = evalml.preprocessing.split_data( X=fm, y=y, test_size=0.2, random_state=2, ) X_train, X_holdout, y_train, y_holdout = splits
Then, let’s run a search on the training set for the best machine learning model. During the search process, predictions from several different pipelines are evaluated to find the best pipeline.
[14]:
automl = evalml.AutoMLSearch( problem_type='multiclass', objective='f1 macro', random_state=0, allowed_model_families=['catboost', 'random_forest'], max_pipelines=3, ) automl.search( X_train, y_train, data_checks='disabled', show_iteration_plot=False, )
Generating pipelines to search over... ***************************** * Beginning pipeline search * ***************************** Optimizing for F1 Macro. Greater score is better. Searching up to 3 pipelines. Allowed model families: catboost, random_forest (1/3) Mode Baseline Multiclass Classificati... Elapsed:00:00 Starting cross validation Finished cross validation - mean F1 Macro: 0.113 (2/3) CatBoost Classifier w/ Imputer Elapsed:00:00 Starting cross validation Finished cross validation - mean F1 Macro: 0.569 (3/3) Random Forest Classifier w/ Imputer Elapsed:00:00 Starting cross validation Finished cross validation - mean F1 Macro: 0.458 Search finished after 00:01 Best pipeline: CatBoost Classifier w/ Imputer Best pipeline F1 Macro: 0.569444
Once the search is complete, we can print out information about the best pipeline found, such as the parameters in each component.
[15]:
automl.best_pipeline.describe() automl.best_pipeline.graph()
********************************** * CatBoost Classifier w/ Imputer * ********************************** Problem Type: Multiclass Classification Model Family: CatBoost Pipeline Steps ============== 1. Imputer * categorical_impute_strategy : most_frequent * numeric_impute_strategy : mean * categorical_fill_value : None * numeric_fill_value : None 2. CatBoost Classifier * n_estimators : 10 * eta : 0.03 * max_depth : 6 * bootstrap_type : None * silent : True * allow_writing_files : False
Let’s score the model performance by evaluating predictions on the holdout set.
[16]:
best_pipeline = automl.best_pipeline.fit(X_train, y_train) score = best_pipeline.score( X=X_holdout, y=y_holdout, objectives=['f1 macro'], ) dict(score)
{'F1 Macro': 0.7}
From the pipeline, we can see which features are most important for predictions.
[17]:
feature_importance = best_pipeline.feature_importance feature_importance = feature_importance.set_index('feature')['importance'] top_k = feature_importance.abs().sort_values().tail(20).index feature_importance[top_k].plot.barh(figsize=(8, 8), fontsize=14, width=.7);
<AxesSubplot:ylabel='feature'>
We are ready to make predictions with our trained model. First, let’s calculate the same set of features by using the feature definitions. We will use a cutoff time based on the latest information available in the dataset.
[18]:
fm = ft.calculate_feature_matrix( features=fd, entityset=es, cutoff_time=ft.pd.Timestamp('2001-01-08'), cutoff_time_in_index=True, verbose=False, ) fm.head()
3 rows × 24 columns
Now, let’s predict which one of the four ranges the RUL is in.
[19]:
values = best_pipeline.predict(fm).values prediction = fm[[]] prediction['rul (estimate)'] = values prediction.head()
At this point, we have completed the machine learning application. We can revisit each step to explore and fine-tune with different parameters until the model is ready for deployment. For more information on how to work with the features produced by Featuretools, take a look at the Featuretools documentation. For more information on how to work with the models produced by EvalML, take a look at the EvalML documentation.