Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Data Science Algorithms in a Week
Data Science Algorithms in a Week

Data Science Algorithms in a Week: Top 7 algorithms for scientific computing, data analysis, and machine learning , Second Edition

eBook
$35.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Data Science Algorithms in a Week

Naive Bayes

A Naive Bayes classification algorithm assigns a class to an element of a set that is most probable according to Bayes' theorem.

Let's say that A and B are probabilistic events. P(A) is the probability of A being true. P(A|B) is the conditional probability of A being true, given that B is true. If this is the case, then Bayes' theorem states the following:

P(A) is the prior probability of A being true without the knowledge of the probability of P(B) and P(B|A). P(A|B) is the posterior probability of A being true, taking into consideration additional knowledge about the probability of B being true.

In this chapter, you will learn about the following topics:

  • How to apply Bayes' theorem in a basic way to compute the probability of a medical test that is correct in the simple example medical test
  • How to grasp Bayes' theorem...

Medical tests – basic application of Bayes' theorem

A patient takes a special cancer test that has an accuracy of test_accuracy=99.9%—if the result is positive, then 99.9% of the patients tested will suffer from that particular type of cancer. Conversely, 99.9% of the patients with a negative result will not suffer from that particular cancer.

Suppose that a patient is tested and the result is positive. What is the probability of that patient suffering from that particular type of cancer?

Analysis

We will use Bayes' theorem to ascertain the probability of the patient having cancer:

To ascertain the prior probability that a patient has cancer, we have to find out how frequently cancer...

Bayes' theorem and its extension

In this section, we will state and prove both Bayes' theorem and its extension.

Bayes' theorem

Bayes' theorem states the following:

Proof

We can prove this theorem by using elementary set theory on the probability spaces of the events A and B. In other words, here, a probability event will be defined as a set of the possible outcomes in the probability space:

Figure 2.1: Probability space for the two events

As you can see in the preceding diagram, we can state the following relationships:

Rearranging these relationships...

Playing chess – independent events

Suppose we are given the following table of data. This tells us whether or not our friend will play a game of chess with us outside in the park, based on a number of weather-related conditions:

Temperature

Wind

Sunshine

Play

Cold

Strong

Cloudy

No

Warm

Strong

Cloudy

No

Warm

None

Sunny

Yes

Hot

None

Sunny

No

Hot

Breeze

Cloudy

Yes

Warm

Breeze

Sunny

Yes

Cold

Breeze

Cloudy

No

Cold

None

Sunny

Yes

Hot

Strong

Cloudy

Yes

Warm

None

Cloudy

Yes

Warm

Strong

Sunny

?

 

We would like to establish, by using Bayes' theorem, whether our friend would like to play a game of chess with us in the park given that the Temperature is Warm, the Wind is Strong, and it is Sunny.

...

Implementation of a Naive Bayes classifier

In this section, we will implement a program calculating the probability of a data item belonging to a certain class by using Bayes' theorem:

# source_code/2/naive_bayes.py 
# A program that reads the CSV file with the data and returns
# the Bayesian probability for the unknown value denoted by ? to
# belong to a certain class.
# An input CSV file should be of the following format:
# 1. items in a row should be separated by a comma ','
# 2. the first row should be a heading - should contain a name for each
# column of the data.
# 3. the remaining rows should contain the data itself - rows with
# complete and rows with the incomplete data.
# A row with complete data is the row that has a non-empty and
# non-question mark value for each column. A row with incomplete data is
# the row that has the last column with the value...

Playing chess – dependent events

Suppose that we would like to find out whether our friend would like to play chess with us in a park in Cambridge, UK. But, this time, we are given different input data:

Temperature

Wind

Season

Play

Cold

Strong

Winter

No

Warm

Strong

Autumn

No

Warm

None

Summer

Yes

Hot

None

Spring

No

Hot

Breeze

Autumn

Yes

Warm

Breeze

Spring

Yes

Cold

Breeze

Winter

No

Cold

None

Spring

Yes

Hot

Strong

Summer

Yes

Warm

None

Autumn

Yes

Warm

Strong

Spring

?

 

So, now we are wondering how the answer to whether our friend would like to play in a park in Cambridge, UK, will change with this different data in regard to the Temperature being Warm, the Wind being Strong, and the Season being Spring.

...

Gender classification – Bayes for continuous random variables

So far, we have been given a probability event that belonged to one of a finite number of classes, for example, a temperature was classified as cold, warm, or hot. But how would we calculate the posterior probability if we were given the temperature in °C instead?

For this example, we are given the heights of five men and five women, as shown in the following table:

Height in cm

Gender

180

Male

174

Male

184

Male

168

Male

178

Male

170

Female

164

Female

155

Female

162

Female

166

Female

172

?

 

Suppose that the next person has a height of 172 cm. What gender is that person more likely to be, and with what probability?

Analysis

...

Summary

