<jupyter_start><jupyter_text>Batch ALS<jupyter_code>import org.apache.commons.math3.linear.SparseRealMatrix;
import org.ruivieira.ml.als.ALSUtils;

SparseRealMatrix R = ALSUtils.toMatrix(ratings);
int maxUser = ALSUtils.maxUser(ratings);
int maxItem = ALSUtils.maxItem(ratings);

System.out.println("max user: " + maxUser + "\nmax item: " + maxItem);
import org.ruivieira.ml.als.LatentFactors;

int rank = 4;

LatentFactors factors = LatentFactors.create(maxUser, maxItem, rank);
import org.ruivieira.ml.als.BatchALS;

BatchALS als = new BatchALS(R, rank, 0.001, 0.001);
int iterations = 100;

for (int i = 0 ; i < iterations ; i++) {
    factors = als.run(factors);
}
import org.apache.commons.math3.linear.RealMatrix;

RealMatrix nR = ALSUtils.approximate(factors);
public double MSE(SparseRealMatrix ratings, RealMatrix predictions) {

    int rows = ratings.getRowDimension();
    int cols = ratings.getColumnDimension();
    
    int counter = 0;
    double se = 0.0;
    for (int i = 0 ; i < rows ; i++) {
        for (int j = 0 ; j < cols ; j++) {
            double rating = ratings.getEntry(i, j);
            if (rating > 0d) {
                counter++;
                se += Math.pow(rating - predictions.getEntry(i, j), 2.0);
            }
        }
    }
    return se / (double) counter;
}
MSE(R, nR);<jupyter_output><empty_output><jupyter_text>Stochastic Gradient Descent ALS<jupyter_code>import org.ruivieira.ml.als.SGDALS;

SGDALS sgd_als = new SGDALS(rank);
factors = LatentFactors.create(maxUser, maxItem, rank);
for (Rating rating : ratings) {
    List<Rating> _r = new ArrayList<>();
    _r.add(rating);
    factors = sgd_als.run(factors, _r);
}
nR = ALSUtils.approximate(factors);
MSE(R, nR);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13638968481375358, 'fraction_numerical': 0.013180515759312322, 'mean_word_length': 3.594736842105263, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '361681', 'license': 'Apache-2.0', 'n_tokens_mistral': 665, 'n_tokens_neox': 634, 'n_words': 135, 'path': 'docs/examples.ipynb', 'repo_name': 'friturier/java-als', 'size': '1745'}