<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>Data preprocessing and model training</title>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.4.1/milligram.css">
</head>
<body style="padding: 2em">
    <div style="text-align: center;">
        <h2>Data preprocessing and model training</h2>
    </div>
    <p>
        Sparks's inbuilt function to train a model takes a dataframe of 'implicit preferences' given by users to some
        products. userID ~ user_id, productID ~ recording_id and preference ~ transformed count as represented by rows
        in playcounts dataframe.
    </p>
    <p>Playcounts dataframe is loaded from HDFS in <b>{{ time_load_playcounts }}</b> and is of the form:</p>
    <table>
        <tr>
            <th>user_id</th>
            <th>recording_id</th>
            <th>playcount</th>
            <th>transformed_listencount</th>
        </tr>
    </table>

    <p>
        Here, playcount is the actual listen count whereas the transformed_listencount was calculated by applying the
        following function on playcounts.
    </p>
    <p><b>{{ listencount_transformer_description }}</b></p>

    <p>
        Preprocessing of playcounts dataframe takes <b>{{ time_preprocessing }}</b>. The preprocessed data divided the
        data into training and test data, in {{ train_test_ratio }}. The model was then trained on the training data
        using K-Folds Cross Validation with {{ num_folds }} folds. From the models trained, the one with least RMSE
        is selected to generate recommendations.
    </p>
    <p>
        Of all the trained models, the model with the least RMSE value is chosen to generate recommendations. The model
        will be referred to as <b>"best model"</b>.
    </p>

    <p>Best model parameters are as follows: </p>
    <table>
        <tr>
            <th>Best model ID</th>
            <th>Best model rank</th>
            <th>Best model lmbda</th>
            <th>Best model iteration</th>
            <th>Best model alpha</th>
            <th>Best model RMSE</th>
        </tr>
        <tr>
            <td>{{ best_model.model_id }}</td>
            <td>{{ best_model.rank }}</td>
            <td>{{ best_model.lmbda }}</td>
            <td>{{ best_model.iteration }}</td>
            <td>{{ best_model.alpha }}</td>
            <td>{{ best_model.validation_rmse | round(3) }}</td>
        </tr>
    </table>

    <p>Test RMSE for the best model is <b>{{ test_rmse | round(3) }}</b></p>
    <p>Total time lapsed in data preprocessing and model training: <b>{{ time_total }}</b></p>
    <p>Best Model saved in <b>{{ time_model_save }}</b></p>
    <p>All the models trained in <b>{{ time_model_training }}</b></p>
    <p>The following table gives information about all the models trained</p>

    <table>
        <tr>
            <th>model id</th>
            <th>rank</th>
            <th>lmbda</th>
            <th>iterations</th>
            <th>alpha</th>
            <th>RMSE</th>
        </tr>
        {% for model in all_models -%}
            <tr>
                <td>{{ model.model_id }}</td>
                <td>{{ model.rank }}</td>
                <td>{{ model.lmbda }}</td>
                <td>{{ model.iteration }}</td>
                <td>{{ model.alpha }}</td>
                <td>{{ model.validation_rmse | round(3) }}</td>
            </tr>
        {% endfor -%}
    </table>

    <h4>Following are the parameters required to train the model</h4>
    <ul>
        <li>
            <b>rank</b>
            <br/>
            This refers to the number of factors in our ALS model, that is,the number of hidden features
            in our low-rank approximation matrices. A rank in the range of 10 to 200 is usually reasonable
        </li>
        <li>
            <b>lmbda</b>
            <br/>
            This parameter controls the regularization of our model.Thus, lambda controls over fitting.
        </li>
        <li>
            <b>iterations</b>
            <br/>
            This refers to the number of iterations to run(around 10 is often a good default).
        </li>
        <li>
            <b>alpha</b>
            <br/>
            The alpha parameter controls the baseline level of confidence weighting applied. A higher level of alpha
            tends to make the model more confident about the fact that missing data equates to no preference for the
            relevant user-item pair.
        </li>
    </ul>

    <p>
        The Mean Squared Error (MSE) is a direct measure of the reconstruction error of the user-item rating matrix. It
        is defined as the sum of the squared errors divided by the number of observations. The squared error, in turn,
        is the square of the difference between the predicted rating for a given user-item pair and the actual
        rating.
    </p>
    <p>
        Ratings are predicted for all the (user_id, recording_id) pairs in validation data, the predicted ratings are
        then subtracted with actual ratings and RMSE is calculated.
    </p>
    <p>
        <i>
            <b>Note:</b> Number of rows in a dataframe or number of elements in a dataframe (count information) is not
            included because it leads to unnecessary computation time.
        </i>
    </p>

</body>
</html>
