Introduction
In this article, I build a data science workflow step by step, visually and without code, using KNIME.
KNIME is an open-source environment where data can be gathered and wrangled, or understood through modelling and visualisation techniques, in a drag-and-drop way. This lets the user forget about code and focus on the data, the models and the data science concepts.
Moreover, KNIME's visual workflows can be effective in making workflows more readable and easier to maintain over time.
Who should read this article
Python is the most widely used programming language for data science, and many other languages are used in the field today, such as R, Scala, Julia and even JavaScript and C#. But what if you have little or no programming skill? Can you still approach and use data science? By now it should be clear that the answer is yes.
This article is not only for data scientists who want to learn a different way to build and deliver data science products; it also suits data analysts or SQL developers with little programming experience beyond good old SQL, and stakeholders and business analysts who want to do some self-service data science without having to learn to code.
The dataset and the predictive model
In this tutorial, I use the Kaggle dataset IBM HR Analytics Employee Attrition & Performance (see the reference list). It is a fictional dataset created by IBM data scientists, with 50 columns describing 1,470 employees, including age, distance from work, monthly income, total working years and so on. The key field is Attrition, which contains “Yes” if the employee left the company and “No” otherwise. The goal of the tutorial is to load, preprocess and explore the dataset to gather useful insights, and to build a predictive model that can tell whether a person is likely to leave the company.
Installing KNIME
To start the tutorial, we need a copy of KNIME on our laptop. KNIME is free and can be downloaded and installed from https://www.knime.com/downloads.
Setting up the local workspace
During installation, take note of the local KNIME workspace; for example, on my MacBook it is under /Users/mac/knime-workspace.
Before launching KNIME, create a directory (for example EmployeeChurn) in the local workspace and paste the two files EmployeeAttritionLabels.csv and EmployeeData.csv into it. (For example, the path of the first file in my case would be /Users/mac/knime-workspace/EmployeeChurn/EmployeeAttritionLabels.csv.)
Installing an extension for KNIME
Open KNIME and, under File->Install KNIME Extensions…, select the extensions highlighted in Figure 1 to install the ones we will use (extensions contain extra tools, nodes and features beyond the standard version).

Creating the workflow
Now it is time to create our KNIME workflow: select File-> New-> New KNIME Workflow (Figure 2) and name it KNIME-Employee-Churn.

The user interface of KNIME
Now that everything is set up, we can take a quick look at the KNIME user interface, shown in Figure 3. The visual environment has seven main elements:
- The top horizontal menu, with some handy shortcuts to frequently used functions.
- The Explorer at the top left, where you can browse pre-built examples or files in the local workspace
- The Node Repository, where nodes can be browsed by category or found by full-text search and then dragged into the workspace
- The Central Workspace, where visual workflows are built and managed.
- The Outline, which shows a preview of the whole workspace.
- The Console, where error, warning or status messages are displayed.
- The Node Monitor, where the status and information of the selected node are displayed.

Creating nodes: File Reader
Inside the workflow, we can create our first KNIME nodes by dragging the two files from the KNIME Explorer into the workflow area in the centre (Figure 4). KNIME automatically recognises the file type and creates the appropriate Reader Node, in this case a CSV reader. By clicking the labels “Node 1” and “Node 2”, we can rename the two nodes “Attrition Labels” and “Employees Data”.

We could also have created the Reader Nodes from the Node Repository in the menu on the right by selecting CSV Reader under IO->Read (Figure 5).

Double-clicking a Reader lets us explore the file's properties, such as a preview of the first 50 rows. The Reader may have picked the wrong column delimiter, as in Figure 6. We can tell because the columns in the preview are not separated correctly but are all in one long string per row.

To fix this, we can click “Autodetect format” or type “;” directly as the column delimiter, then click OK and Execute to correct the problem and see the columns properly separated, as in Figure 7.

Creating annotations
We can also right-click (ctrl + click on a Mac) anywhere in the KNIME Workspace to create an annotation. Once it is created, we can click the pencil next to it to edit it and play with the font style and the border or background. Here I created a title at the top and a box below the file readers to show that they belong to the “Load Data” step, as shown in Figure 8.

Joining the data sources
Once we have our data sources, we can perform an inner join on them with the Joiner node, which can be found in the Node Repository by typing “join” in the search. Drag the Joiner into the workspace, drop it after the data sources and connect it manually by clicking each small “output” triangle on the data sources and dragging the line to the Joiner's “input” triangle. The final result will look like Figure 9, where a suitable annotation has also been added.

