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

Arrow left icon
Profile Icon David Toth Profile Icon Natingga
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7 (3 Ratings)
Paperback Oct 2018 214 pages 2nd Edition
eBook
€26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon David Toth Profile Icon Natingga
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7 (3 Ratings)
Paperback Oct 2018 214 pages 2nd Edition
eBook
€26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781789806076
Category :
Languages :
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 94.97
Data Science Algorithms in a Week
€32.99
Python Data Science Essentials
€36.99
Artificial Intelligence and Machine Learning Fundamentals
€24.99
Total 94.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.