2015-04-12 02:01:49 +08:00
{
2015-04-15 02:24:23 +08:00
" cells " : [
2015-04-12 02:01:49 +08:00
{
2015-04-15 02:24:23 +08:00
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-04-16 00:35:19 +08:00
" # scikit-learn-intro "
2015-04-15 02:24:23 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-05-31 21:46:07 +08:00
" Credits: Forked from [PyCon 2015 Scikit-learn Tutorial](https://github.com/jakevdp/sklearn_pycon2015) by Jake VanderPlas \n " ,
" \n " ,
2015-04-15 02:24:23 +08:00
" * Machine Learning Models Cheat Sheet \n " ,
" * Estimators \n " ,
" * Introduction: Iris Dataset \n " ,
2015-04-16 00:35:19 +08:00
" * K-Nearest Neighbors Classifier "
2015-04-15 02:24:23 +08:00
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 1 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [ ] ,
" source " : [
" % matplotlib inline \n " ,
" import numpy as np \n " ,
" import matplotlib.pyplot as plt \n " ,
" import seaborn; \n " ,
" from sklearn.linear_model import LinearRegression \n " ,
" from scipy import stats \n " ,
" import pylab as pl \n " ,
" \n " ,
" seaborn.set() "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Machine Learning Models Cheat Sheet "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 2 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
2015-04-12 04:33:28 +08:00
{
2015-04-15 02:24:23 +08:00
" data " : {
" image/png " : " iVBORw0KGgoAAAANSUhEUgAACEoAAAUrCAYAAAA3g9BsAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAG \n bgAABm4BRLh3LwAAAAd0SU1FB90EEw8XG1Hi8acAAAAGYktHRAD/AP8A/6C9p5MAC5xvSURBVHja \n 7N0HfBR1+sfxv3eo2M7ee+9nOT0PxcKJZzlUVFA5sYsKdk9PTz3RS68EAklIQhopEAgJkEK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WEnuZPmaKNDZGARWZC \n gtbJYkxkpAoLCelWQQp53UtMTDRPmTLFJOOF6urqDOvWrdPv3LlTZzab+T0BAAAAAAAAAAAISgA9 \n gU6na2lubjZIYTDkIIuaocHBamJCglowZYpaN2+e0jU2UmAGjnDm5cvVvvp6taO6Wm2oqFCr585V \n dYWF2uvEzIkTVXZiohaoigwL69IhisDAQBUXF2eR18r58+drnSi2b9/OOA8AAAAAAAAAAEBQAugO \n pLDX1NRkyM7ONgcFBR1U8TBxzBi1MC9PbV20SPvEOYVhAO1lam5WO5cuVRvKy9WK0lJVXVCgZufk \n qGmpqSotLk4bAxJ8kK9ZnRWgyM3NbQ1Q/PjjjwQoAAAAAAAAAAAAQQmgK9i/f7+uoqLCGB0dfVBj \n NSaMHauq8vO1gibFXQCHWktDg/px8WKtc01DUZGqyMvTRvzkJidrYz/Cu8DIDwIUAAAAAAAAAACA \n oARwGG3btk03Y8YMU3BwcLsKfgEBASo9Pl4tnTFD7amtpVALoMs70NCgtlRWquWlpapy2jQ1MyND \n ex0bHRmpvaYRoAAAAAA
" text/plain " : [
" <IPython.core.display.Image object> "
]
} ,
" execution_count " : 2 ,
" metadata " : {
" image/png " : {
" width " : 800
2015-04-12 04:33:28 +08:00
}
2015-04-15 02:24:23 +08:00
} ,
" output_type " : " execute_result "
}
] ,
" source " : [
" from IPython.display import Image \n " ,
" Image( \" http://scikit-learn.org/dev/_static/ml_map.png \" , width=800) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Estimators "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" Given a scikit-learn *estimator* object named `model`, the following methods are available: \n " ,
" \n " ,
" - Available in **all Estimators** \n " ,
" + `model.fit()` : fit training data. For supervised learning applications, \n " ,
" this accepts two arguments: the data `X` and the labels `y` (e.g. `model.fit(X, y)`). \n " ,
" For unsupervised learning applications, this accepts only a single argument, \n " ,
" the data `X` (e.g. `model.fit(X)`). \n " ,
" - Available in **supervised estimators** \n " ,
" + `model.predict()` : given a trained model, predict the label of a new set of data. \n " ,
" This method accepts one argument, the new data `X_new` (e.g. `model.predict(X_new)`), \n " ,
" and returns the learned label for each object in the array. \n " ,
" + `model.predict_proba()` : For classification problems, some estimators also provide \n " ,
" this method, which returns the probability that a new observation has each categorical label. \n " ,
" In this case, the label with the highest probability is returned by `model.predict()`. \n " ,
" + `model.score()` : for classification or regression problems, most (all?) estimators implement \n " ,
" a score method. Scores are between 0 and 1, with a larger score indicating a better fit. \n " ,
" - Available in **unsupervised estimators** \n " ,
" + `model.predict()` : predict labels in clustering algorithms. \n " ,
" + `model.transform()` : given an unsupervised model, transform new data into the new basis. \n " ,
" This also accepts one argument `X_new`, and returns the new representation of the data based \n " ,
" on the unsupervised model. \n " ,
" + `model.fit_transform()` : some estimators implement this method, \n " ,
" which more efficiently performs a fit and a transform on the same input data. "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Introduction: Iris Dataset "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 3 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
2015-04-12 04:33:28 +08:00
{
2015-04-15 02:24:23 +08:00
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" [ ' target_names ' , ' data ' , ' target ' , ' DESCR ' , ' feature_names ' ] \n " ,
" (150, 4) \n " ,
" (150, 4) \n " ,
" (150,) \n " ,
" [ ' setosa ' ' versicolor ' ' virginica ' ] \n " ,
" [ ' sepal length (cm) ' , ' sepal width (cm) ' , ' petal length (cm) ' , ' petal width (cm) ' ] \n "
2015-04-12 04:33:28 +08:00
]
2015-04-15 02:24:23 +08:00
}
] ,
" source " : [
" from sklearn.datasets import load_iris \n " ,
" iris = load_iris() \n " ,
" \n " ,
" n_samples, n_features = iris.data.shape \n " ,
" print(iris.keys()) \n " ,
" print((n_samples, n_features)) \n " ,
" print(iris.data.shape) \n " ,
" print(iris.target.shape) \n " ,
" print(iris.target_names) \n " ,
" print(iris.feature_names) "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 4 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
2015-04-12 04:33:28 +08:00
{
2015-04-15 02:24:23 +08:00
" data " : {
" image/png " : " iVBORw0KGgoAAAANSUhEUgAAAeYAAAFkCAYAAAD165gcAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz \n AAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd8VFXawPHfnZn0TgihBEKTQw9NmggoNoqoWFcFG4pd \n dxXL7rvuru66urq2VXTXBoJYEUWKoii9KL2FAwSQDiEhvc7Mff+YEMpMkiHJJJPh+frhY+7k3HOe \n k/bMPefecwzTNBFCCCGEf7DUdwBCCCGEOEkSsxBCCOFHJDELIYQQfkQSsxBCCOFHJDELIYQQfkQS \n sxBCCOFHbL6qWCllAd4DOgBO4G6ttfZVe0IIIUQg8OUV82VAhNZ6EPAs8A8ftiWEEEIEBF8m5kIg \n RillADFAiQ/bEkIIIQKCz4aygWVAKLANiAeu9GFbQgghREAwfLUkp1Lqj7iGsv+klEoCfgK6aq09 \n XjmbpmkahuGTWIQQQpw1n/1B7tVvUrUTz9pV9wd8ovDlFXMEkFP28XEgCLBWVNgwDNLTc30YTt1J \n SIiSvvgh6Yt/kr74p4SEqPoO4Zzly8T8EvChUmoJrqT8tNa60IftCSGEEA2ezxKz1joLuMZX9Qsh \n hBCBSBYYEUIIIfyIJGYhhBDCj0hiFkIIIfyIJGYhhBDCj0hiFkIIIfyIJGYhhBDCj0hiFkIIIfyI \n JGYhhBDCj0hiFkIIIfyIJGYhhBDCj0hiFkIIIfyIJGYhhBDCj0hiFkIIIfyIJGYhhBDCj0hiFkII \n IfyIJGYhhBDCj0hiFkIIIfyIJGYhhBDCj0hiFkIIIfyIJGYhhBDCj0hiFkIIIfyIJGYhhBDCj0hi \n FkIIIfyIJGYhhBDCj0hiFkIIIfyIJGYhhBDCj0hiFkIIIfyIJGYhhBDCj0hiFkIIIfyIJGYhhBDC \n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0rykpcTDmmvtITEys \n 77CEEKLGJDGLBis0NJS77rypvsMQQohaJUPZQgghhB+RK2bhd1asWM1389dgtcAdt4+kZcukGtWX \n n5/PW5M+obTUoGvXllw1OvDnok3T5P33P2fP3uMktYjmnrtvwmKR9+FCNASSmIVf2bBhC8+/+CMl \n 9hYAbN32Pu/99w9ERkZVu86JT77Gzt1JGIaFRctSsZfaufbaEbUVsl96+d/vM/8nJxZLBKtW53Ms \n 47/83x/vq++whBBekLfQwq8sXLS6PCkDpGc0ZcXK6q/JnJeXy67dTgzD9aNuGI34dc2emobp97am \n ZmKxhAFgsYSybVtWPUckhPCWT6+YlVJPA1cCQcCbWuspvmxPNHyxMeE4HNlYrSEAWI0cklo0rXZ9 \n YWHhhIU6KCh2HZumSXhYbUTq38LDjUqPhRD+y2dXzEqpocAArfVAYCjQ1ldticBx661j6NEtE9O5 \n F6uxm6tGNaNTp47Vrs9qtXLH7YMIC07DdB4gqdkeHnygZndym6bJG29M4b4HXuHxJ15jz569FZad \n MWMuDzz0Kg898grLl/9ao3bPxn0TRtEoZhcO+35io3dx7z0j66xtIUTNGKZp+qRipdTzgAl0AaKB \n iVrrysYkzfT0XJ/EUtcSEqKQvlSfaZocP55JcHAIkZGRtVJnUVERwcFODCMMw6jZ1eO7733K51/l \n lA8VN03YzeQP/uxW7+IlK3n+xeWYJAAQFryXdybdRdOm1R8BOMGb74vdbicjI4P4+HhsNv+9nUR+ \n X/xTQkKUz4ZZevWbVO3Es3bV/QE//OPLOeYEoDdwHXAv8LEP2xIBxDAMGjWKr7WkDK5nnhMTE2uc \n lAHSdmWUJ2WAw0cs5ObmuJVbv357eVIGyCtM5Ndf19e4fW/ZbDYSExP9OikLIdz58jf2GJCqtbYD \n 25VSRUqpxlrrYxWdkJB
" text/plain " : [
2015-04-16 00:35:19 +08:00
" <matplotlib.figure.Figure at 0x10da12b90> "
2015-04-15 02:24:23 +08:00
]
} ,
2015-04-12 04:33:28 +08:00
" metadata " : { } ,
2015-04-15 02:24:23 +08:00
" output_type " : " display_data "
}
] ,
" source " : [
" import numpy as np \n " ,
" import matplotlib.pyplot as plt \n " ,
" \n " ,
" # ' sepal width (cm) ' \n " ,
" x_index = 1 \n " ,
" # ' petal length (cm) ' \n " ,
" y_index = 2 \n " ,
" \n " ,
" # this formatter will label the colorbar with the correct target names \n " ,
" formatter = plt.FuncFormatter(lambda i, *args: iris.target_names[int(i)]) \n " ,
" \n " ,
" plt.scatter(iris.data[:, x_index], iris.data[:, y_index], \n " ,
" c=iris.target, cmap=plt.cm.get_cmap( ' RdYlBu ' , 3)) \n " ,
" plt.colorbar(ticks=[0, 1, 2], format=formatter) \n " ,
" plt.clim(-0.5, 2.5) \n " ,
" plt.xlabel(iris.feature_names[x_index]) \n " ,
" plt.ylabel(iris.feature_names[y_index]); "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## K-Nearest Neighbors Classifier \n " ,
" \n " ,
" The K-Nearest Neighbors (KNN) algorithm is a method used for algorithm used for **classification** or for **regression**. In both cases, the input consists of the k closest training examples in the feature space. Given a new, unknown observation, look up which points have the closest features and assign the predominant class. "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 5 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
2015-04-12 04:33:28 +08:00
{
2015-04-15 02:24:23 +08:00
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" [ ' versicolor ' ] \n " ,
" [ ' setosa ' ' versicolor ' ' virginica ' ] \n " ,
" [[ 0. 0.8 0.2]] \n "
2015-04-12 04:33:28 +08:00
]
} ,
{
2015-04-15 02:24:23 +08:00
" data " : {
" image/png " : " iVBORw0KGgoAAAANSUhEUgAAAfAAAAFgCAYAAABEyiulAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz \n AAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd4VFX6wPHvJJPeSELovV2qCCKgSAcLIChVVNTVXXVd \n XXV3rdtcd127a10Lrj8UKQJKV5AuRemhcwIJISQE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E9I0hbeUlSUR2LiLpo1 \n a06nTq5ZGGu1Wtm37xcMBgN9+95AUFBIna8ZNqzQJfu+ICEhqsZhvyRwIYTLSAIXDZ0nE7isQhdC \n XDVJ2EJ4j5wDF0IIIfyQJHAhhBDCD0kCF0IIIfyQnAMXHndIKdT33xNosdB86FD69+vn9DZLDAZW \n zJ5NaE4O5tatGT9tGoE1lFJUyckcWr6cQLOZJoMGccPAgU7vXwghPE0SuPCoM+fPc+qDD5iYlQXA \n 9iNHOPr883Tt1Mmp7X730UdM/+UXAoHCXbtYYjYz6d57q/U7n59P0vvvMzEzE4A9hw6xPyqKaxpY \n PXYhhP+TBC48ak9iIjdXJG+AAQUFLNu3z+kEHn3yJBfqmUUBwSdO2O2379AhhlUkb4C+RUUsO3hQ \n ErgDZMW5EL5FzoELj2rTpg0qNLSyfSYggJhmzZzebmlMTOXPVqA0Otr+/lu3Jin8YoWm8zod4QkJ \n Tu9fCCE8TUbgwqN6du7MD5MmkbR2LYFmM4aBA5kyaJDT2+1z333M++ILos+fJ7dlS262M30O0Kl1 \n a9ZMncp3q1YRZDJR3K8f04YPd3r/QgjhaVKJTXiF1WrFarW65J69VZktlhoXr12+f4vV6lDfhkqm \n zIW4clKJTdR7Op0Onc7
" text/plain " : [
2015-04-16 00:35:19 +08:00
" <matplotlib.figure.Figure at 0x10ddfad10> "
2015-04-15 02:24:23 +08:00
]
} ,
2015-04-12 04:33:28 +08:00
" metadata " : { } ,
2015-04-15 02:24:23 +08:00
" output_type " : " display_data "
}
] ,
" source " : [
" from sklearn import neighbors, datasets \n " ,
" \n " ,
" iris = datasets.load_iris() \n " ,
" X, y = iris.data, iris.target \n " ,
" \n " ,
" # create the model \n " ,
" knn = neighbors.KNeighborsClassifier(n_neighbors=5, weights= ' uniform ' ) \n " ,
" \n " ,
" # fit the model \n " ,
" knn.fit(X, y) \n " ,
" \n " ,
" # What kind of iris has 3cm x 5cm sepal and 4cm x 2cm petal? \n " ,
" X_pred = [3, 5, 4, 2] \n " ,
" result = knn.predict([X_pred, ]) \n " ,
" \n " ,
" print(iris.target_names[result]) \n " ,
" print(iris.target_names) \n " ,
" print(knn.predict_proba([X_pred, ])) \n " ,
" \n " ,
" from fig_code import plot_iris_knn \n " ,
" plot_iris_knn() "
]
2015-04-23 01:29:07 +08:00
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" Note we see overfitting in the K-Nearest Neighbors model above. We ' ll be addressing overfitting and model validation in a later notebook. "
]
2015-04-15 02:24:23 +08:00
}
] ,
" metadata " : {
" kernelspec " : {
" display_name " : " Python 2 " ,
" language " : " python " ,
" name " : " python2 "
} ,
" language_info " : {
" codemirror_mode " : {
" name " : " ipython " ,
" version " : 2
} ,
" file_extension " : " .py " ,
" mimetype " : " text/x-python " ,
" name " : " python " ,
" nbconvert_exporter " : " python " ,
" pygments_lexer " : " ipython2 " ,
" version " : " 2.7.9 "
2015-04-12 02:01:49 +08:00
}
2015-04-15 02:24:23 +08:00
} ,
" nbformat " : 4 ,
" nbformat_minor " : 0
}