Once the Joiner node is connected, we need to set the join mode. Clicking the Joiner, we choose the “Inner Join” mode and the columns to join on, which are called Id in both tables (Figure 10).

We can also exclude some columns that hold unnecessary information, such as “EmployeeCount”, “EmployeeNumber”, “Over18” and “StandardHours”, in the ” Column Selection ” tab, using the arrows in the multi-select boxes to move them to the Exclude section, as shown in Figure 11.

After clicking “Apply”, we can run the Joiner by right-clicking it and choosing “Execute”, and check that no error messages appear in the console at the bottom right. We can then check that the joined table has indeed combined the employees' “churn” information with the other data by clicking ” Joined Table”, as in Figure 12. Note the “traffic light” below each node: it shows red when the node is not connected or configured correctly, amber when the node is connected, configured and ready to run, and finally green when it has run successfully.

Processing the fields
Inspecting the joined table, we see that many fields are categorical and that the BusinessTravel field is a string with three values meaning no travel, travel with medium frequency and travel with high frequency. To make the data manageable for our predictive model, we first need to convert BusinessTravel into a number (0/1/2) and then encode categorical columns such as “MaritalStatus”, “EducationField”, “Department” and so on. To do this, we can apply a Rule Engine node followed by a One to Many node. As before, we use the Node Repository search to find the Rule Engine node and drag it into the workspace to connect it to the Joiner, as in Figure 13.

Double-clicking the Rule Engine node, we can tell it to replace the “BusinessTravel” column (at the bottom of the window, with the Replace radio button selected and “BusinessTravel” chosen from the drop-down) with values equal to the integer 0 if the field contains the string “Non-Travel”, 1 if it contains “Travel_rarely” and 2 if it contains “Travel_Frequently”, as expressed by the simple rules we added in the Expression box (Figure 14). As usual, remember to apply, execute and then inspect the results.

To perform one-hot encoding, we add and connect the One to Many node and configure it to replace (tick the “Remove included columns from output “ box) all the string columns with one-hot encoded columns except “Attrition ”, using the arrows in the multi-select dialog to move “Attrition” to the left, as shown in Figure 10. “Attrition ” does not need converting because it is not an independent variable in our problem but the dependent variable to be predicted.
Here we get a warning (a yellow exclamation mark on the node) in the console below that reads “Possible duplicate values found. The original column name will be appended“. That is right, since some of the selected columns share identical field values (for example “Human Resources” appears in both the JobRole and EducationalField columns), so to avoid confusion the new columns are all named with the field value joined to the original column name, for example “Human_Resources_Job_Role”.
Again, we execute and inspect the result to check it (see the “One Hot Encoding” entry in the reference list if you need a refresher on the topic).

The last action we take on the fields is to colour the rows red or blue depending on the value of the churn field, which will be useful in the visualisation part, for example when plotting or exploring the decision tree's results. We do this by adding a Color Manager and connecting it as shown in Figure 16.

In the Color Manager, we can link red to the Attrition value “Yes” and blue to the Attrition value “No” by selecting the values and clicking the desired colour in the palette, as shown in Figure 17.

Preparation: partitioning and sampling
The last steps needed before we can run the training are partitioning and sampling our dataset. Partitioning is done because we need to “put aside ” part of the dataset to validate our trained model later. Of course, if the model were trained and tested on the same data, its accuracy would be misleadingly high, because it would be tested on samples it has already “seen ”.
Training the model on 80% of the samples and validating it on the remaining 20% of previously unseen samples is common practice to avoid this trap and measure a reliable accuracy. To split the records into two random 80-20 sets, we can use the Partitioning node, as shown in Figure 18.

Random selection and the 80-20 ratio can easily be set in the Partitioning node's window, as shown in Figure 19.

The last preparation step concerns the 20% dataset used for validation. It needs further processing to make sure the two classes, churning employees and non churning employees, are equally represented. This is achieved by randomly removing rows of the majority class until the two classes have the same number of rows.
We need this step because if the classes were highly imbalanced, say 9 out of 10 employees do not churn, a very simple (and dumb) model that predicts “no churn ” 100% of the time would still reach 90% accuracy, which would again be misleading. To get equal representation we can use the Equal Size Sampling node, as shown in Figure 20.

The Equal Size Sampling node is configured by selecting the column that holds our class of interest, here “Attrition”, and the mode “Use selected sampling”, as shown in Figure 21. As usual, do not forget to execute the node.

