An Introduction to Statistical Learning: with Applications in R... with Python!
This page contains the solutions to the exercises proposed in
- 'An Introduction to Statistical Learning with Applications in R' (ISLR) by James, Witten, Hastie and Tibshirani .
Both conceptual and applied exercises were solved. An effort was made to detail all the answers and to provide a set of bibliographical references that we found useful. The exercises were solved using Python instead of R. You are welcome to collaborate.
Note [03.October.2017]: we will release each chapter's solutions on a monthly basis (at least).
|Chapter 2||Chapter 3||Chapter 4||Chapter 5||Chapter 6||Chapter 7||Chapter 8||Chapter 9||Chapter 10|
The main motivation of this project was learning. Today there are several good books and other resources from which to learn the material we covered, and we spent some time choosing a good learning project. We chose ISLR because it is an excellent, clear introduction to statistical learning, that keeps a nice balance between theory, intuition, mathematical rigour and programming. Our main goal was to use the exercises as an excuse to improve our proficiency using Python's data science stack. We had done other data science projects with Python, but, as we imagined, we still had a bit more to learn (and still do!). Since the book was written with R in mind, it made the use of Python a cool additional challenge. We are strong advocates of the active learning principles, and this project, once more, reinforced them in our minds. If you're starting out in machine learning with Python (or R!), we recommend you try it!
Technical requirements, and How to Install
This project was developed using Python 3.5 on Jupyter notebooks (Jupyter Lab, in fact). We tried to stay within the standard Python data science stack as much as possible. Accordingly, our main Python packages were numpy, matplotlib, pandas, seaborn, statsmodels and scikit-learn. You should be able to run this with the standard Python setup, and the additional libraries we list below.
If you're just starting out with Python, here's a more complete 'how-to'. We recommend using Anaconda whether you are using Linux, Mac or Windows. Anaconda allows you to easily manage several Python environments.
An environment is a collection of installed Python packages.
Imagine that you have two projects with different requirements: a recent one with, say, Python 3.5 and matplotlib 4.0, and a legacy project with Python 2.7 and matplotlib 3.5.
A good environment manager helps you install libraries and allows you to switch between both environments easily, avoiding dependencies migraines.
You can even work on both at the same time.
You don't want to know what the alternative is, to not using an environment manager.
So after installing Anaconda, the easiest way is to create a new environment and just install the libraries we list below one by one.
After this is done, just make sure the desired environment is active (for example, on Linux and Mac, type 'source activate
Here's the list of packages we installed:
- jupyterlab (but this should run just as well on regular ipython notebooks)
- ipywidgets (so that a seaborn import warning goes away)
In addition, we chose mkdocs to present these solutions in a website format, for a better presentation. We might change to a different scheme in the future (any suggestions), but meanwhile we used these libraries:
- pymdown-extensions #for latex
How to colaborate
If you want to collaborate, you can open an issue in our GitHub project and give us your suggestions on how to improve these solutions. On GitHub, you can also fork this project and send a pull request to fix any mistakes that you have found. Alternatively, you can also go for the classical way of collaboration and send us an e-mail. Any effort to improve the quality of these solutions will be appreciated.
In addition to thinking hard about them, to solve the exercises we followed several references. Besides ISLR , which is available for free and explains almost everything you need to know to solve the exercises, we also read some other books that provide a self-contained introduction to the field of statistical learning [2, 3, 4]. We also spent some quality time on CrossValidate. For the Python data science stack we think Wes McKinney's book  is a good choice, as well as Jake VanderPlas' . Additional references for some of the exercises are scattered througout the solutions.
-  James, G., Witten, D., Hastie, T., Tibshirani, R., 2015, An Introduction to Statistical Learning with Applications in R, Springer. [available for free]
-  Hastie, T., Tibshirani, R. 2006, The Elements of Statistical Learning: Data Mining, Inference, and Prediction, Springer. [available for free]
-  Bishop, C.M., 2006, Pattern Recognition and Machine Learning, Springer.
-  Murphy, K.P., 2012, Machine Learning: A Probabilistic Perspective, MIT Press.
-  McKinney, W., 2012, Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython
-  Jake VanderPlas, 2016, Python Data Science Handbook: Essential Tools for Working with Data, O'Reilly Media.
Fortunately, online resources are becoming more and more an essential tool for self-learning strategies. During the course of this project, we found several resources that can help you on your learning path. Here are the best ones we found.
Other solutions to ISLR
There are other solutions to ISLR, though most of them do not use Python. Below you can find a link to the solutions we found that were reasonably complete. We did a occasional check with some of these, and they might be a good complementary resource. A special mention to JWarmenhoven's github repo, which uses Python to reproduce figures, tables and calculations of the main text of the chapters and labs. Please let us know if you find any other significant solutions.
- JWarmenhoven: Python for the chapters text and labs
- James Crouser's Exercises and Python Labs
- John Weatherwax's solutions to the conceptual and practical exercises in R
- Pierre Pacquay's
Copyright (c)  [ISLP]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.