├── hello.png
├── .gitignore
└── .gitignore
├── README.md
├── 05.01-What-Is-Machine-Learning.ipynb
├── Pandas-Operations.ipynb
├── Matplotlib-Errorbars.ipynb
├── Pandas-Performance-Eval-and-Query.ipynb
├── Pandas-Missing-Values.ipynb
└── Pandas-Vectorized-String-Ops.ipynb
/hello.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thinknewtech/Data-Science/HEAD/hello.png
--------------------------------------------------------------------------------
/.gitignore/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | env/
12 | build/
13 | develop-eggs/
14 | dist/
15 | downloads/
16 | eggs/
17 | .eggs/
18 | lib/
19 | lib64/
20 | parts/
21 | sdist/
22 | var/
23 | wheels/
24 | *.egg-info/
25 | .installed.cfg
26 | *.egg
27 |
28 | # PyInstaller
29 | # Usually these files are written by a python script from a template
30 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
31 | *.manifest
32 | *.spec
33 |
34 | # Installer logs
35 | pip-log.txt
36 | pip-delete-this-directory.txt
37 |
38 | # Unit test / coverage reports
39 | htmlcov/
40 | .tox/
41 | .coverage
42 | .coverage.*
43 | .cache
44 | nosetests.xml
45 | coverage.xml
46 | *.cover
47 | .hypothesis/
48 |
49 | # Translations
50 | *.mo
51 | *.pot
52 |
53 | # Django stuff:
54 | *.log
55 | local_settings.py
56 |
57 | # Flask stuff:
58 | instance/
59 | .webassets-cache
60 |
61 | # Scrapy stuff:
62 | .scrapy
63 |
64 | # Sphinx documentation
65 | docs/_build/
66 |
67 | # PyBuilder
68 | target/
69 |
70 | # Jupyter Notebook
71 | .ipynb_checkpoints
72 |
73 | # pyenv
74 | .python-version
75 |
76 | # celery beat schedule file
77 | celerybeat-schedule
78 |
79 | # SageMath parsed files
80 | *.sage.py
81 |
82 | # dotenv
83 | .env
84 |
85 | # virtualenv
86 | .venv
87 | venv/
88 | ENV/
89 |
90 | # Spyder project settings
91 | .spyderproject
92 | .spyproject
93 |
94 | # Rope project settings
95 | .ropeproject
96 |
97 | # mkdocs documentation
98 | /site
99 |
100 | # mypy
101 | .mypy_cache/
102 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Data Science Handbook Cribsheet
2 | Intro to Numpy, Pandas, Matplotlib and Scikit-Learn. Condensed from Python Data Science Handbook (@jakevdp)
3 |
4 | 
5 |
6 |
7 |
8 | | iPython |
9 | NumPy |
10 | Pandas |
11 | Matplotlib |
12 | Scikit-Learn |
13 |
14 |
15 | | Help & docs |
16 | Datatypes |
17 | Pandas objects |
18 | Line plots |
19 | Intro to ML |
20 |
21 | | Keyboard shortcuts |
22 | Arrays |
23 | Indexing & selection |
24 | Scatter plots |
25 | Intro to Scikit-Learn |
26 |
27 | | Magic commands |
28 | Universal functions |
29 | Data operations |
30 | Error Bars |
31 | Hyperparameters & Model validation |
32 |
33 | | Input & output history |
34 | Aggregations |
35 | Missing data |
36 | Density & contour plots |
37 | Feature engineering |
38 |
39 | | Shell commands |
40 | Broadcasting |
41 | Hierarchical indexing |
42 | Histograms, bins & densities |
43 | Naive Bayes |
44 |
45 | | Errors & debugging |
46 | Comparisons, masks, boolean logic |
47 | Concat & append |
48 | Plot legends |
49 | Linear regression |
50 |
51 | | Profiling & timing |
52 | Fancy indexing |
53 | Merge & join |
54 | Colorbars |
55 | Support vector machines (SVM) |
56 |
57 | | Resources |
58 | Sorting arrays |
59 | Aggregation & grouping |
60 | Subplots |
61 | Decision trees & Random forests |
62 |
63 | |
64 | Structured data |
65 | Pivot tables |
66 | Text & Annotation |
67 | Principal component analysis (PCA) |
68 |
69 | |
70 | |
71 | Vectorized strings |
72 | Custom Tickmarks |
73 | Manifold learning |
74 |
75 | |
76 | |
77 | Time series |
78 | Configs & stylesheets |
79 | K-Means clustering |
80 |
81 | |
82 | |
83 | High-performance ops: eval(), query() |
84 | 3D plotting |
85 | Gaussian mixtures |
86 |
87 | |
88 | |
89 | |
90 | Geo data with Basemap |
91 | TO DO:Kernel density estimation (KDE) |
92 |
93 | |
94 | |
95 | |
96 | Visualization with Seaborn |
97 | TO DO:Face detection pipeline |
98 |
99 | |
100 | |
101 | |
102 | |
103 | Resources |
104 |
105 |
--------------------------------------------------------------------------------
/05.01-What-Is-Machine-Learning.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# What Is Machine Learning?"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Categories of Machine Learning\n",
15 | "\n",
16 | "- __Supervised__ learning models the relationship between measured features and label associated with some data. Once the model is built, it can be used to apply labels to new, unknown data.\n",
17 | "- This is further subdivided into __classification__ and __regression__ tasks: classification labels are __discrete__ categories; regression labels are __continuous__ quantities.\n",
18 | "\n",
19 | "- __Unsupervised__ learning models the features of a dataset without reference to any label. It is often described as \"letting the dataset speak for itself.\"\n",
20 | "- These models include tasks such as __clustering__ and __dimensionality reduction.__\n",
21 | "- Clustering algorithms identify __distinct__ groups of data, while dimensionality reduction algorithms search for __more succinct representations__ of the data.\n",
22 | "- In addition, there are so-called __semi-supervised__ learning methods. They are often useful when labels are incomplete or misleading."
23 | ]
24 | },
25 | {
26 | "cell_type": "markdown",
27 | "metadata": {},
28 | "source": [
29 | "### Classification: Predicting discrete labels\n",
30 | "\n",
31 | "- We will first take a look at a simple __classification__ task, in which you are given a set of labeled points and want to use these to classify some unlabeled points."
32 | ]
33 | },
34 | {
35 | "cell_type": "markdown",
36 | "metadata": {},
37 | "source": [
38 | ""
39 | ]
40 | },
41 | {
42 | "cell_type": "markdown",
43 | "metadata": {},
44 | "source": [
45 | "- Here we have two *features* for each point, represented by the *(x,y)* positions of the points on the plane. We also have one of two *class labels* for each point represented by the point's color.\n",
46 | "- We would like to create a model that will let us decide whether a new point should be labeled \"blue\" or \"red.\"\n",
47 | "- Let's assume the two groups can be separated by drawing a straight line through the plane between them, such that points on each side of the line fall in the same group.\n",
48 | "- Here the *model* is a mathematical equivalent of \"a straight line separates the classes\", while the *model parameters* are the values describing the location and orientation of that line.\n",
49 | "- The optimal values for these model parameters are learned from the data. This is often called *training the model*.\n",
50 | "- Here's what the trained model looks like:"
51 | ]
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "metadata": {},
56 | "source": [
57 | ""
58 | ]
59 | },
60 | {
61 | "cell_type": "markdown",
62 | "metadata": {},
63 | "source": [
64 | "- This trained model can now be applied to new, unlabeled data. This is called *prediction*. See the following figure:"
65 | ]
66 | },
67 | {
68 | "cell_type": "markdown",
69 | "metadata": {},
70 | "source": [
71 | ""
72 | ]
73 | },
74 | {
75 | "cell_type": "markdown",
76 | "metadata": {},
77 | "source": [
78 | "### Regression: Predicting continuous labels\n",
79 | "\n",
80 | "- Consider the data shown in the following figure, which consists of a set of points with continuous labels:"
81 | ]
82 | },
83 | {
84 | "cell_type": "markdown",
85 | "metadata": {},
86 | "source": [
87 | ""
88 | ]
89 | },
90 | {
91 | "cell_type": "markdown",
92 | "metadata": {},
93 | "source": [
94 | "- The color of each point represents the continuous label for that point. We will use a simple linear regression to predict the points.\n",
95 | "- This model assumes if we treat the label as a third spatial dimension, we can fit a plane to the data."
96 | ]
97 | },
98 | {
99 | "cell_type": "markdown",
100 | "metadata": {},
101 | "source": [
102 | ""
103 | ]
104 | },
105 | {
106 | "cell_type": "markdown",
107 | "metadata": {},
108 | "source": [
109 | "Notice that the *feature 1-feature 2* plane here is the same as in the two-dimensional plot from before; in this case, however, we have represented the labels by both color and three-dimensional axis position.\n",
110 | "From this view, it seems reasonable that fitting a plane through this three-dimensional data would allow us to predict the expected label for any set of input parameters.\n",
111 | "Returning to the two-dimensional projection, when we fit such a plane we get the result shown in the following figure:"
112 | ]
113 | },
114 | {
115 | "cell_type": "markdown",
116 | "metadata": {},
117 | "source": [
118 | "\n",
119 | "[figure source in Appendix](06.00-Figure-Code.ipynb#Regression-Example-Figure-3)"
120 | ]
121 | },
122 | {
123 | "cell_type": "markdown",
124 | "metadata": {},
125 | "source": [
126 | "This plane of fit gives us what we need to predict labels for new points.\n",
127 | "Visually, we find the results shown in the following figure:"
128 | ]
129 | },
130 | {
131 | "cell_type": "markdown",
132 | "metadata": {},
133 | "source": [
134 | ""
135 | ]
136 | },
137 | {
138 | "cell_type": "markdown",
139 | "metadata": {},
140 | "source": [
141 | "As with the classification example, this may seem rather trivial in a low number of dimensions.\n",
142 | "But the power of these methods is that they can be straightforwardly applied and evaluated in the case of data with many, many features.\n",
143 | "\n",
144 | "For example, this is similar to the task of computing the distance to galaxies observed through a telescope—in this case, we might use the following features and labels:\n",
145 | "\n",
146 | "- *feature 1*, *feature 2*, etc. $\\to$ brightness of each galaxy at one of several wave lengths or colors\n",
147 | "- *label* $\\to$ distance or redshift of the galaxy\n",
148 | "\n",
149 | "The distances for a small number of these galaxies might be determined through an independent set of (typically more expensive) observations.\n",
150 | "Distances to remaining galaxies could then be estimated using a suitable regression model, without the need to employ the more expensive observation across the entire set.\n",
151 | "In astronomy circles, this is known as the \"photometric redshift\" problem."
152 | ]
153 | },
154 | {
155 | "cell_type": "markdown",
156 | "metadata": {},
157 | "source": [
158 | "### Clustering: Inferring labels on unlabeled data\n",
159 | "\n",
160 | "The classification and regression illustrations we just looked at are examples of supervised learning algorithms, in which we are trying to build a model that will predict labels for new data.\n",
161 | "Unsupervised learning involves models that describe data without reference to any known labels.\n",
162 | "\n",
163 | "One common case of unsupervised learning is \"clustering,\" in which data is automatically assigned to some number of discrete groups.\n",
164 | "For example, we might have some two-dimensional data like that shown in the following figure:"
165 | ]
166 | },
167 | {
168 | "cell_type": "markdown",
169 | "metadata": {},
170 | "source": [
171 | ""
172 | ]
173 | },
174 | {
175 | "cell_type": "markdown",
176 | "metadata": {},
177 | "source": [
178 | "By eye, it is clear that each of these points is part of a distinct group.\n",
179 | "Given this input, a clustering model will use the intrinsic structure of the data to determine which points are related.\n",
180 | "The *k*-means algorithm yields the clusters shown in the following figure:"
181 | ]
182 | },
183 | {
184 | "cell_type": "markdown",
185 | "metadata": {},
186 | "source": [
187 | "\n",
188 | "[figure source in Appendix](06.00-Figure-Code.ipynb#Clustering-Example-Figure-2)"
189 | ]
190 | },
191 | {
192 | "cell_type": "markdown",
193 | "metadata": {},
194 | "source": [
195 | "*k*-means fits a model consisting of *k* cluster centers; the optimal centers are assumed to be those that minimize the distance of each point from its assigned center.\n",
196 | "Again, this might seem like a trivial exercise in two dimensions, but as our data becomes larger and more complex, such clustering algorithms can be employed to extract useful information from the dataset."
197 | ]
198 | },
199 | {
200 | "cell_type": "markdown",
201 | "metadata": {},
202 | "source": [
203 | "### Dimensionality reduction: Inferring structure of unlabeled data\n",
204 | "\n",
205 | "Dimensionality reduction is another example of an unsupervised algorithm, in which labels or other information are inferred from the structure of the dataset itself.\n",
206 | "Dimensionality reduction is a bit more abstract than the examples we looked at before, but generally it seeks to pull out some low-dimensional representation of data that in some way preserves relevant qualities of the full dataset.\n",
207 | "\n",
208 | "As an example of this, consider the data shown in the following figure:"
209 | ]
210 | },
211 | {
212 | "cell_type": "markdown",
213 | "metadata": {},
214 | "source": [
215 | ""
216 | ]
217 | },
218 | {
219 | "cell_type": "markdown",
220 | "metadata": {},
221 | "source": [
222 | "Visually, it is clear that there is some structure in this data: it is drawn from a one-dimensional line that is arranged in a spiral within this two-dimensional space.\n",
223 | "In a sense, you could say that this data is \"intrinsically\" only one dimensional, though this one-dimensional data is embedded in higher-dimensional space.\n",
224 | "A suitable dimensionality reduction model in this case would be sensitive to this nonlinear embedded structure, and be able to pull out this lower-dimensionality representation.\n",
225 | "\n",
226 | "The following figure shows a visualization of the results of the Isomap algorithm, a manifold learning algorithm that does exactly this:"
227 | ]
228 | },
229 | {
230 | "cell_type": "markdown",
231 | "metadata": {},
232 | "source": [
233 | ""
234 | ]
235 | },
236 | {
237 | "cell_type": "markdown",
238 | "metadata": {},
239 | "source": [
240 | "Notice that the colors (which represent the extracted one-dimensional latent variable) change uniformly along the spiral, which indicates that the algorithm did in fact detect the structure we saw by eye.\n",
241 | "As with the previous examples, the power of dimensionality reduction algorithms becomes clearer in higher-dimensional cases.\n",
242 | "For example, we might wish to visualize important relationships within a dataset that has 100 or 1,000 features.\n",
243 | "Visualizing 1,000-dimensional data is a challenge, and one way we can make this more manageable is to use a dimensionality reduction technique to reduce the data to two or three dimensions."
244 | ]
245 | },
246 | {
247 | "cell_type": "markdown",
248 | "metadata": {},
249 | "source": [
250 | "## Summary\n",
251 | "\n",
252 | "Here we have seen a few simple examples of some of the basic types of machine learning approaches.\n",
253 | "Needless to say, there are a number of important practical details that we have glossed over, but I hope this section was enough to give you a basic idea of what types of problems machine learning approaches can solve.\n",
254 | "\n",
255 | "In short, we saw the following:\n",
256 | "\n",
257 | "- *Supervised learning*: Models that can predict labels based on labeled training data\n",
258 | "\n",
259 | " - *Classification*: Models that predict labels as two or more discrete categories\n",
260 | " - *Regression*: Models that predict continuous labels\n",
261 | " \n",
262 | "- *Unsupervised learning*: Models that identify structure in unlabeled data\n",
263 | "\n",
264 | " - *Clustering*: Models that detect and identify distinct groups in the data\n",
265 | " - *Dimensionality reduction*: Models that detect and identify lower-dimensional structure in higher-dimensional data\n",
266 | " \n",
267 | "In the following sections we will go into much greater depth within these categories, and see some more interesting examples of where these concepts can be useful."
268 | ]
269 | }
270 | ],
271 | "metadata": {
272 | "anaconda-cloud": {},
273 | "kernelspec": {
274 | "display_name": "Python 3",
275 | "language": "python",
276 | "name": "python3"
277 | },
278 | "language_info": {
279 | "codemirror_mode": {
280 | "name": "ipython",
281 | "version": 3
282 | },
283 | "file_extension": ".py",
284 | "mimetype": "text/x-python",
285 | "name": "python",
286 | "nbconvert_exporter": "python",
287 | "pygments_lexer": "ipython3",
288 | "version": "3.6.3"
289 | }
290 | },
291 | "nbformat": 4,
292 | "nbformat_minor": 1
293 | }
294 |
--------------------------------------------------------------------------------
/Pandas-Operations.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Operating on Data in Pandas"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "- NumPy provides quick element-wise operations for basic arithmetic and sophisticated operations (trigonometric functions, exponential and logarithmic functions, etc.).\n",
15 | "\n",
16 | "- Pandas inherits this functionality and adds useful twists:\n",
17 | "\n",
18 | " - unary operations like negation and trigonometric functions *preserve index and column labels* in the output.\n",
19 | " - binary operations such as addition and multiplication automatically *align indices* when passing the objects to the ufunc."
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {},
25 | "source": [
26 | "### Ufuncs: Index Preservation\n",
27 | "\n",
28 | "- Any NumPy ufunc will work on Pandas ``Series`` and ``DataFrame`` objects."
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 1,
34 | "metadata": {
35 | "collapsed": true
36 | },
37 | "outputs": [],
38 | "source": [
39 | "import pandas as pd\n",
40 | "import numpy as np"
41 | ]
42 | },
43 | {
44 | "cell_type": "code",
45 | "execution_count": 2,
46 | "metadata": {},
47 | "outputs": [
48 | {
49 | "data": {
50 | "text/plain": [
51 | "0 6\n",
52 | "1 3\n",
53 | "2 7\n",
54 | "3 4\n",
55 | "dtype: int64"
56 | ]
57 | },
58 | "execution_count": 2,
59 | "metadata": {},
60 | "output_type": "execute_result"
61 | }
62 | ],
63 | "source": [
64 | "rng = np.random.RandomState(42)\n",
65 | "ser = pd.Series(rng.randint(0, 10, 4))\n",
66 | "ser"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": 3,
72 | "metadata": {},
73 | "outputs": [
74 | {
75 | "data": {
76 | "text/html": [
77 | "