# Flow-Based Programming for Machine Learning
## 7 Running Examples
### Use Case 1: Predicting Forest Cover with Decision Trees  
The first use case involved creating an ML Spark application based on the Decision tree algorithm. This supervised ML technique splits the input data depending on the model parameters to make a decision. We have used the covtype dataset in this example [38].  
The dataset is available online in compressed CSV format. The dataset reports types of forest covering parcels of land in Colorado, USA. The dataset features describe the parcel of land in terms of its elevation, slope, soil type and the known forest type covers. The dataset has 54 features to describe the pieces of land and 581,012 examples. The forest covers have been categorised into seven different cover types. The categorised values range from 1 to 7.  
Figure 6: IDAR.  
The trained model using the 54 features of the dataset and labelled data should learn forest cover type. The data are already structured, and therefore have used it directly as input in our example.  
The end-user drags various graphical components from the palette to the canvas and connects them in the form of a flow, as shown in Figure 7. The specifics of the application like the input dataset, label column and ML model parameters are taken as inputs from the user. Part 3A in Figure 7 depicts the parameters required to create a decision tree model. Internally, the parameters are specified to make calls to the Spark ML Decision Tree APIs. The flow creation specifications are sent as a JSON object to the back-end system. The decision tree model parameters comprise _impurity_, _depth of the tree_ and _max bins_. The model parameters are crucial to the performance of the model. The impurity parameter minimises the probability of misclassification of the decision tree classifier. The Spark ML library provides two impurity measures for classification, namely, _entropy_ and _Gini_. The impurity is set to the estimator by using the _setImpurity_ function provided by _DecisionTreeClassifier_ Spark API. Similarly, max bins and max depth also play a vital role in finding an optimal decision tree classifier. The end-user can tweak with these settings until the desired outcome is achieved without having to understand the internals of Spark ML APIs or updating the code manually. This process of trying out different model parameters is called hyperparameter tuning. Listing 1 lists the feature extraction function auto-generated for our use case on covtype data. Listing 2 lists the automatic generated code of the decision tree function that includes an estimator and transformer steps of an ML Spark flow.  
```
publicstaticDataset<Row>featureExtraction(SparkSessionspark,StringfilePath,StringlabelColName){Dataset<Row>df=spark.read()option(\"header\",false).option(\"inferSchema\",true).csv(filePath); for(Stringc:df.columns()){df=df.withColumn(c,df.col(c).cast(\"double\")); } df=df.withColumnRenamed(labelColName,\"labelCol\"); df=df.withColumn(\"labelCol\",df.col(\"labelCol\").minus(1)); returndf; }
```  
Listing 1: Generated feature extraction code for use case 1.  
```
publicstaticDecisionTreeClassificationModeldecisionTree(Stringimpurity,int depth,intmaxBins,Dataset<Row>elbyuvroqk){ DecisionTreeClassifierpcesjndfqw=newDecisionTreeClassifier().setLabelCol(\"labelCol\").setFeaturesCol(\"features\").setMaxDepth(depth).setImpurity(impurity).setMaxBins(maxBins); DecisionTreeClassificationModelrwljasxoxf=pscsjndfqw.fit(elbyuvroqk); returnrwljasxoxf; }
```  
Listing 2: Automatic generation of decision tree function.