
Data Science Using Python and R
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
More details
Other editions
Additional editions


Persons
CHANTAL D. LAROSE, PHD, is an Assistant Professor of Statistics & Data Science at Eastern Connecticut State University (ECSU). She has co-authored three books on data science and predictive analytics and helped develop data science programs at ECSU and SUNY New Paltz. Her PhD dissertation, Model-Based Clustering of Incomplete Data, tackles the persistent problem of trying to do data science with incomplete data.
DANIEL T. LAROSE, PHD, is a Professor of Data Science and Statistics and Director of the Data Science programs at Central Connecticut State University. He has published many books on data science, data mining, predictive analytics, and statistics. His consulting clients include The Economist magazine, Forbes Magazine, the CIT Group, and Microsoft.
Content
Preface xi
About the Authors xv
Acknowledgements xvii
Chapter 1 Introduction to Data Science 1
1.1 Why Data Science? 1
1.2 What is Data Science? 1
1.3 The Data Science Methodology 2
1.4 Data Science Tasks 5
1.4.1 Description 6
1.4.2 Estimation 6
1.4.3 Classification 6
1.4.4 Clustering 7
1.4.5 Prediction 7
1.4.6 Association 7
Exercises 8
Chapter 2 The Basics of Python and R 9
2.1 Downloading Python 9
2.2 Basics of Coding in Python 9
2.2.1 Using Comments in Python 9
2.2.2 Executing Commands in Python 10
2.2.3 Importing Packages in Python 11
2.2.4 Getting Data into Python 12
2.2.5 Saving Output in Python 13
2.2.6 Accessing Records and Variables in Python 14
2.2.7 Setting Up Graphics in Python 15
2.3 Downloading R and RStudio 17
2.4 Basics of Coding in R 19
2.4.1 Using Comments in R 19
2.4.2 Executing Commands in R 20
2.4.3 Importing Packages in R 20
2.4.4 Getting Data into R 21
2.4.5 Saving Output in R 23
2.4.6 Accessing Records and Variables in R 24
References 26
Exercises 26
Chapter 3 Data Preparation 29
3.1 The Bank Marketing Data Set 29
3.2 The Problem Understanding Phase 29
3.2.1 Clearly Enunciate the Project Objectives 29
3.2.2 Translate These Objectives into a Data Science Problem 30
3.3 Data Preparation Phase 31
3.4 Adding an Index Field 31
3.4.1 How to Add an Index Field Using Python 31
3.4.2 How to Add an Index Field Using R 32
3.5 Changing Misleading Field Values 33
3.5.1 How to Change Misleading Field Values Using Python 34
3.5.2 How to Change Misleading Field Values Using R 34
3.6 Reexpression of Categorical Data as Numeric 36
3.6.1 How to Reexpress Categorical Field Values Using Python 36
3.6.2 How to Reexpress Categorical Field Values Using R 38
3.7 Standardizing the Numeric Fields 39
3.7.1 How to Standardize Numeric Fields Using Python 40
3.7.2 How to Standardize Numeric Fields Using R 40
3.8 Identifying Outliers 40
3.8.1 How to Identify Outliers Using Python 41
3.8.2 How to Identify Outliers Using R 42
References 43
Exercises 44
Chapter 4 Exploratory Data Analysis 47
4.1 EDA Versus HT 47
4.2 Bar Graphs with Response Overlay 47
4.2.1 How to Construct a Bar Graph with Overlay Using Python 49
4.2.2 How to Construct a Bar Graph with Overlay Using R 50
4.3 Contingency Tables 51
4.3.1 How to Construct Contingency Tables Using Python 52
4.3.2 How to Construct Contingency Tables Using R 53
4.4 Histograms with Response Overlay 53
4.4.1 How to Construct Histograms with Overlay Using Python 55
4.4.2 How to Construct Histograms with Overlay Using R 58
4.5 Binning Based on Predictive Value 58
4.5.1 How to Perform Binning Based on Predictive Value Using Python 59
4.5.2 How to Perform Binning Based on Predictive Value Using R 62
References 63
Exercises 63
Chapter 5 Preparing to Model the Data 69
5.1 The Story So Far 69
5.2 Partitioning the Data 69
5.2.1 How to Partition the Data in Python 70
5.2.2 How to Partition the Data in R 71
5.3 Validating your Partition 72
5.4 Balancing the Training Data Set 73
5.4.1 How to Balance the Training Data Set in Python 74
5.4.2 How to Balance the Training Data Set in R 75
5.5 Establishing Baseline Model Performance 77
References 78
Exercises 78
Chapter 6 Decision Trees 81
6.1 Introduction to Decision Trees 81
6.2 Classification and Regression Trees 83
6.2.1 How to Build CART Decision Trees Using Python 84
6.2.2 How to Build CART Decision Trees Using R 86
6.3 The C5.0 Algorithm for Building Decision Trees 88
6.3.1 How to Build C5.0 Decision Trees Using Python 89
6.3.2 How to Build C5.0 Decision Trees Using R 90
6.4 Random Forests 91
6.4.1 How to Build Random Forests in Python 92
6.4.2 How to Build Random Forests in R 92
References 93
Exercises 93
Chapter 7 Model Evaluation 97
7.1 Introduction to Model Evaluation 97
7.2 Classification Evaluation Measures 97
7.3 Sensitivity and Specificity 99
7.4 Precision, Recall, and Fß Scores 99
7.5 Method for Model Evaluation 100
7.6 An Application of Model Evaluation 100
7.6.1 How to Perform Model Evaluation Using R 103
7.7 Accounting for Unequal Error Costs 104
7.7.1 Accounting for Unequal Error Costs Using R 105
7.8 Comparing Models with and without Unequal Error Costs 106
7.9 Data-Driven Error Costs 107
Exercises 109
Chapter 8 Naïve Bayes Classification 113
8.1 Introduction to Naive Bayes 113
8.2 Bayes Theorem 113
8.3 Maximum a Posteriori Hypothesis 114
8.4 Class Conditional Independence 114
8.5 Application of Naive Bayes Classification 115
8.5.1 Naive Bayes in Python 121
8.5.2 Naive Bayes in R 123
References 125
Exercises 126
Chapter 9 Neural Networks 129
9.1 Introduction to Neural Networks 129
9.2 The Neural Network Structure 129
9.3 Connection Weights and the Combination Function 131
9.4 The Sigmoid Activation Function 133
9.5 Backpropagation 134
9.6 An Application of a Neural Network Model 134
9.7 Interpreting the Weights in a Neural Network Model 136
9.8 How to Use Neural Networks in R 137
References 138
Exercises 138
Chapter 10 Clustering 141
10.1 What is Clustering? 141
10.2 Introduction to the K-Means Clustering Algorithm 142
10.3 An Application of K-Means Clustering 143
10.4 Cluster Validation 144
10.5 How to Perform K-Means Clustering Using Python 145
10.6 How to Perform K-Means Clustering Using R 147
Exercises 149
Chapter 11 Regression Modeling 151
11.1 The Estimation Task 151
11.2 Descriptive Regression Modeling 151
11.3 An Application of Multiple Regression Modeling 152
11.4 How to Perform Multiple Regression Modeling Using Python 154
11.5 How to Perform Multiple Regression Modeling Using R 156
11.6 Model Evaluation for Estimation 157
11.6.1 How to Perform Estimation Model Evaluation Using Python 159
11.6.2 How to Perform Estimation Model Evaluation Using R 160
11.7 Stepwise Regression 161
11.7.1 How to Perform Stepwise Regression Using R 162
11.8 Baseline Models for Regression 162
References 163
Exercises 164
Chapter 12 Dimension Reduction 167
12.1 The Need for Dimension Reduction 167
12.2 Multicollinearity 168
12.3 Identifying Multicollinearity Using Variance Inflation Factors 171
12.3.1 How to Identify Multicollinearity Using Python 172
12.3.2 How to Identify Multicollinearity in R 173
12.4 Principal Components Analysis 175
12.5 An Application of Principal Components Analysis 175
12.6 How Many Components Should We Extract? 176
12.6.1 The Eigenvalue Criterion 176
12.6.2 The Proportion of Variance Explained Criterion 177
12.7 Performing Pca with K = 4 178
12.8 Validation of the Principal Components 178
12.9 How to Perform Principal Components Analysis Using Python 179
12.10 How to Perform Principal Components Analysis Using R 181
12.11 When is Multicollinearity Not a Problem? 183
References 184
Exercises 184
Chapter 13 Generalized Linear Models 187
13.1 An Overview of General Linear Models 187
13.2 Linear Regression as a General Linear Model 188
13.3 Logistic Regression as a General Linear Model 188
13.4 An Application of Logistic Regression Modeling 189
13.4.1 How to Perform Logistic Regression Using Python 190
13.4.2 How to Perform Logistic Regression Using R 191
13.5 Poisson Regression 192
13.6 An Application of Poisson Regression Modeling 192
13.6.1 How to Perform Poisson Regression Using Python 193
13.6.2 How to Perform Poisson Regression Using R 194
Reference 195
Exercises 195
Chapter 14 Association Rules 199
14.1 Introduction to Association Rules 199
14.2 A Simple Example of Association Rule Mining 200
14.3 Support, Confidence, and Lift 200
14.4 Mining Association Rules 202
14.4.1 How to Mine Association Rules Using R 203
14.5 Confirming Our Metrics 207
14.6 The Confidence Difference Criterion 208
14.6.1 How to Apply the Confidence Difference Criterion Using R 208
14.7 The Confidence Quotient Criterion 209
14.7.1 How to Apply the Confidence Quotient Criterion Using R 210
References 211
Exercises 211
Appendix Data Summarization and Visualization 215
Part 1: Summarization 1: Building Blocks of Data Analysis 215
Part 2: Visualization: Graphs and Tables for Summarizing and Organizing Data 217
Part 3: Summarization 2: Measures of Center, Variability, and Position 222
Part 4: Summarization and Visualization of Bivariate Elationships 225
Index 231
Chapter 1
INTRODUCTION TO DATA SCIENCE
1.1 WHY DATA SCIENCE?
Data science is one of the fastest growing fields in the world, with 6.5 times as many job openings in 2017 as compared to 2012.1 Demand for data scientists is expected to increase in the future. For example, in May 2017, IBM projected that yearly demand for "data scientist, data developers, and data engineers will reach nearly 700,000 openings by 2020."2 http://InfoWorld.com reported that the #1 "reason why data scientist remains the top job in America"3 is that "there is a shortage of talent." That is why we wrote this book, to help alleviate the shortage of qualified data scientists.
1.2 WHAT IS DATA SCIENCE?
Simply put, data science is the systematic analysis of data within a scientific framework. That is, data science is the
- adaptive, iterative, and phased approach to the analysis of data,
- performed within a systematic framework,
- that uncovers optimal models,
- by assessing and accounting for the true costs of prediction errors.
Data science combines the
- data-driven approach of statistical data analysis,
- the computational power and programming acumen of computer science, and
- domain-specific business intelligence,
in order to uncover actionable and profitable nuggets of information from large databases.
In other words, data science allows us to extract actionable knowledge from under-utilized databases. Thus, data warehouses that have been gathering dust can now be leveraged to uncover hidden profit and enhance the bottom line. Data science lets people leverage large amounts of data and computing power to tackle complex questions. Patterns can arise out of data which could not have been uncovered otherwise. These discoveries can lead to powerful results, such as more effective treatment of medical patients or more profits for a company.
1.3 THE DATA SCIENCE METHODOLOGY
We follow the Data Science Methodology (DSM),4 which helps the analyst keep track of which phase of the analysis he or she is performing. Figure 1.1 illustrates the adaptive and iterative nature of the DSM, using the following phases:
- Problem Understanding Phase. How often have teams worked hard to solve a problem, only to find out later that they solved the wrong problem? Further, how often have the marketing team and the analytics team not been on the same page? This phase attempts to avoid these pitfalls.
- First, clearly enunciate the project objectives,
- Then, translate these objectives into the formulation of a problem that can be solved using data science.
- Data Preparation Phase. Raw data from data repositories is seldom ready for the algorithms straight out of the box. Instead, it needs to be cleaned or "prepared for analysis." When analysts first examine the data, they uncover the inevitable problems with data quality that always seem to occur. It is in this phase that we fix these problems. Data cleaning/preparation is probably the most labor-intensive phase of the entire data science process. The following is a non-exhaustive list of the issues that await the data preparer.
- Identifying outliers and determining what to do about them.
- Transforming and standardizing the data.
- Reclassifying categorical variables.
- Binning numerical variables.
- Adding an index field.
The data preparation phase is covered in Chapter 3.
- Exploratory Data Analysis Phase. Now that your data are nice and clean, we can begin to explore the data, and learn some basic information. Graphical exploration is the focus here. Now is not the time for complex algorithms. Rather, we use simple exploratory methods to help us gain some preliminary insights. You might find that you can learn quite a bit just by using these simple methods. Here are some of the ways we can do this.
- Exploring the univariate relationships between predictors and the target variable.
- Exploring multivariate relationships among the variables.
- Binning based on predictive value to enhance our models.
- Deriving new variables based on a combination of existing variables.
We cover the exploratory data analysis phase in Chapter 4.
- Setup Phase. At this point we are nearly ready to begin modeling the data. We just need to take care of a few important chores first, such as the following:
- Cross-validation, either twofold or n-fold. This is necessary to avoid data dredging. In addition, your data partitions need to be evaluated to ensure that they are indeed random.
- Balancing the data. This enhances the ability of certain algorithms to uncover relationships in the data.
- Establishing baseline performance. Suppose we told you we had a model that could predict correctly whether a credit card transaction was fraudulent or not 99% of the time. Impressed? You should not be. The non-fraudulent transaction rate is 99.932%.5 So, our model could simply predict that every transaction was non-fraudulent and be correct 99.932% of the time. This illustrates the importance of establishing baseline performance for your models, so that we can calibrate our models and determine whether they are any good.
The Setup Phase is covered in Chapter 5.
- Modeling Phase. The modeling phase represents the opportunity to apply state-of-the-art algorithms to uncover some seriously profitable relationships lying hidden in the data. The modeling phase is the heart of your data scientific investigation and includes the following:
- Selecting and implementing the appropriate modeling algorithms. Applying inappropriate techniques will lead to inaccurate results that could cost your company big bucks.
- Making sure that our models outperform the baseline models.
- Fine-tuning your model algorithms to optimize the results. Should our decision tree be wide or deep? Should our neural network have one hidden layer or two? What should be our cutoff point to maximize profits? Analysts will need to spend some time fine-tuning their models before arriving at the optimal solution.
The modeling phase represents the core of your data science endeavor and is covered in Chapters 6 and 8-14.
- Evaluation Phase. Your buddy at work may think he has a lock on his prediction for the Super Bowl. But is his prediction any good? That is the question. Anyone can make predictions. It is how the predictions perform against real data that is the real test. In the evaluation phase, we assess how our models are doing, whether they are making any money, or whether we need to go back and try to improve our prediction models.
- Your models need to be evaluated against the baseline performance measures from the Setup Phase. Are we beating the monkeys-with-darts model? If not, better try again.
- You need to determine whether your models are actually solving the problem at hand. Are your models actually achieving the objectives set for it back in the Problem Understanding Phase? Has some important aspect of the problem not been sufficiently accounted for?
- Apply error costs intrinsic to the data, because data-driven cost evaluation is the best way to model the actual costs involved. For instance, in a marketing campaign, a false positive is not as costly as a false negative. However, for a mortgage lender, a false positive is much more costly.
- You should tabulate a suite of models and determine which model performs the best. Choose either a single best model, or a small number of models, to move forward to the Deployment Phase.
The Evaluation Phase is covered in Chapter 7.
- Deployment Phase. Finally, your models are ready for prime time! Report to management on your best models and work with management to adapt your models for real-world deployment.
- Writing a report of your results may be considered a simple example of deployment. In your report, concentrate on the results of interest to management. Show that you solved the problem and report on the estimated profit, if applicable.
- Stay involved with the project! Participate in the meetings and processes involved in model deployment, so that they stay focused on the problem at hand.
Figure 1.1 Data science methodology: the seven phases.
It should be emphasized that the DSM is iterative and adaptive. By adaptive, we mean that sometimes it is necessary to return to a previous phase for further work, based on some knowledge gained in the current phase. This is why there are arrows pointing both ways between most of the phases. For example, in the Evaluation Phase, we may find that the model we crafted does not actually address the original problem at hand, and that we need to return to the Modeling Phase to develop a model that will do so.
Also, the DSM is iterative, in that sometimes we may use our experience of building an effective model on a similar problem. That is, the model we created serves as an input to the investigation of a related problem. This is why the outer ring of arrows in Figure 1.1 shows a constant recycling of older models used as inputs to examining new solutions...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.