A Java implementation of Alternating Least Squares (ALS). Supports:
- Batch ALS
- Stochastic Gradient Descent (SGD) ALS
API documentation available here. A live, online Jupyter notebook is available here or by clicking the above launch binder button.
You can use this library as a Maven dependency by adding
<dependency>
<groupId>org.ruivieira.ml</groupId>
<artifactId>als</artifactId>
<version>0.0.3</version>
</dependency>
to your Maven dependencies
.
You should then specify the dependency location by using one of the following methods:
Add the repository information to your pom.xml
:
<profiles>
<profile>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-ruivieira-maven</id>
<name>bintray</name>
<url>https://dl.bintray.com/ruivieira/maven</url>
</repository>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-ruivieira-maven</id>
<name>bintray-plugins</name>
<url>https://dl.bintray.com/ruivieira/maven</url>
</pluginRepository>
</pluginRepositories>
<id>bintray</id>
</profile>
</profiles>
Or by downloading this reposity and installing the library locally by running
$ mvn clean install
A minimal example consists on parsing a ratings dataset into a collection such as List<Rating> ratings
. Given this collection, a sparse ratings matrix can be created using
SparseRealMatrix R = ALSUtils.toMatrix(ratings);
The initial latent factors can then be created with
LatentFactors factors = LatentFactors.create(nUsers, nItems, rank);
We create a BatchALS
instance
BatchALS als = new BatchALS(R, rank, alpha, beta);
and the ALS process can then be repeated for n
iterations:
for (int iter = 0 ; iter < n ; iter++) {
factors = als.run(factors);
}
After which, we can use the factors to approximate the ratings matrix, which will provide the rating predictions.
RealMatrix approximation = ALSUtils.approximate(factors);