<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hans Wai CHAN on Medium]]></title>
        <description><![CDATA[Stories by Hans Wai CHAN on Medium]]></description>
        <link>https://medium.com/@hanswai93?source=rss-909feca6a7af------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*coQ1DwKqieUQSjBA6jrxfA.png</url>
            <title>Stories by Hans Wai CHAN on Medium</title>
            <link>https://medium.com/@hanswai93?source=rss-909feca6a7af------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 07:09:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hanswai93/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Anime Alchemy: Unveiling Your Next Favorite through Machine Learning (1/4)]]></title>
            <link>https://medium.com/@hanswai93/anime-alchemy-unveiling-your-next-favorite-through-machine-learning-1-4-1d1c40d5419f?source=rss-909feca6a7af------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d1c40d5419f</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[anime]]></category>
            <category><![CDATA[recommendation-system]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Hans Wai CHAN]]></dc:creator>
            <pubDate>Sun, 20 Aug 2023 14:43:24 GMT</pubDate>
            <atom:updated>2023-08-23T12:30:33.654Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b_Aawaa2iiuAJlsQjoc3Pw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@graciadharmaa?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Gracia Dharma</a> on <a href="https://unsplash.com/photos/qTlbO6mkQH0?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Anime, a realm where imagination knows no bounds, enthralls viewers with its diverse worlds and compelling narratives. Yet, the vast expanse of anime choices can leave enthusiasts overwhelmed when selecting their next watch. This is where the fusion of machine learning and anime enters the stage. By harnessing the capabilities of algorithms and data analysis, machine learning offers a transformative solution to the eternal question: “What anime should I watch next?”</p><p>Gone are the days of aimlessly scrolling through recommendations or relying on random selections. Machine learning algorithms, fueled by the wealth of data from fans and streaming platforms, now provide tailored suggestions that resonate with individual preferences. In this exploration of anime and machine learning, we unravel the mechanics behind recommendation systems, the data they rely on, and the techniques that empower them to accurately predict your next anime obsession.</p><p>From collaborative and content-based filtering to hybrid models and deep learning, we’ll demystify these algorithms and their ability to capture the essence of what makes a show captivating. However, beyond the technical underpinnings, we’ll also delve into the intricate connection between data-driven insights and the artistry that underpins every anime tale.</p><p>Whether you’re an anime veteran seeking fresh adventures or a newcomer ready to embark on an animated journey, join us as we navigate the world of anime recommendations powered by machine learning. Together, we’ll uncover how this fusion of technology and creativity is reshaping the way we indulge in the enchanting realm of anime.</p><p>I will cover the following things, probably in separate pages:</p><p><strong>• Data Preprocessing </strong><br>• Collaborative Filtering using Single Value Decomposition<br>• Collaborative Filtering using Neural Nets<br>• Hybrid Collaborative Filtering.</p><p><strong>Data Preprocessing</strong></p><p>Data Preprocessing is not the most interesting but it is essential for laying the foundations for accurate and meaningful analysis. Raw data (especially when scraped) often contains imperfections, inconsistencies and noise that can hinder the performance of the machine learning models.</p><p>Data used for this project contains approximately 300k unique users. <em>Thank you Azathoth for saving me from needing to scrape the data myself. (It took me 5–10 minutes per user to scrape all the review data, and I quickly realised that it was going to take too long.)</em></p><p>Link to the data is here on Kaggle: <a href="https://www.kaggle.com/datasets/azathoth42/myanimelist?select=users_cleaned.csv">https://www.kaggle.com/datasets/azathoth42/myanimelist?select=users_cleaned.csv</a></p><p>Pandas is a great Python library to use for analysing, understanding and wrangling.</p><p>Using this data, the aim is the achieve a clean set of data for user id, anime id which are the features (X) and rating (Y)</p><pre>import pandas as pd<br>anime_raw = pd.read_csv(&#39;./data/full/anime_cleaned.csv&#39;)<br>scores_raw = pd.read_csv(&#39;./data/full/animelists_cleaned.csv&#39;)<br>users_raw = pd.read_csv(&#39;./data/full/users_cleaned.csv&#39;)</pre><pre># Need user_id and user_name<br>users_raw.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8tQKu3FNpWQBg5PZf9LRJg.png" /></figure><p>`username`, `user_id` will be useful here.</p><p>Interestingly `stats_mean_score` suggests that each person will have different distribution of ratings. There was some thought to renormalise the ratings for each user based on their mean score. However I decided against it as some users may only be willing to review good or users may only be willing to review bad anime. I cannot distinguish this from optimisistic reviewers and pessimistic reviews respectively.</p><pre>scores_raw.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CbFRU7kQhB4kyksvxHthCw.png" /></figure><p>my_score is the label which can be joined together user_raw</p><pre>reviews_df = scores_raw[[&#39;username&#39;, &#39;anime_id&#39;, &#39;my_score&#39;, &#39;my_status&#39;]]<br>usernames_and_ids = users_raw[[&#39;username&#39;, &#39;user_id&#39;]]<br><br>reviews_df = reviews_df.merge(anime_raw[[&#39;anime_id&#39;, &#39;title&#39;]], left_on=&#39;anime_id&#39;, right_on=&#39;anime_id&#39;)<br>reviews_df = reviews_df.merge(usernames_and_ids[[&#39;username&#39;, &#39;user_id&#39;]], left_on=&#39;username&#39;, right_on=&#39;username&#39;)<br># Remove scores with 0 - it&#39;s not a possible value acccording to MyAnimeList<br>reviews_df = reviews_df[reviews_df[&#39;my_score&#39;] &gt; 0]<br>reviews_df.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*FU8Ihysug2bFk1hKcYPGQg.png" /></figure><p>Let’s use a crude way to check for sparsity of the data we have.</p><pre>user_per_anime = reviews_df[&#39;anime_id&#39;].value_counts().reset_index().rename(columns={&quot;count&quot;: &quot;number_of_users&quot;})<br>anime_per_user = reviews_df[&#39;username&#39;].value_counts().reset_index().rename(columns={&quot;count&quot;: &quot;number_of_animes&quot;})</pre><p>There were a bunch of users where they have only reviewed very few animes, making the user entry very sparse. This will not be good for the collaborative filtering algorithm.</p><p>Only taking users that have made 10 ratings or more is a bit arbitary but it’s something to work with now. This can be tweaked if the model is not performing well.</p><p>I can filter this based on `value_counts()` and then join it back in with inner join on the username and anime_id</p><pre>user_per_anime = user_per_anime[user_per_anime[&#39;number_of_users&#39;] &gt; 10]<br>anime_per_user = anime_per_user[anime_per_user[&#39;number_of_animes&#39;] &gt; 10]<br><br>reviews_df = pd.merge(reviews_df, anime_per_user, left_on = &#39;username&#39;, right_on = &#39;username&#39;, how = &#39;inner&#39;)<br>reviews_df = pd.merge(reviews_df, user_per_anime, left_on = &#39;anime_id&#39;, right_on = &#39;anime_id&#39;, how = &#39;inner&#39;)<br>reviews_df = reviews_df[[&#39;anime_id&#39;, &#39;user_id&#39;, &#39;my_score&#39;]]<br>reviews_df</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*5Z1xk0ndOiAEqgqVFx0L1g.png" /></figure><p>Perfect. With this, the data is in the correct format before we pipe it into the various techniques we will be covering in the next few articles.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d1c40d5419f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[3 types of similarity metrics used in collaborative filtering recommender systems]]></title>
            <link>https://medium.com/@hanswai93/3-types-of-similarity-metrics-used-in-collaborative-filtering-recommender-systems-1b18db37732?source=rss-909feca6a7af------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b18db37732</guid>
            <category><![CDATA[similarity-metrics]]></category>
            <category><![CDATA[collobarative-filtering]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[recommender]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Hans Wai CHAN]]></dc:creator>
            <pubDate>Fri, 10 Jun 2022 16:54:18 GMT</pubDate>
            <atom:updated>2022-06-11T21:30:30.430Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ih3JI46vGrSy20hAmoaH6w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@pimchu?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Pim Chu</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>If you want to get suggested a new movie to watch, new song to listen or even new article to read, a simple approach is to reach out to a friend. Based on their suggestions, you may notice that some friends will have “better” taste than others. That may work initially but as list of movies and songs get larger simply reaching out to a small group from friends becomes insufficient — unless you’re really lucky!</p><p>Collaborative Filtering (CF) usually works by evaluating a large number of users and based on your preferences, matches a subset that show similar taste to yours.</p><p>In this article, I’m going to cover the different ways a recommender system considers users as similar, via a similarity score.</p><h3>Euclidean distance</h3><p>This is a very straightforward approach to calculate a similarity score. Conceptualise by taking the users rankings and plot them into a preference space. Users that are close together in preference space are considered to be similar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*1Fr23P5LzNuIaeaI" /><figcaption>This chart only shows a two-dimensional preference space. The concept is the same for higher dimensions when there are more show ratings.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*etFlmBLt8Z7kjEmny83m5w.png" /></figure><p>where r represents the preference vector.</p><p>To establish the distance to represent similarity, it is better to observe the inverse and plus 1 to avoid division by 0:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*woMXZvbGnTWB_91Tz-_xLQ.png" /></figure><p>When distance, d_ij tends towards infinity the similarity score tends towards 0. On the contrary, as d_ij tends towards 0 the similarity score tends towards 1. With this in mind, d_ij is bounded between 0 and 1. (0 ≤ d_ij ≤ 1)</p><p>For code example, if we take a Dataframe with two columns denoted by x and y. You can use <em>numpy</em> to help you via the <em>linalg.norm()</em></p><pre># Suppose we have two columns: data[&#39;x&#39;], data[&#39;y&#39;]  </pre><pre>distance = np.linalg.norm(data[&#39;x&#39;] - data[&#39;y&#39;]) <br>score = 1/(1+distance)</pre><h3>Pearson coefficient</h3><p>A more sophisticated approach to determine similarities between two users is to calculate the Pearson correlation coefficient. The maths behind it is slightly more complex, but it tends to deal with cases where the data isn’t well normalised. For example, if a particular user commonly reviews more harshly than average, or a user rarely gives 1-star reviews.</p><p>To conceptualise this approach, let’s plot the ratings of two users on a chart:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*KgdWgJlrWCnLS9am" /><figcaption>Points on this scatter plot are further from the best-fit line. Correlation between the users are lower. (r=~0.4)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*yHGkr9ZNTjNZTtAM" /><figcaption>Points on this scatter plot are on the best-fit line. Correlation between the users here is strong. (r=1)</figcaption></figure><p>When plotting a best-fit line we can determine the correlation between two users by evaluating the distances between the points and this line. The lower the distances of all the points with the line, the strong the users are correlated. The correlation between the users Toby and Dill is much higher than Toby and Bob.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*4_u8c60EKgEci2MZzZLmgg.png" /></figure><p>Note that if you want to calculate the correlation between two items, you can use the same equation.</p><p>Fear not because the code example is a simple one liner using pandas <a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.corr.html">corr()</a>.</p><pre># Suppose we have two columns: data[&#39;x&#39;], data[&#39;y&#39;]  <br>score = data.corr(method=&#39;pearson&#39;)</pre><h3>Tanimoto Coefficient</h3><p>Tanimoto coefficient denotes the ratio of intersections for two different users/shows. This dataset is mostly used for sparse datasets or datasets where the ratings are binary (likes and dislikes)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/0*dgYcvSHmyXRr1DB-" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8dsbLrw_FrwqVJZsbC-KOQ.png" /></figure><p>In vector form, it will be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*xlWEh_rd9-4hfpMiaoo75g.png" /></figure><p>where X and Y represent different users. When preferences for the two users are the same, the score will be 1, on the contrary, if the preferences for the two users do not overlap at all, the score will be 0.</p><p>For code example, I was only able to find numpy’s dot(). If you find anything else that is easier for panda dataframes, give me a shout!</p><pre># Suppose we have two columns: data[&#39;x&#39;], data[&#39;y&#39;] <br>X_dot_Y = np.dot(data[&#39;x&#39;], data[&#39;y&#39;]) <br>X_dot_X = np.dot(data[&#39;x&#39;], data[&#39;x&#39;]) <br>Y_dot_Y = np.dot(data[&#39;y&#39;], data[&#39;y&#39;])   </pre><pre>distance = X_dot_Y / (X_dot_X + Y_dot_Y - X_dot_Y )</pre><h3>Conclusion</h3><p>Thanks for reading! Hope you have gained more insight in the different similarity metrics: Euclidean distance, Pearson coefficient and Tanimoto coefficient. These metrics are very useful in both item-based or user-based collaborative filtering. With these scores calculated, you can make predictions on the score a given user will rate.</p><p>There are plenty of other metrics, if you’re interested you can look into Loglikelihood Similarity, Spearman Correlation Similarity and cosine distance.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b18db37732" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>