Download Complete Python Machine Learning Case Studies: Five Case Studies for the Data Scientist 1st Edition Danish Haroon PDF for All Chapters
Download Complete Python Machine Learning Case Studies: Five Case Studies for the Data Scientist 1st Edition Danish Haroon PDF for All Chapters
com
https://textbookfull.com/product/python-machine-learning-
case-studies-five-case-studies-for-the-data-scientist-1st-
edition-danish-haroon-2/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/python-machine-learning-case-studies-
five-case-studies-for-the-data-scientist-1st-edition-danish-haroon-2/
textboxfull.com
https://textbookfull.com/product/the-family-nurse-practitioner-
clinical-case-studies-case-studies-in-nursing-leslie-neal-boylan/
textboxfull.com
https://textbookfull.com/product/transit-oriented-development-
learning-from-international-case-studies-ren-thomas/
textboxfull.com
https://textbookfull.com/product/project-management-case-studies-
harold-kerzner/
textboxfull.com
https://textbookfull.com/product/case-studies-in-forensic-psychology-
ruth-tully/
textboxfull.com
https://textbookfull.com/product/case-studies-in-building-
rehabilitation-j-m-p-q-delgado/
textboxfull.com
Python
Machine Learning
Case Studies
Five Case Studies for the Data Scientist
—
Danish Haroon
Python Machine
Learning Case
Studies
Five Case Studies for the
Data Scientist
Danish Haroon
Python Machine Learning Case Studies
Danish Haroon
Karachi, Pakistan
ISBN-13 (pbk): 978-1-4842-2822-7 ISBN-13 (electronic): 978-1-4842-2823-4
DOI 10.1007/978-1-4842-2823-4
Library of Congress Control Number: 2017957234
Copyright © 2017 by Danish Haroon
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole
or part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical
way, and transmission or information storage and retrieval, electronic adaptation, computer
software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the
date of publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Cover image by Freepik (www.freepik.com)
Managing Director: Welmoed Spahr
Editorial Director: Todd Green
Acquisitions Editor: Celestin Suresh John
Development Editor: Matthew Moodie
Technical Reviewer: Somil Asthana
Coordinating Editor: Sanchita Mandal
Copy Editor: Lori Jacobs
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505,
e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is
a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc
(SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit
http://www.apress.com/rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available
to readers on GitHub via the book’s product page, located at www.apress.com/978-1-4842-2822-7.
For more detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
Contents at a Glance
■
■Chapter 1: Statistics and Probability���������������������������������������������� 1
■
■Chapter 2: Regression������������������������������������������������������������������ 45
■
■Chapter 3: Time Series����������������������������������������������������������������� 95
■
■Chapter 4: Clustering������������������������������������������������������������������ 129
■
■Chapter 5: Classification������������������������������������������������������������ 161
■
■Appendix A: Chart types and when to use them������������������������� 197
Index���������������������������������������������������������������������������������������������� 201
iii
Contents
■
■Chapter 1: Statistics and Probability���������������������������������������������� 1
Case Study: Cycle Sharing Scheme—Determining Brand Persona�������� 1
Performing Exploratory Data Analysis����������������������������������������������������� 4
Feature Exploration�������������������������������������������������������������������������������������������������� 4
Types of variables����������������������������������������������������������������������������������������������������� 6
Univariate Analysis��������������������������������������������������������������������������������������������������� 9
Multivariate Analysis���������������������������������������������������������������������������������������������� 14
Time Series Components���������������������������������������������������������������������������������������� 18
Measuring Center of Measure��������������������������������������������������������������� 20
Mean����������������������������������������������������������������������������������������������������������������������� 20
Median�������������������������������������������������������������������������������������������������������������������� 22
Mode����������������������������������������������������������������������������������������������������������������������� 22
Variance������������������������������������������������������������������������������������������������������������������ 22
Standard Deviation������������������������������������������������������������������������������������������������� 23
Changes in Measure of Center Statistics due to Presence of Constants���������������� 23
The Normal Distribution������������������������������������������������������������������������������������������ 25
v
■ Contents
Correlation��������������������������������������������������������������������������������������������� 34
Pearson R Correlation��������������������������������������������������������������������������������������������� 34
Kendall Rank Correlation���������������������������������������������������������������������������������������� 34
Spearman Rank Correlation������������������������������������������������������������������������������������ 35
■
■Chapter 2: Regression������������������������������������������������������������������ 45
Case Study: Removing Inconsistencies in Concrete
Compressive Strength��������������������������������������������������������������������������� 45
Concepts of Regression������������������������������������������������������������������������ 48
Interpolation and Extrapolation������������������������������������������������������������������������������ 48
Linear Regression��������������������������������������������������������������������������������������������������� 49
Least Squares Regression Line of y on x���������������������������������������������������������������� 50
Multiple Regression������������������������������������������������������������������������������������������������ 51
Stepwise Regression���������������������������������������������������������������������������������������������� 52
Polynomial Regression������������������������������������������������������������������������������������������� 53
vi
■ Contents
Assumptions of Regressions����������������������������������������������������������������� 54
Number of Cases���������������������������������������������������������������������������������������������������� 55
Missing Data����������������������������������������������������������������������������������������������������������� 55
Multicollinearity and Singularity����������������������������������������������������������������������������� 55
Features’ Exploration���������������������������������������������������������������������������� 56
Correlation�������������������������������������������������������������������������������������������������������������� 58
Types of Regression������������������������������������������������������������������������������ 70
Linear Regression��������������������������������������������������������������������������������������������������� 71
Grid Search������������������������������������������������������������������������������������������������������������� 75
Ridge Regression���������������������������������������������������������������������������������������������������� 75
Lasso Regression��������������������������������������������������������������������������������������������������� 79
ElasticNet��������������������������������������������������������������������������������������������������������������� 81
Gradient Boosting Regression�������������������������������������������������������������������������������� 82
Support Vector Machines���������������������������������������������������������������������������������������� 86
Applications of Regression�������������������������������������������������������������������� 89
Predicting Sales������������������������������������������������������������������������������������������������������ 89
Predicting Value of Bond����������������������������������������������������������������������������������������� 90
Rate of Inflation������������������������������������������������������������������������������������������������������ 90
Insurance Companies��������������������������������������������������������������������������������������������� 91
Call Center�������������������������������������������������������������������������������������������������������������� 91
vii
■ Contents
Agriculture�������������������������������������������������������������������������������������������������������������� 91
Predicting Salary���������������������������������������������������������������������������������������������������� 91
Real Estate Industry����������������������������������������������������������������������������������������������� 92
■
■Chapter 3: Time Series����������������������������������������������������������������� 95
Case Study: Predicting Daily Adjusted Closing Rate of Yahoo��������������� 95
Feature Exploration������������������������������������������������������������������������������� 97
Time Series Modeling��������������������������������������������������������������������������������������������� 98
viii
■ Contents
■
■Chapter 4: Clustering������������������������������������������������������������������ 129
Case Study: Determination of Short Tail Keywords for Marketing������� 129
Features’ Exploration�������������������������������������������������������������������������� 131
Supervised vs. Unsupervised Learning����������������������������������������������� 133
Supervised Learning��������������������������������������������������������������������������������������������� 133
Unsupervised Learning����������������������������������������������������������������������������������������� 133
Clustering�������������������������������������������������������������������������������������������� 134
Data Transformation for Modeling������������������������������������������������������� 135
Metrics of Evaluating Clustering Models�������������������������������������������������������������� 137
■
■Chapter 5: Classification������������������������������������������������������������ 161
Case Study: Ohio Clinic—Meeting Supply and Demand��������������������� 161
Features’ Exploration�������������������������������������������������������������������������� 164
Performing Data Wrangling����������������������������������������������������������������� 168
Performing Exploratory Data Analysis������������������������������������������������� 172
Features’ Generation��������������������������������������������������������������������������� 178
ix
■ Contents
Classification��������������������������������������������������������������������������������������� 180
Model Evaluation Techniques������������������������������������������������������������������������������� 181
Ensuring Cross-Validation by Splitting the Dataset���������������������������������������������� 184
Decision Tree Classification���������������������������������������������������������������������������������� 185
■
■Appendix A: Chart types and when to use them������������������������� 197
Pie chart���������������������������������������������������������������������������������������������� 197
Bar graph�������������������������������������������������������������������������������������������� 198
Histogram�������������������������������������������������������������������������������������������� 198
Stem and Leaf plot������������������������������������������������������������������������������ 199
Box plot����������������������������������������������������������������������������������������������� 199
Index���������������������������������������������������������������������������������������������� 201
x
About the Author
xi
About the Technical
Reviewer
xiii
Acknowledgments
I would like to thank my parents and lovely wife for their continuous support throughout
this enlightening journey.
xv
Introduction
This volume embraces machine learning approaches and Python to enable automatic
rendering of rich insights and solutions to business problems. The book uses a
hands-on case study-based approach to crack real-world applications where machine
learning concepts can provide a best fit. These smarter machines will enable your
business processes to achieve efficiencies in minimal time and resources.
Python Machine Learning Case Studies walks you through a step-by-step approach to
improve business processes and help you discover the pivotal points that frame corporate
strategies. You will read about machine learning techniques that can provide support to
your products and services. The book also highlights the pros and cons of each of these
machine learning concepts to help you decide which one best suits your needs.
By taking a step-by-step approach to coding you will be able to understand the
rationale behind model selection within the machine learning process. The book is
equipped with practical examples and code snippets to ensure that you understand the
data science approach for solving real-world problems.
Python Machine Leaarning Case Studies acts as an enabler for people from both
technical and non-technical backgrounds to apply machine learning techniques to
real-world problems. Each chapter starts with a case study that has a well-defined
business problem. The chapters then proceed by incorporating storylines, and code
snippets to decide on the most optimal solution. Exercises are laid out throughout the
chapters to enable the hands-on practice of the concepts learned. Each chapter ends
with a highlight of real-world applications to which the concepts learned can be applied.
Following is a brief overview of the contents covered in each of the five chapters:
Chapter 1 covers the concepts of statistics and probability.
Chapter 2 talks about regression techniques and methods to fine-tune the model.
Chapter 3 exposes readers to time series models and covers the property of
stationary in detail.
Chapter 4 uses clustering as an aid to segment the data for marketing purposes.
Chapter 5 talks about classification models and evaluation metrics to gauge the
goodness of these models.
xvii
CHAPTER 1
The purpose of this chapter is to instill in you the basic concepts of traditional statistics
and probability. Certainly many of you might be wondering what it has to do with
machine learning. Well, in order to apply a best fit model to your data, the most important
prerequisite is for you to understand the data in the first place. This will enable you to find
out distributions within data, measure the goodness of data, and run some basic tests
to understand if some form of relationship exists between dependant and independent
variables. Let’s dive in.
■■Note This book incorporates Python 2.7.11 as the de facto standard for coding
examples. Moreover, you are required to have it installed it for the Exercises as well.
So why do I prefer Python 2.7.11 over Python 3x? Following are some of the reasons:
• Third-party library support for Python 2x is relatively better than
support for Python 3x. This means that there are a considerable
number of libraries in Python 2x that lack support in Python 3x.
• Some current Linux distributions and macOS provide Python 2x
by default. The objective is to let readers, regardless of their OS
version, apply the code examples on their systems, and thus this
is the choice to go forward with.
• The above-mentioned facts are the reason why companies prefer
to work with Python 2x or why they decide not to migrate their
code base from Python 2x to Python 3x.
The cycle sharing scheme provides means for the people of the city to commute
using a convenient, cheap, and green transportation alternative. The service has 500
bikes at 50 stations across Seattle. Each of the stations has a dock locking system (where
all bikes are parked); kiosks (so customers can get a membership key or pay for a trip);
and a helmet rental service. A person can choose between purchasing a membership
key or short-term pass. A membership key entitles an annual membership, and the key
can be obtained from a kiosk. Advantages for members include quick retrieval of bikes
and unlimited 45-minute rentals. Short-term passes offer access to bikes for a 24-hour
or 3-day time interval. Riders can avail and return the bikes at any of the 50 stations
citywide.
Jason started this service in May 2014 and since then had been focusing on
increasing the number of bikes as well as docking stations in order to increase
convenience and accessibility for his customers. Despite this expansion, customer
retention remained an issue. As Jason recalled, “We had planned to put in the investment
for a year to lay out the infrastructure necessary for the customers to start using it. We
had a strategy to make sure that the retention levels remain high to make this model self-
sustainable. However, it worked otherwise (i.e., the customer base didn’t catch up with
the rate of the infrastructure expansion).”
A private service would have had three alternatives to curb this problem: get
sponsors on board, increase service charges, or expand the pool of customers. Price hikes
were not an option for Jason as this was a publicly sponsored initiative with the goal of
providing affordable transportation to all. As for increasing the customer base, they had
to decide upon a marketing channel that guarantees broad reach on low cost incurred.
Nancy, a marketer who had worked in the corporate sector for ten years, and Eric, a
data analyst, were explicitly hired to find a way to make things work around this problem.
The advantage on their side was that they were provided with the dataset of transaction
history and thus they didn’t had to go through the hassle of conducting marketing
research to gather data.
Nancy realized that attracting recurring customers on a minimal budget
required understanding the customers in the first place (i.e., persona). As she stated,
“Understanding the persona of your brand is essential, as it helps you reach a targeted
audience which is likely to convert at a higher probability. Moreover, this also helps in
reaching out to sponsors who target a similar persona. This two-fold approach can make
our bottom line positive.”
As Nancy and Eric contemplated the problem at hand, they had questions like the
following: Which attribute correlates the best with trip duration and number of trips?
Which age generation adapts the most to our service?
Following is the data dictionary of the Trips dataset that was provided to Nancy and
Eric:
2
Chapter 1 ■ Statistics and Probability
Table 1-1. Data Dictionary for the Trips Data from Cycles Share Dataset
Exercises for this chapter required Eric to install the packages shown in Listing 1-1.
He preferred to import all of them upfront to avoid bottlenecks while implementing the
code snippets on your local machine.
However, for Eric to import these packages in his code, he needed to install them in
the first place. He did so as follows:
1. Opened terminal/shell
2. Navigated to his code directory using terminal/shell
3. Installed pip:
python get-pip.py
import random
import datetime
import pandas as pd
import matplotlib.pyplot as plt
import statistics
3
Chapter 1 ■ Statistics and Probability
import numpy as np
import scipy
from scipy import stats
import seaborn
Feature Exploration
Eric started off by loading the data into memory (see Listing 1-2).
Nancy was curious to know how big the data was and what it looked like. Hence, Eric
wrote the code in Listing 1-3 to print some initial observations of the dataset to get a feel
of what it contains.
Listing 1-3. Printing Size of the Dataset and Printing First Few Rows
print len(data)
data.head()
Output
236065
4
Chapter 1 ■ Statistics and Probability
Occidental Park/
10/13/2014 10/13/2014
431 SEA00298 985.935 2nd Ave & Spring St Occidental Ave S
10:31 10:48
& S Washing...
Occidental Park/
10/13/2014 10/13/2014
432 SEA00195 926.375 2nd Ave & Spring St Occidental Ave S
10:32 10:48
& S Washing...
Occidental Park/
10/13/2014 10/13/2014
433 SEA00486 883.831 2nd Ave & Spring St Occidental Ave S
10:33 10:48
& S Washing...
Occidental Park/
10/13/2014 10/13/2014
434 SEA00333 865.937 2nd Ave & Spring St Occidental Ave S
10:34 10:48
& S Washing...
Occidental Park/
10/13/2014 10/13/2014
435 SEA00202 923.923 2nd Ave & Spring St Occidental Ave S
10:34 10:49
& S Washing...
5
Chapter 1 ■ Statistics and Probability
After looking at Table 1-2 and Table 1-3 Nancy noticed that tripduration is
represented in seconds. Moreover, the unique identifiers for bike, from_station, and
to_station are in the form of strings, contrary to those for trip identifier which are in
the form of integers.
Types of variables
Nancy decided to go an extra mile and allocated data type to each feature in the dataset.
After looking at the feature classification in Table 1-4 Eric noticed that Nancy had
correctly identified the data types and thus it seemed to be an easy job for him to explain
what variable types mean. As Eric recalled to have explained the following:
Continuous/Quantitative Variables
A continuous variable can have an infinite number of values within a given range. Unlike
discrete variables, they are not countable. Before exploring the types of continuous
variables, let’s understand what is meant by a true zero point.
6
Chapter 1 ■ Statistics and Probability
Interval Variables
Interval variables exist around data which is continuous in nature and has a numerical
value. Take, for example, the temperature of a neighborhood measured on a daily basis.
Difference between intervals remains constant, such that the difference between 70
Celsius and 50 Celsius is the same as the difference between 80 Celsius and 100 Celsius.
We can compute the mean and median of interval variables however they don’t have a
true zero point.
Ratio Variables
Properties of interval variables are very similar to those of ratio variables with the
difference that in ratio variables a 0 indicates the absence of that measurement. Take,
for example, distance covered by cars from a certain neighborhood. Temperature in
Celsius is an interval variable, so having a value of 0 Celsius does not mean absence of
temperature. However, notice that a value of 0 KM will depict no distance covered by the
car and thus is considered as a ratio variable. Moreover, as evident from the name, ratios
of measurements can be used as well such that a distance covered of 50 KM is twice the
distance of 25 KM covered by a car.
Discrete Variables
A discrete variable will have finite set of values within a given range. Unlike continuous
variables those are countable. Let’s look at some examples of discrete variables which are
categorical in nature.
Ordinal Variables
Ordinal variables have values that are in an order from lowest to highest or vice versa.
These levels within ordinal variables can have unequal spacing between them. Take, for
example, the following levels:
1. Primary school
2. High school
3. College
4. University
7
Chapter 1 ■ Statistics and Probability
The difference between primary school and high school in years is definitely not
equal to the difference between high school and college. If these differences were
constant, then this variable would have also qualified as an interval variable.
Nominal Variables
Nominal variables are categorical variables with no intrinsic order; however, constant
differences between the levels exist. Examples of nominal variables can be gender, month
of the year, cars released by a manufacturer, and so on. In the case of month of year, each
month is a different level.
Dichotomous Variables
Dichotomous variables are nominal variables which have only two categories or levels.
Examples include
• Age: under 24 years, above 24 years
• Gender: male, female
Lurking Variable
A lurking variable is not among exploratory (i.e., independent) or response
(i.e., dependent) variables and yet may influence the interpretations of relationship
among these variables. For example, if we want to predict whether or not an applicant
will get admission in a college on the basis of his/her gender. A possible lurking variable
in this case can be the name of the department the applicant is seeking admission to.
Demographic Variable
Demography (from the Greek word meaning “description of people”) is the study of
human populations. The discipline examines size and composition of populations as well
as the movement of people from locale to locale. Demographers also analyze the effects
of population growth and its control. A demographic variable is a variable that is collected
by researchers to describe the nature and distribution of the sample used with inferential
statistics. Within applied statistics and research, these are variables such as age, gender,
ethnicity, socioeconomic measures, and group membership.
8
Chapter 1 ■ Statistics and Probability
of cycles can be ensured. In that case, what is your dependent variable? Definitely
tripduration. And what are the independent variables? Well, these variables will comprise
of the features which we believe influence the dependent variable (e.g., usertype, gender,
and time and date of the day).
Eric asked Nancy to classify the features in the variable types he had just explained.
Nancy now had a clear idea of the variable types within machine learning, and also
which of the features qualify for which of those variable types (see Table 1-5). However
despite of looking at the initial observations of each of these features (see Table 1-2) she
couldn’t deduce the depth and breadth of information that each of those tables contains.
She mentioned this to Eric, and Eric, being a data analytics guru, had an answer: perform
univariate analysis on features within the dataset.
Univariate Analysis
Univariate comes from the word “uni” meaning one. This is the analysis performed on a
single variable and thus does not account for any sort of relationship among exploratory
variables.
Eric decided to perform univariate analysis on the dataset to better understand the
features in isolation (see Listing 1-4).
Output
9
Chapter 1 ■ Statistics and Probability
Eric knew that Nancy would have a hard time understanding the code so he decided
to explain the ones that he felt were complex in nature. In regard to the code in Listing
1-4, Eric explained the following:
Eric’s analysis presented two insights. One is that the data ranges from October 2014
up till September 2016 (i.e., three years of data). Moreover, it seems like the cycle sharing
service is usually operational beyond the standard 9 to 5 business hours.
Nancy believed that short-term pass holders would avail more trips than their
counterparts. She believed that most people would use the service on a daily basis rather
than purchasing the long term membership. Eric thought otherwise; he believed that
new users would be short-term pass holders however once they try out the service and
become satisfied would ultimately avail the membership to receive the perks and benefits
offered. He also believed that people tend to give more weight to services they have paid
for, and they make sure to get the maximum out of each buck spent. Thus, Eric decided
to plot a bar graph of trip frequencies by user type to validate his viewpoint (see Listing 1-5).
But before doing so he made a brief document of the commonly used charts and
situations for which they are a best fit to (see Appendix A for a copy). This chart gave
Nancy his perspective for choosing a bar graph for the current situation.
10
Chapter 1 ■ Statistics and Probability
140000
120000
100000
80000
60000
40000
20000
usertype
Nancy didn’t understand the code snippet in Listing 1-5. She was confused by the
functionality of groupby and size methods. She recalled asking Eric the following: “I can
understand that groupby groups the data by a given field, that is, usertype, in the current
situation. But what do we mean by size? Is it the same as count, that is, counts trips falling
within each of the grouped usertypes?”
Eric was surprised by Nancy’s deductions and he deemed them to be correct.
However, the bar graph presented insights (see Figure 1-1) in favor of Eric’s view as the
members tend to avail more trips than their counterparts.
Nancy had recently read an article that talked about the gender gap among
people who prefer riding bicycles. The article mentioned a cycle sharing scheme in UK
where 77% of the people who availed the service were men. She wasn’t sure if similar
phenomenon exists for people using the service in United States. Hence Eric came up
with the code snippet in Listing 1-6 to answer the question at hand.
11
Chapter 1 ■ Statistics and Probability
Distribution of genders
120000
100000
80000
60000
40000
20000
0
Male
Other
Female
gender
Figure 1-2 revealed that the gender gap resonates in states as well. Males seem to
dominate the trips taken as part of the program.
Nancy, being a marketing guru, was content with the analysis done so far. However
she wanted to know more about her target customers to whom to company’s marketing
message will be targetted to. Thus Eric decided to come up with the distribution of
birth years by writing the code in Listing 1-7. He believed this would help the Nancy
understand the age groups that are most likely to ride a cycle or the ones that are more
prone to avail the service.
12
Chapter 1 ■ Statistics and Probability
12000
10000
8000
6000
4000
2000
0
1931.0
1936.0
1939.0
1942.0
1943.0
1944.0
1945.0
1946.0
1947.0
1948.0
1949.0
1950.0
1951.0
1952.0
1953.0
1954.0
1955.0
1956.0
1957.0
1958.0
1959.0
1960.0
1961.0
1962.0
1963.0
1964.0
1965.0
1966.0
1967.0
1968.0
1969.0
1970.0
1971.0
1972.0
1973.0
1974.0
1975.0
1976.0
1977.0
1978.0
1979.0
1980.0
1981.0
1982.0
1983.0
1984.0
1985.0
1986.0
1987.0
1988.0
1989.0
1990.0
1991.0
1992.0
1993.0
1994.0
1995.0
1996.0
1997.0
1998.0
1999.0
birthyear
Figure 1-3 provided a very interesting illustration. Majority of the people who had
subscribed to this program belong to Generation Y (i.e., born in the early 1980s to mid
to late 1990s, also known as millennials). Nancy had recently read the reports published
by Elite Daily and CrowdTwist which said that millennials are the most loyal generation
to their favorite brands. One reason for this is their willingness to share thoughts and
opinions on products/services. These opinions thus form a huge corpus of experiences—
enough information for the millenials to make a conscious decision, a decision they will
remain loyal to for a long period. Hence Nancy was convinced that most millennials
would be members rather than short-term pass holders. Eric decided to populate a bar
graph to see if Nancy’s deduction holds true.
100000
80000
60000
40000
20000
0
Member
usertype
After looking at Figure 1-4 Eric was surprised to see that Nancy’s deduction appeared
to be valid, and Nancy made a note to make sure that the brand engaged millennials as
part of the marketing plan.
Eric knew that more insights can pop up when more than one feature is used as part
of the analysis. Hence, he decided to give Nancy a sneak peek at multivariate analysis
before moving forward with more insights.
Multivariate Analysis
Multivariate analysis refers to incorporation of multiple exploratory variables to
understand the behavior of a response variable. This seems to be the most feasible
and realistic approach considering the fact that entities within this world are usually
interconnected. Thus the variability in response variable might be affected by the
variability in the interconnected exploratory variables.
Nancy believed males would dominate females in terms of the trips completed. The
graph in Figure 1-2, which showed that males had completed far more trips than any
other gender types, made her embrace this viewpoint. Eric thought that the best approach
to validate this viewpoint was a stacked bar graph (i.e., a bar graph for birth year, but each
bar having two colors, one for each gender) (see Figure 1-5).
6000
4000
2000
0
1931.0
1936.0
1939.0
1942.0
1943.0
1944.0
1945.0
1946.0
1947.0
1948.0
1949.0
1950.0
1951.0
1952.0
1953.0
1954.0
1955.0
1956.0
1957.0
1958.0
1959.0
1960.0
1961.0
1962.0
1963.0
1964.0
1965.0
1966.0
1967.0
1968.0
1969.0
1970.0
1971.0
1972.0
1973.0
1974.0
1975.0
1976.0
1977.0
1978.0
1979.0
1980.0
1981.0
1982.0
1983.0
1984.0
1985.0
1986.0
1987.0
1988.0
1989.0
1990.0
1991.0
1992.0
1993.0
1994.0
1995.0
1996.0
1997.0
1998.0
1999.0
birthyear
Figure 1-5. Bar graph signifying the distribution of birth years by gender type
14
Chapter 1 ■ Statistics and Probability
The code snippet in Listing 1-9 brought up some new aspects not previously
highlighted.
It seemed as if males were dominating the distribution. It made sense as well. No?
Well, it did; as seen earlier, that majority of the trips were availed by males, hence this
skewed the distribution in favor of males. However, subscribers born in 1947 were all
females. Moreover, those born in 1964 and 1994 were dominated by females as well. Thus
Nancy’s hypothesis and reasoning did hold true.
The analysis in Listing 1-4 had revealed that all millennials are members. Nancy was
curious to see what the distribution of user type was for the other age generations. Is it
that the majority of people in the other age generations were short-term pass holders?
Hence Eric brought a stacked bar graph into the application yet again (see Figure 1-6).
birthyear
Figure 1-6. Bar graph signifying the distribution of birth years by user types
15
Exploring the Variety of Random
Documents with Different Content
doen.—Tegelijk reikte hij Sokrates den beker. En deze nam hem aan
en zeer blijmoedig, o Echekrates, zonder eenigszins te beven of van
kleur of gelaatsuitdrukking te veranderen, maar den mensch met
strak-open oogen aanziende, zooals hij dat gewoon was, vroeg hij:
Wat denkt gij van dezen drank, is het geoorloofd daarvan aan
iemand te plengen of niet?—Zooveel, zeî hij, o Sokrates, mengen wij
C als wij meenen dat voldoende is tot drinken.—Juist, zeide hij.
Maar allicht is het geoorloofd, en ook passend, tot de goden te
bidden, dat de verhuizing van hier eene gelukkige moge zijn. Dit doe
ik dan ook, en moge het zoo geschieden. Dadelijk na deze woorden
bracht hij den beker aan zijn mond en dronk hem vlug en rustig leêg.
En de meesten van ons waren zoolang vrij-wel in-staat onze tranen
in te houden, maar toen wij zagen dat hij dronk en gedronken had,
niet meer, maar bij mij vloeiden de tranen met geweld in stroomen,
zoodat ik mij omhulde en mij-zelven beweende; want over hem
D weende ik niet, maar om mijn eigen lot, van welk een vriend ik
beroofd was. Kritoon was nog eer dan ik uit den kring opgestaan,
omdat hij niet in-staat was zijn tranen te bedwingen. En Apollodoros,
die ook al vroeger niet ophield te weenen, brak toen in luide
jammerklachten los en ontstelde elk der aanwezigen, behalve
Sokrates zelven. Doch deze zeide: Wat-voor dingen doet gij nu, mijn
bewonderenswaardigen! Ik echter heb boven-al om die reden de
vrouwen weggezonden, opdat zij met zulke dingen niet storen
E zouden. Want ik heb gehoord, dat men in heilige stilte behoort te
sterven. Doch houdt u rustig en kloek!—En wij op het hooren
hiervan, schaamden ons en lieten af van weenen. Hij wandelde eerst
rond, en nadat, zooals hij zeide, zijn beenen zwaar werden, legde hij
zich achterover neder. Want zoo verzocht hem de slaaf. En deze,
dezelfde die hem het gif had toegediend, onderzocht tegelijk van-tijd-
tot-tijd zijn voeten en beenen, door die te betasten, en daarop kneep
hij hem sterk in den éenen voet en vraagde of hij het voelde.
Sokrates zeide van-niet. En daarna kneep hij in de scheenbeenen,
118 en zoo omhooggaande, liet hij ons zien, dat hij langzamerhand
koud en stijf werd. Ook Sokrates zelf betastte zich en zeide, dat,
wanneer het zijn hart zoû bereiken, hij dan zoû heengaan. Reeds
begonnen ongeveer de deelen van ’t onderlijf koud te worden, toen
hij zijn gelaat onthulde—want hij had zich omhuld—, en het laatste
woord zeide, dat hij gesproken heeft: o Kritoon, wij zijn Asklepios
een haan schuldig. Geef hem dien en vergeet het niet.—Dat zal
geschieden, zeide Kritoon. Maar bedenk of gij nog iets anders te
zeggen hebt.—Op deze vraag van Kritoon antwoordde hij niet meer,
maar kort daarop kreeg hij een lichten schok, en de mensch
onthulde hem, en zijn oogen stonden star. Toen Kritoon dat zag,
drukte hij hem mond en oogen toe.
Dit was het einde voor ons, o Echekrates, van onzen vriend, een
man, zooals wij zouden zeggen, van zijn tijdgenooten die wij leerden
kennen, den besten, en ook overigens den wijsten en
rechtvaardigsten.
AANTEEKENINGEN
60D. E u e n o s . Sofist en dichter, afkomstig van
het eiland Paros. Ook elders vermeldt
Platoon hem (Ap. 20B, Phaidros 267A), met
dezelfde goedmoedige ironie als hier.
89C. A r g e i e r s . Toen de Argeiers in 550 hun
zuidelijk grensgebied met de stad Thureai
aan de Lakedaimoniërs verloren, verboden
zij bij wet hun mannen lang haar, en hun
vrouwen gouden sieraden te dragen
zoolang die stad niet heroverd zoû zijn. Zie
Herodotos I 82.
I o l a o s . Neef van Herakles en diens
wagenmenner en trouwe metgezel. Toen
Herakles bij zijn strijd met de Hydra door
een reusachtige zeekrabbe werd
aangevallen, riep hij de hulp van Iolaos in.
Zie Platoons Euthydemos 297C.
90C. E u r i p o s . De om haar onstuimigheid
bekende enge zeestraat tusschen Boiotia
en het eiland Euboia op de hoogte der
steden Chalkis en Aulis.
95A. H a r m o n i a d e T h e b a a n s c h e .
Gemalin van Kadmos den Phoinikiër, den
mythischen stichter van Thebai.
97C. A n a x a g o r a s . Uit Klazomenai in Lydia.
500-428. Beroemd leerling der Ionische
natuurphilosofen. Hij vestigde zich te
Athenai en werd bevriend met den kring van
Perikles. Om zijn atheïstische stellingen
werd hij, evenals later Sokrates, van
„asebeia” beschuldigd en ontkwam alleen
door Perikles’ invloed aan de doodstraf. Hij
stierf te Lampsakos. Van zijn hoofdwerk
„Over de natuur” bestaan nog slechts
fragmenten.
108D. G l a u k o s . Waarschijnlijk wordt gedoeld op
Glaukos van Chios, den uitvinder van het
soldeeren van ijzer. Zie Herodotos I 25.
118A. Wij zijn A s k l e p i o s een haan schuldig.
Het gewone offer aan den god der
geneeskunde, wanneer men van een ziekte
is hersteld.
Colofon
Duidelijke zetfouten in de originele tekst zijn verbeterd. Wisselende spelling is
gecorrigeerd. Daarnaast is aangepast:
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com