In this chapter, we learned about Naive Bayes and how it can be applied in different ways for different purposes.

Bayes' theorem states the following:

Here, P(A|B) is the conditional probability of A being true, given that B is true. It is used to update the value of the probability that A is true given the new observations about other probabilistic events. This theorem can be extended to a statement with multiple random variables:

The random variables B1,...,Bn have to be conditionally independent given A. The random variables can be discrete or continuous and follow a probability distribution, for example, normal (Gaussian) distribution.

We also studied the discrete random variable. We learned that it is best to ensure that you have a data item for each value of a discrete random variable given any...

Medical tests – basic application of Bayes' theorem


A patient takes a special cancer test that has an accuracy of test_accuracy=99.9%—if the result is positive, then 99.9% of the patients tested will suffer from that particular type of cancer. Conversely, 99.9% of the patients with a negative result will not suffer from that particular cancer.

Suppose that a patient is tested and the result is positive. What is the probability of that patient suffering from that particular type of cancer?

Analysis

We will use Bayes' theorem to ascertain the probability of the patient having cancer:

To ascertain the prior probability that a patient has cancer, we have to find out how frequently cancer occurs among people. Say that we find out that 1 person in 100,000 suffers from this kind of cancer. Therefore, P(cancer)=1/100,000. So, P(test_positive|cancer) = test_accuracy=99.9%=0.999 given by the accuracy of the test.

P(test_positive) has to be computed as follows:

Therefore, we can calculate the following:

So...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Use Python and its wide array of machine learning libraries to build predictive models
  • Learn the basics of the 7 most widely used machine learning algorithms within a week
  • Know when and where to apply data science algorithms using this guide

Description

Machine learning applications are highly automated and self-modifying, and continue to improve over time with minimal human intervention, as they learn from the trained data. To address the complex nature of various real-world data problems, specialized machine learning algorithms have been developed. Through algorithmic and statistical analysis, these models can be leveraged to gain new knowledge from existing data as well. Data Science Algorithms in a Week addresses all problems related to accurate and efficient data classification and prediction. Over the course of seven days, you will be introduced to seven algorithms, along with exercises that will help you understand different aspects of machine learning. You will see how to pre-cluster your data to optimize and classify it for large datasets. This book also guides you in predicting data based on existing trends in your dataset. This book covers algorithms such as k-nearest neighbors, Naive Bayes, decision trees, random forest, k-means, regression, and time-series analysis. By the end of this book, you will understand how to choose machine learning algorithms for clustering, classification, and regression and know which is best suited for your problem

Who is this book for?

This book is for aspiring data science professionals who are familiar with Python and have a little background in statistics. You’ll also find this book useful if you’re currently working with data science algorithms in some capacity and want to expand your skill set

What you will learn

  • Understand how to identify a data science problem correctly
  • Implement well-known machine learning algorithms efficiently using Python
  • Classify your datasets using Naive Bayes, decision trees, and random forest with accuracy
  • Devise an appropriate prediction solution using regression
  • Work with time series data to identify relevant data events and trends
  • Cluster your data using the k-means algorithm

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 31, 2018
Length: 214 pages
Edition : 2nd
Language : English
ISBN-13 : 9781789800968
Category :
Languages :
Concepts :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Oct 31, 2018
Length: 214 pages
Edition : 2nd
Language : English
ISBN-13 : 9781789800968
Category :
Languages :
Concepts :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 125.97
Data Science Algorithms in a Week
$43.99
Python Data Science Essentials
$48.99
Artificial Intelligence and Machine Learning Fundamentals
$32.99
Total $ 125.97 Stars icon

Table of Contents

11 Chapters
Classification Using K-Nearest Neighbors Chevron down icon Chevron up icon
Naive Bayes Chevron down icon Chevron up icon
Decision Trees Chevron down icon Chevron up icon
Random Forests Chevron down icon Chevron up icon
Clustering into K Clusters Chevron down icon Chevron up icon
Regression Chevron down icon Chevron up icon
Time Series Analysis Chevron down icon Chevron up icon
Python Reference Chevron down icon Chevron up icon
Statistics Chevron down icon Chevron up icon
Glossary of Algorithms and Methods in Data Science Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7
(3 Ratings)
5 star 66.7%
4 star 0%
3 star 0%
2 star 0%
1 star 33.3%
Placeholder Nov 27, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book is good and explains in detail
Amazon Verified review Amazon
Rajesh Feb 23, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Algorithm presented with great clarity
Amazon Verified review Amazon
yassine chaachaa Nov 03, 2020
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I am quite disappointed with the book. Yes it has main concepts and algorithms of data science. However, I cannot see the grids or data in the map; for example from page 17 to 20. The map of Italy does not have any data on it. and if it does, I cannot see it. I understand it is being made in black and white to save money. I bought R for data science book and it is all colours (500 pages) for the same price while this book is only 200 pages but they cannot make pages in colours. great disappointment.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.