# Flow-Based Programming for Machine Learning
## 7 Running Examples
### Use Case 3: Music Recommender Application
```
@Getter @Set publicstaticclassRatingimplementsSerializable{ privateIntegeruserId; privateIntegerplaycount; privateIntegerartistId; publicRating(IntegerartistId,Integerplaycount,IntegeruserId){ this.artistId=artistId; this.playcount=playcount; this.userId=userId; } publicstaticRatingparseRating(Stringstr){ String[]fields=str.split(\"-\"); if(fields.length!=3){ thrownewIllegalArgumentException(\"Eachlinemustcontain3 fields\"); }; IntegerartistId=Integer.parseInt(fields[0]); Integerplaycount=Integer.parseInt(fields[1]); IntegeruserId=Integer.parseInt(fields[2]); IntegerfinalArtistData=CollaborativeFiltering.artistAliasMap.getOrDefault(artistId,artistId); returnnewRating(artistId,playcount,userId); }
```The user can create an application with auto-hyperparameter tuning through the graphical interface shown in part 3A of Figure 9. All the available Spark ALS API parameters' settings have been considered to design the collaborative filtering module in our system. In the given dataset, the preference of the user is inferred from the given data. Hence, the implicit preference box should be checked, or in case of sending direct JSON data, the field should contain the true value. Part 3A of Figure 9 depicts how to set the model parameters for CF application. The model parameters are crucial in making a sound recommender system. The user interface allows for setting different parameters for training the model. The code generation for training the model using ALS is done depending on the input parameters from the enduser. Table 2 shows the available option for the end-user, and Listing 5 shows how the parameters are set for the ALS algorithm in Spark internally. \begin{table}
\begin{tabular}{l l l} \hline \hline
**SN.** & **Parameter Name** & **Purpose** \\ \hline
1. & numBlocks & parallelizing the computations by partitioning the data. default value is 10. \\
2. & rank & number of latent factors to be used in the model. default value is 10 \\
3. & maxIter & maximum number of iteration to run the train model. default value is 10 \\
4. & regParam & regularization parameter.default value is 1.0 \\
5. & implicitPrefs & to specify if the data contain implicit or explicit feedback. \\
6. & alpha & default value is false, which means explicit feedback \\
7. & userCol & setting of input data user column name in the ALS algorithm \\
8. & itemCol & setting of input data item col name in the ALS algorithm.in our use case, its artistId \\
9. & ratingCol & setting of input data rating column name in the ALS algorithm \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Supported graphical ML components with their functionality. Figure 9: Graphical flow to create a collaborative filtering model for music recommendation. ```
//restofthecode for(Integerrank:ranks){ for(DoubleregParam:regParams){ for(Doublealpha:alpha)=}{ ALSals=newAIS().setMaxIter(10).setAlpha(alpha).setRegParam(regParam).setImplicitPref(true).setRank(rank).setUserCol(\"userId\").setItemCol(\"artistId\").setRatingCol(\"count\");ALSModelalsModel=als.fit(training);alsModel.setColStartStrategy(\"drop\").Dataset<Row>predictions=alsModel.transform(test); RegressionEvaluatorevaluator=newRegressionEvaluator().setMetricName(\"rmse\")/rmse,mse,mae,r2.setLabelCol(\"count\").setPredictionCol(\"prediction\");Doublermse=evaluator.evaluate(predictions);System.out.println(\"HyperParams=(\"+rank+\"-+alpha+\"-+regParam+\")\");;System.out.println(\"Root-mean-squareerror=\"+rmse); } }
```  
**Listing 5** Example of ALS model creation and evaluation. Spark 2.4.4 has four variants of metrics for ALS model evaluation, namely rmse (Root Mean Square Error), mse (Mean Square Error), mae (Mean Absolute Error) and r2 (RSquared). The RegressionEvaluator uses one of these metrics to evaluate the model. The setting of the metric to this API is shown in Listing 5, and the output of the model evaluation is shown in part 5 of Figure 9. The Spark application outputs the least error and the corresponding model parameters used for ALS model creation.