This commit is contained in:
packtBhagyashree committed 2017-09-19 15:50:43 +05:30
1 parent 1cf55acd75
commit 96f3e58e99
51 files changed
+1120 -1053

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
Python Machine Learning - Code Examples
## Chapter 7: Combining Different Models for Ensemble Learning
### Chapter Outline
- Learning with ensembles
- Combining classifiers via majority vote
- Implementing a simple majority vote classifier
- Using the majority voting principle to make predictions
- Evaluating and tuning the ensemble classifier
- Bagging – building an ensemble of classifiers from bootstrap samples
- Bagging in a nutshell
- Applying bagging to classify samples in the Wine dataset
- Leveraging weak learners via adaptive boosting
- How boosting works
- Applying AdaBoost using scikit-learn
- Summary
### A note on using the code examples
The recommended way to interact with the code examples in this book is via Jupyter Notebook (the `.ipynb` files). Using Jupyter Notebook, you will be able to execute the code step by step and have all the resulting outputs (including plots and images) all in one convenient document.
![](../ch02/images/jupyter-example-1.png)
Setting up Jupyter Notebook is really easy: if you are using the Anaconda Python distribution, all you need to install jupyter notebook is to execute the following command in your terminal:
conda install jupyter notebook
Then you can launch jupyter notebook by executing
jupyter notebook
A window will open up in your browser, which you can then use to navigate to the target directory that contains the `.ipynb` file you wish to open.
**More installation and setup instructions can be found in the [README.md file of Chapter 1](../ch01/README.md)**.
**(Even if you decide not to install Jupyter Notebook, note that you can also view the notebook files on GitHub by simply clicking on them: [`ch07.ipynb`](ch07.ipynb))**
In addition to the code examples, I added a table of contents to each Jupyter notebook as well as section headers that are consistent with the content of the book. Also, I included the original images and figures in hope that these make it easier to navigate and work with the code interactively as you are reading the book.
![](../ch02/images/jupyter-example-2.png)
When I was creating these notebooks, I was hoping to make your reading (and coding) experience as convenient as possible! However, if you don't wish to use Jupyter Notebooks, I also converted these notebooks to regular Python script files (`.py` files) that can be viewed and edited in any plaintext editor.
+1 -1
View File
@@ -1542,7 +1542,7 @@
"output_type": "stream",
"text": [
"[NbConvertApp] Converting notebook ch07.ipynb to script\n",
"[NbConvertApp] Writing 21930 bytes to ch07.py\n"
"[NbConvertApp] Writing 22091 bytes to ch07.py\n"
]
}
],
+37 -75
View File
@@ -1,6 +1,34 @@
# coding: utf-8
from scipy.misc import comb
import math
import numpy as np
import matplotlib.pyplot as plt
from sklearn.base import BaseEstimator
from sklearn.base import ClassifierMixin
from sklearn.preprocessing import LabelEncoder
from sklearn.externals import six
from sklearn.base import clone
from sklearn.pipeline import _name_estimators
import operator
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import Pipeline
from sklearn.model_selection import cross_val_score
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
from itertools import product
from sklearn.model_selection import GridSearchCV
import pandas as pd
from sklearn.ensemble import BaggingClassifier
from sklearn.metrics import accuracy_score
from sklearn.ensemble import AdaBoostClassifier
# *Python Machine Learning 2nd Edition* by [Sebastian Raschka](https://sebastianraschka.com), Packt Publishing Ltd. 2017
#
# Code Repository: https://github.com/rasbt/python-machine-learning-book-2nd-edition
@@ -13,7 +41,6 @@
# Note that the optional watermark extension is a small IPython notebook plugin that I developed to make the code reproducible. You can just skip the following line(s).
# In[1]:
@@ -37,29 +64,22 @@
# - [Summary](#Summary)
# In[1]:
from IPython.display import Image
# # Learning with ensembles
# In[2]:
# In[3]:
# In[5]:
from scipy.misc import comb
import math
def ensemble_error(n_classifier, error):
k_start = int(math.ceil(n_classifier / 2.))
@@ -68,26 +88,21 @@ def ensemble_error(n_classifier, error):
return sum(probs)
# In[6]:
ensemble_error(n_classifier=11, error=0.25)
# In[7]:
import numpy as np
error_range = np.arange(0.0, 1.01, 0.01)
ens_errors = [ensemble_error(n_classifier=11, error=error)
for error in error_range]
# In[8]:
import matplotlib.pyplot as plt
plt.plot(error_range,
ens_errors,
@@ -113,16 +128,13 @@ plt.show()
# ## Implementing a simple majority vote classifier
# In[9]:
import numpy as np
np.argmax(np.bincount([0, 0, 1],
weights=[0.2, 0.2, 0.6]))
# In[10]:
ex = np.array([[0.9, 0.1],
@@ -135,23 +147,13 @@ p = np.average(ex,
p
# In[11]:
np.argmax(p)
# In[12]:
from sklearn.base import BaseEstimator
from sklearn.base import ClassifierMixin
from sklearn.preprocessing import LabelEncoder
from sklearn.externals import six
from sklearn.base import clone
from sklearn.pipeline import _name_estimators
import numpy as np
import operator
class MajorityVoteClassifier(BaseEstimator,
@@ -285,13 +287,8 @@ class MajorityVoteClassifier(BaseEstimator,
# ## Using the majority voting principle to make predictions
# In[13]:
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
iris = datasets.load_iris()
X, y = iris.data[50:, [1, 2]], iris.target[50:]
@@ -304,15 +301,8 @@ X_train, X_test, y_train, y_test = train_test_split(X, y,
stratify=y)
# In[14]:
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import Pipeline
from sklearn.model_selection import cross_val_score
clf1 = LogisticRegression(penalty='l2',
C=0.001,
@@ -344,7 +334,6 @@ for clf, label in zip([pipe1, clf2, pipe3], clf_labels):
% (scores.mean(), scores.std(), label))
# In[15]:
# Majority Rule (hard) Voting
@@ -367,11 +356,8 @@ for clf, label in zip(all_clf, clf_labels):
# # Evaluating and tuning the ensemble classifier
# In[16]:
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
colors = ['black', 'orange', 'blue', 'green']
linestyles = [':', '--', '-.', '-']
@@ -406,17 +392,14 @@ plt.ylabel('True positive rate (TPR)')
plt.show()
# In[17]:
sc = StandardScaler()
X_train_std = sc.fit_transform(X_train)
# In[18]:
from itertools import product
all_clf = [pipe1, clf2, pipe3, mv_clf]
@@ -468,16 +451,13 @@ plt.text(-12.5, 4.5,
plt.show()
# In[19]:
mv_clf.get_params()
# In[20]:
from sklearn.model_selection import GridSearchCV
params = {'decisiontreeclassifier__max_depth': [1, 2],
'pipeline-1__clf__C': [0.001, 0.1, 100.0]}
@@ -495,7 +475,6 @@ for r, _ in enumerate(grid.cv_results_['mean_test_score']):
grid.cv_results_['params'][r]))
# In[21]:
print('Best parameters: %s' % grid.best_params_)
@@ -514,25 +493,21 @@ print('Accuracy: %.2f' % grid.best_score_)
#
# In addition, the "best" estimator can directly be accessed via the `best_estimator_` attribute.
# In[22]:
grid.best_estimator_.classifiers
# In[23]:
mv_clf = grid.best_estimator_
# In[24]:
mv_clf.set_params(**grid.best_estimator_.get_params())
# In[25]:
mv_clf
@@ -541,24 +516,20 @@ mv_clf
# # Bagging -- Building an ensemble of classifiers from bootstrap samples
# In[4]:
# ## Bagging in a nutshell
# In[28]:
# ## Applying bagging to classify samples in the Wine dataset
# In[29]:
import pandas as pd
df_wine = pd.read_csv('https://archive.ics.uci.edu/ml/'
'machine-learning-databases/wine/wine.data',
@@ -583,11 +554,8 @@ y = df_wine['Class label'].values
X = df_wine[['Alcohol', 'OD280/OD315 of diluted wines']].values
# In[30]:
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
le = LabelEncoder()
@@ -599,11 +567,8 @@ X_train, X_test, y_train, y_test = train_test_split(X, y,
stratify=y)
# In[31]:
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(criterion='entropy',
max_depth=None,
@@ -619,10 +584,8 @@ bag = BaggingClassifier(base_estimator=tree,
random_state=1)
# In[32]:
from sklearn.metrics import accuracy_score
tree = tree.fit(X_train, y_train)
y_train_pred = tree.predict(X_train)
@@ -643,11 +606,8 @@ print('Bagging train/test accuracies %.3f/%.3f'
% (bag_train, bag_test))
# In[33]:
import numpy as np
import matplotlib.pyplot as plt
x_min = X_train[:, 0].min() - 1
x_max = X_train[:, 0].max() + 1
@@ -697,22 +657,18 @@ plt.show()
# ## How boosting works
# In[34]:
# In[35]:
# ## Applying AdaBoost using scikit-learn
# In[36]:
from sklearn.ensemble import AdaBoostClassifier
tree = DecisionTreeClassifier(criterion='entropy',
max_depth=1,
@@ -724,7 +680,6 @@ ada = AdaBoostClassifier(base_estimator=tree,
random_state=1)
# In[37]:
tree = tree.fit(X_train, y_train)
@@ -746,7 +701,6 @@ print('AdaBoost train/test accuracies %.3f/%.3f'
% (ada_train, ada_test))
# In[38]:
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
@@ -788,3 +742,11 @@ plt.show()
# # Summary
# ...
# ---
#
# Readers may ignore the next cell.