Book Image

Python Machine Learning Cookbook

By : Prateek Joshi, Vahid Mirjalili
Book Image

Python Machine Learning Cookbook

By: Prateek Joshi, Vahid Mirjalili

Overview of this book

Machine learning is becoming increasingly pervasive in the modern data-driven world. It is used extensively across many fields such as search engines, robotics, self-driving cars, and more. With this book, you will learn how to perform various machine learning tasks in different environments. We’ll start by exploring a range of real-life scenarios where machine learning can be used, and look at various building blocks. Throughout the book, you’ll use a wide variety of machine learning algorithms to solve real-world problems and use Python to implement these algorithms. You’ll discover how to deal with various types of data and explore the differences between machine learning paradigms such as supervised and unsupervised learning. We also cover a range of regression techniques, classification algorithms, predictive modeling, data visualization techniques, recommendation engines, and more with the help of real-world examples.
Table of Contents (19 chapters)
Python Machine Learning Cookbook
About the Author
About the Reviewer

Generating movie recommendations

Now that we built all of the different parts of a recommendation engine, we are ready to generate movie recommendations. We will use all the functionality that we built in the previous recipes to build a movie recommendation engine. Let's see how to build it.

How to do it…

  1. Create a new Python file, and import the following packages:

    import json
    import numpy as np
    from euclidean_score import euclidean_score
    from pearson_score import pearson_score
    from find_similar_users import find_similar_users
  2. We will define a function to generate movie recommendations for a given user. The first step is to check whether the user exists in the dataset:

    # Generate recommendations for a given user
    def generate_recommendations(dataset, user):
        if user not in dataset:
            raise TypeError('User ' + user + ' not present in the dataset')
  3. Let's compute the Pearson score of this user with all the other users in the dataset:

        total_scores = {}
        similarity_sums = {}