Training the model
The moment has come to train our predictive model, which will be a Decision Tree. A decision tree is an algorithm that recursively splits the rows of a dataset into binary subsets, each time deciding the split on a threshold value of a single column, in a way that maximises the homogeneity of the subsets with respect to the class variable.
For example, if I were training a decision tree to predict the quality of red wine from a set of variables and the algorithm split on sulphate content, the concentration threshold for the split would be the value that gives the highest possible share of good wines in one subset and the lowest in the other.
The ultimate goal of the algorithm is to build the smallest tree among the many possible ones while keeping its predictive power on new samples (see more about decision trees in the model evaluation section and in the reference list).
To train the decision tree in KNIME, I need to create a Decision Tree Learner node and connect it to the 80% output of the Partitioning node, as shown in Figure 22.

The decision tree's configuration window, where we leave all parameters at their defaults for this tutorial, appears in Figure 23 (check that the class we are training the tree on is indeed “Attrition”).

Evaluating the model
Once the model is trained, the first thing we can do is explore the insights the tree has learned by right-clicking the Decision Tree Learner node and choosing “View: Decision Tree View” from the drop-down menu. As shown in Figure 24, the first split is on the TotalWorkingYears column, with a threshold of 1.5 years, which is the most effective at producing subsets with the greatest separation with respect to the Attrition class.
Again from the figure, we get a valuable insight: the probability of attrition is much higher for people who have been with the company for less than 1.5 years and much lower once that threshold is passed, so the company should take special care to retain new employees.

Exploring the tree with the navigator on the right of the tree view and opening branches with the “+” symbol reveals other useful insights, such as those in Figure 25, which shows how people younger than 33.5 or with a monthly income below $2,470 are more likely to leave.

We can also check the model's accuracy on the 20% dataset by feeding the model into a Decision Tree Predictor node, which can load a trained decision tree model and predict class outcomes for an input dataset. The Decision Tree Predictor node is shown in Figure 26. Here too we accept the default parameters and execute the node.

The prediction results can be explored by right-clicking the node and choosing “Classified Data” at the bottom of the drop-down menu. A new column called Prediction (Attrition) appears on the right, and the predicted results can be checked visually against the original label, shown by the record's colour (red = “Yes”, blue = “No”), as in Figure 27.

To measure how many predictions were right or wrong, we can add and execute the last node of this tutorial, the Scorer node, as shown in Figure 28.

The Scorer node is configured as usual by double-clicking it, making sure the accuracy is measured between “Attrition” and ” Prediction(Attrition) ”, as shown in Figure 29.

Finally, we can check the model's accuracy by right-clicking the Scorer node and choosing the View: Confusion Matrix option, as shown in Figure 30. As the figure shows, 48 of the 51 non-churners (the “No” row of the matrix) were identified correctly, while only 20 of the 51 churners were.
This gives a total of 68 correctly classified samples against 34 misclassified ones, an overall accuracy of 2/3 (66.667%). That is clearly better than a “random” classifier, which would give 50% accuracy on an evenly balanced dataset, and not a bad result for a model using all default settings without any fine-tuning, but the workflow certainly has clear room for improvement (as a hint for further practice, the author reached 85% accuracy using a Logistic Regression model instead of the Decision Tree).

Conclusions
We have built a very simple data workflow and predictive model with KNIME's visual environment. You can download the workflow used in the example here. There is much more to learn in the KNIME self-paced courses and in the KNIME cheat sheets (see the reference list).
I hope you found this article useful, and I encourage you to connect with me on LinkedIn for feedback, suggestions or just to keep in touch.
References
Decision Tree: https://towardsdatascience.com/decision-trees-in-machine-learning-641b9c4e8052
KNIME cheat sheets: https://www.knime.com/cheat-sheets
KNIME self-paced course: https://www.knime.com/knime-self-paced-courses
IBM HR Analytics Employee Attrition & Performance: https://www.kaggle.com/pavansubhasht/ibm-hr-analytics-attrition-dataset
One Hot Encoding: https://towardsdatascience.com/what-is-one-hot-encoding-and-how-to-use-pandas-get-dummies-function-922eb9bd4970
Download the workflow shown in the example from the KNIME Hub: https://we.tl/t-bbHp182L0C
As previously published on LinkedIn Pulse https://www.linkedin.com/pulse/codeless-data-science-knime-fabio-rebecchi/
Post links
- Source: medium
- Via: Rosaria Silipo

