Make Machine Learning Powered Anime Recommender iOS App in 3 Steps

Nico Prananta
Feb 18 · 5 min read
Image for post
Image for post
Poster for the new season of Boku no Hero Academia and Shingeki no Kyoujin

Who else is excited for the next episode of Attack on Titan or One Piece? 🙋‍♂️ And who else is also excited for the next season of My Hero Academia? 🙋‍♂️ While waiting for these awesome episodes, I’m thinking of watching an anime that I haven’t watched. But what should I watch?

Since Machine Learning (ML) and Artificial Intelligence (AI) have picked up a lot of buzz in the market these days, I figured it’d be fun to make an anime recommender app to tell me what anime to watch based on what others have also watched. This is called a collaborative filtering technique.

Apple has made it relatively easy to create a machine learning powered iOS and macOS appa with Core ML. So in this article I will share how to create an anime recommender app in three steps—Download, Train, and Use.

#1 Download the anime dataset

Image for post
Image for post
Download the dataset

First, we need a dataset. After searching in the internet for the anime dataset to use, I ended up with the MyAnimeList dataset in Kaggle. It contains hundreds of thousands users and millions of ratings. I used the animelists_cleaned.csv because it contains the ratings given by users. I’ve got to warn you, it’s 2GB with more than 30 million rows.

#2 Create and Train ML Model using Create ML.

Image for post
Image for post
Train the model

Next let’s create the recommender model. Here’s the cool thing about what Apple has done with machine learning development in macOS. You don’t need to write any script or code to train an ML model. You can create and train an ML model using the Create ML app. You can find it by opening Xcode, click on Xcode menu, hover on “Open Developer Tool” menu, then click on Create ML.

Image for post
Image for post
Create ML app in Xcode Developer Tool

In the New Project window, you can choose a template for the ML model you want to create. For the anime recommender app, I chose the Recommendation template.

Create ML Templates

Load the animelists_cleaned.csv to the training data, then select username column in Users, anime_id column in Items, and my_score column in Ratings.

Image for post
Image for post
Anime recommender ML Training

Finally, start the training by clicking the Train button. Using the data from the three columns (username, anime_id, and my_score), Create ML will then create a recommendation model by learning from millions of ratings in the dataset. The training only took around a minute, but it will depend on the machine you use.

Once the training completes, you can test it out in the Preview tab by giving it a User, and list of items which are the ids of anime and the rating. For example, since I like Attack on Titan, I entered 25777 as the id and 10 as the rating. You can find the id and the title of an anime in anime_cleaned.csv file.

Image for post
Image for post
Anime recommender Core ML Preview

Last thing to do is to export the model by clicking the Get button in the Output tab.

Image for post
Image for post
Export Core ML model from Create ML

#3 Use the Model in iOS App

Image for post
Image for post
Use the model in the app

The last step is to use the model in the app. Start by dragging the model into your Xcode project. When Xcode imports the model to a project, it automatically generates all the Swift classes so you can use it in the app. The name of the class will follow the name of the model. In my case, the class is called AnimeUserRating.

Image for post
Image for post
Import Core ML model to Xcode project

To get the recommendations, first create an instance of the model (1), then create an input for the model (2), and finally get the predictions from the model (3). See the snippet below.

Using SwiftUI, I made a simple single view app that shows the popular anime and a search text field so user can search for the anime they like. When an anime in the popular list or in the search result is tapped, the app will instantly show a list of anime recommendation.

Image for post
Image for post
Screenshots of anime recommender ML app

You can find the sample app in my Github repository. In the sample app, I simply set value “10” as the rating for the anime which is selected by the user. It is possible however to get better recommendation if we can let user to dislike an anime by setting lower rating in the anime that user dislikes, say 1. I’ll leave that as an exercise to the readers.

Hyperjump is an open-source-first company providing engineering excellence service. We aim to build and commercialize open-source tools to help companies streamline, simplify, and secure the most important aspects of its modern DevOps practices.

Hyperjump Tech

Maju bersama Teknologi Open-Source

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store