Icon

04 Machine Learning - Solution

<p><strong>Learning objective: </strong>In this exercise you will train and evaluate supervised and unsupervised machine learning models.</p><p><strong>Workflow description:</strong></p><p>The same customer transaction data set from the demo is used in this exercise, with some additional features on the loyalty program membership information.</p><ol><li><p>The <strong><em>Loading Customer Data</em></strong> metanode reads the customer information as well as the loyalty program membership information.</p></li><li><p>Training a classification model to predict the membership status (<strong><em>Member</em></strong> or <strong><em>Gold</em></strong>). A decision tree model is trained, and its performance is evaluated. (<strong><em>Activity I</em></strong>)</p></li><li><p>Training a linear regression model to predict the Total spending with the number of purchased items and the loyalty program points. The goodness-of-fit of the trained model is then evaluated. (<strong><em>Activity II</em></strong>)</p></li><li><p>Grouping customers with similar characteristics into clusters by k-Means clustering. Visualizing the resulting clusters. (<strong><em>Activity III</em></strong>)</p></li></ol>
Annotations and comments
  • Introduction to Data Science 04 - Machine Learning
  • Loading the customer data The metanode Loading Customer Data reads the customer transaction data and the membership data from two sources. The output data set contains the customer information n_items: number of unique items purchased Total spending City Newsletter: newsletter subscription Customer group: CC1, CC2, or CC3 Status: membership status, either Member or Gold Loyalty points
  • Activity I: Classification of the Membership Status From the output data of the Loading Customer Data metanode, remove the column CustomerID with a Column Filter node Partition the data into two partitions: training (70%) and testing (30%) with a Table Partitioner node. Stratify the sampling according to the classes in the Status column. Train a decision tree model to classify the Status with a Decision Tree Learner node. Set the Min number records per node to 5. Apply the trained decision tree model to the testing data with a Decision Tree Predictor node. Check Append columns with normalized class distribution. Evaluate the classification performance with a Scorer node, with Status as the First Column and Prediction (Status) as the Second Column. Plot an ROC curve from the prediction results with a ROC Curve node. The Target column is Status, Positive class value is Gold, and plot P (Status=Gold).
  • Activity II: Regression Model to Predict Total Spending Use the output data of the Loading Customer Data metanode and partition into two partitions: training (70%) and testing (30%) with a Table Partitioner node. Select Draw randomly. Train a linear regression model with a Linear Regression Learner node. Predict Total spending by n_items and Loyalty points. Open the view and examine the parameter estimates from the trained model. Apply the trained regression model to the testing data with a Regression Predictor node. Evaluate the goodness-of-fit of the regression model with a Numeric Scorer node, with Total spending as the Reference column and Prediction (Total spending) as the Predicted column.
  • Activity III: Grouping Customers with Similar Characteristics by Clustering In the output data of the Loading Customer Data metanode, normalize numerical columns with a Normalizer node. Use the Min-Max Normalization with Min=0.0 and Max=1.0. Perform k-means clustering with a k-Means node. Set the Number of clusters to 3. Use all the numerical columns (n_items, Total spending, & Loyalty points) for clustering. Visualize the clusters discovered in the previous step. With a Color Manager node, assign different colors to different classes of the column Cluster. Generate a scatter plot with a Scatter Plot node, with n_items on the x-axis and Loyalty points on the y-axis. Use Cluster as the Color dimension.
  • Assigning different colors to clusters
  • Removing Customer ID
  • Training decision tree model
  • Generate predicted class probabilities
  • Renaming columns
  • Reading customer transaction data
  • Summary for each customer
  • Merging tables
  • Removing extremely old people
  • Loyalty program membership data
  • Normalizing numerical columns
  • Evaluating goodness-of-fit
  • Training:Testing 70:30 Stratified on status
  • Training:Testing 70:30
  • Plotting ROC curve
  • Applying the model to testing data
  • Scatter plot of n_items vs Loyalty points
  • Assessing the model fit
  • K-means clustering with 3 clusters

Nodes

Extensions

Links