Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Machine Learning by Example

You're reading from   Python Machine Learning by Example Build intelligent systems using Python, TensorFlow 2, PyTorch, and scikit-learn

Arrow left icon
Product type Paperback
Published in Oct 2020
Publisher Packt
ISBN-13 9781800209718
Length 526 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Yuxi (Hayden) Liu Yuxi (Hayden) Liu
Author Profile Icon Yuxi (Hayden) Liu
Yuxi (Hayden) Liu
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Getting Started with Machine Learning and Python 2. Building a Movie Recommendation Engine with Naïve Bayes FREE CHAPTER 3. Recognizing Faces with Support Vector Machine 4. Predicting Online Ad Click-Through with Tree-Based Algorithms 5. Predicting Online Ad Click-Through with Logistic Regression 6. Scaling Up Prediction to Terabyte Click Logs 7. Predicting Stock Prices with Regression Algorithms 8. Predicting Stock Prices with Artificial Neural Networks 9. Mining the 20 Newsgroups Dataset with Text Analysis Techniques 10. Discovering Underlying Topics in the Newsgroups Dataset with Clustering and Topic Modeling 11. Machine Learning Best Practices 12. Categorizing Images of Clothing with Convolutional Neural Networks 13. Making Predictions with Sequences Using Recurrent Neural Networks 14. Making Decisions in Complex Environments with Reinforcement Learning 15. Other Books You May Enjoy
16. Index

Implementing Naïve Bayes

After calculating by hand the movie preference prediction example, as promised, we are going to code Naïve Bayes from scratch. After that, we will implement it using the scikit-learn package.

Implementing Naïve Bayes from scratch

Before we develop the model, let's define the toy dataset we just worked with:

>>> import numpy as np
>>> X_train = np.array([
...     [0, 1, 1],
...     [0, 0, 1],
...     [0, 0, 0],
...     [1, 1, 0]])
>>> Y_train = ['Y', 'N', 'Y', 'Y']
>>> X_test = np.array([[1, 1, 0]])

For the model, starting with the prior, we first group the data by label and record their indices by classes:

>>> def get_label_indices(labels):
...     """
...     Group samples based on their labels and return indices
...     @param labels: list of labels
...     @return: dict, {class1: [indices], class2: [indices]}...
You have been reading a chapter from
Python Machine Learning by Example - Third Edition
Published in: Oct 2020
Publisher: Packt
ISBN-13: 9781800209718
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime