updating week 45
@@ -173,7 +173,7 @@ MathJax.Hub.Config({
|
||||
|
||||
<ul>
|
||||
<li> Thursday: Boosting methods, froma AdaBoost to Gradient boosting</li>
|
||||
<li> Friday: Gradient boosting and discussion of Decision trees and ensemble methods</li>
|
||||
<li> Friday: Gradient boosting and discussion of Decision trees and ensemble methods. Wrapping up trees and start discussing Support Vector Machines</li>
|
||||
</ul>
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
@@ -181,6 +181,8 @@ MathJax.Hub.Config({
|
||||
<ol>
|
||||
<li> <a href="https://www.youtube.com/watch?v=RmajweUFKvM&ab_channel=Simplilearn" target="_self">Video on Decision trees</a></li>
|
||||
<li> <a href="https://www.youtube.com/watch?v=wPqtzj5VZus&ab_channel=H2O.ai" target="_self">Video on boosting methods by Hastie</a>.</li>
|
||||
<li> <a href="https://www.youtube.com/watch?v=LsK-xG1cLYA" target="_self">Video on AdaBoost</a></li>
|
||||
<li> <a href="https://www.youtube.com/watch?v=3CC4N4z3GJc" target="_self">Video on Gradient boost, part 1, parts 2-4 follows</a></li>
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
@@ -190,7 +192,7 @@ MathJax.Hub.Config({
|
||||
<div class="panel-body">
|
||||
<!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
|
||||
<ol>
|
||||
<li> Add material about AdaBoost and Gradient boosting</li>
|
||||
<li> <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/Textbooks/elementsstat.pdf" target="_self">Hastie et al, chapter 10.1-10.10</a></li>
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -178,25 +178,27 @@ MathJax.Hub.Config({
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class="highlight" style="background: #f8f8f8">
|
||||
<pre style="line-height: 125%;"><span style="color: #408080; font-style: italic"># Common imports</span>
|
||||
<pre style="line-height: 125%;"><span style="color: #666666">%</span>matplotlib inline
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Common imports</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> Image
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pydot</span> <span style="color: #008000; font-weight: bold">import</span> graph_from_dot_data
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeClassifier
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeRegressor
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> export_graphviz
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler, OneHotEncoder
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.compose</span> <span style="color: #008000; font-weight: bold">import</span> ColumnTransformer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> Image
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pydot</span> <span style="color: #008000; font-weight: bold">import</span> graph_from_dot_data
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> BaggingClassifier
|
||||
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> RandomForestClassifier
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> LabelEncoder
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> cross_validate
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">os</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Where to save the figures and data files</span>
|
||||
@@ -229,37 +231,31 @@ X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_tes
|
||||
<span style="color: #008000">print</span>(X_train<span style="color: #666666">.</span>shape)
|
||||
<span style="color: #008000">print</span>(X_test<span style="color: #666666">.</span>shape)
|
||||
<span style="color: #408080; font-style: italic">#Scale the data</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
|
||||
scaler <span style="color: #666666">=</span> StandardScaler()
|
||||
scaler<span style="color: #666666">.</span>fit(X_train)
|
||||
X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_train)
|
||||
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
|
||||
<span style="color: #408080; font-style: italic">#define methods</span>
|
||||
<span style="color: #408080; font-style: italic"># Logistic Regression</span>
|
||||
logreg <span style="color: #666666">=</span> LogisticRegression(solver<span style="color: #666666">=</span><span style="color: #BA2121">'lbfgs'</span>)
|
||||
logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Test set accuracy Logistic Regression with scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
<span style="color: #408080; font-style: italic"># Support Vector Machine</span>
|
||||
svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Test set accuracy SVM with scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
<span style="color: #408080; font-style: italic"># Decision Trees</span>
|
||||
deep_tree_clf <span style="color: #666666">=</span> DecisionTreeClassifier(max_depth<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">None</span>)
|
||||
deep_tree_clf<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Test set accuracy with Decision Trees and scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(deep_tree_clf<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
|
||||
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> RandomForestClassifier
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> LabelEncoder
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> cross_validate
|
||||
<span style="color: #408080; font-style: italic"># Data set not specificied</span>
|
||||
<span style="color: #408080; font-style: italic"># Support Vector Machine</span>
|
||||
svm <span style="color: #666666">=</span> SVC(gamma<span style="color: #666666">=</span><span style="color: #BA2121">'auto'</span>, C<span style="color: #666666">=100</span>)
|
||||
svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Test set accuracy SVM with scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
<span style="color: #408080; font-style: italic"># Random forests</span>
|
||||
<span style="color: #408080; font-style: italic">#Instantiate the model with 500 trees and entropy as splitting criteria</span>
|
||||
Random_Forest_model <span style="color: #666666">=</span> RandomForestClassifier(n_estimators<span style="color: #666666">=500</span>,criterion<span style="color: #666666">=</span><span style="color: #BA2121">"entropy"</span>)
|
||||
Random_Forest_model<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #408080; font-style: italic">#Cross validation</span>
|
||||
accuracy <span style="color: #666666">=</span> cross_validate(Random_Forest_model,X_test_scaled,y_test,cv<span style="color: #666666">=10</span>)[<span style="color: #BA2121">'test_score'</span>]
|
||||
<span style="color: #008000">print</span>(accuracy)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Test set accuracy with Random Forests and scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(Random_Forest_model<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
|
||||
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</span>
|
||||
|
||||
y_pred <span style="color: #666666">=</span> Random_Forest_model<span style="color: #666666">.</span>predict(X_test_scaled)
|
||||
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_confusion_matrix(y_test, y_pred, normalize<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
@@ -199,7 +199,7 @@ MathJax.Hub.Config({
|
||||
|
||||
<ul>
|
||||
<p><li> Thursday: Boosting methods, froma AdaBoost to Gradient boosting</li>
|
||||
<p><li> Friday: Gradient boosting and discussion of Decision trees and ensemble methods</li>
|
||||
<p><li> Friday: Gradient boosting and discussion of Decision trees and ensemble methods. Wrapping up trees and start discussing Support Vector Machines</li>
|
||||
</ul>
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -208,6 +208,8 @@ MathJax.Hub.Config({
|
||||
<ol>
|
||||
<p><li> <a href="https://www.youtube.com/watch?v=RmajweUFKvM&ab_channel=Simplilearn" target="_blank">Video on Decision trees</a></li>
|
||||
<p><li> <a href="https://www.youtube.com/watch?v=wPqtzj5VZus&ab_channel=H2O.ai" target="_blank">Video on boosting methods by Hastie</a>.</li>
|
||||
<p><li> <a href="https://www.youtube.com/watch?v=LsK-xG1cLYA" target="_blank">Video on AdaBoost</a></li>
|
||||
<p><li> <a href="https://www.youtube.com/watch?v=3CC4N4z3GJc" target="_blank">Video on Gradient boost, part 1, parts 2-4 follows</a></li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
@@ -216,7 +218,7 @@ MathJax.Hub.Config({
|
||||
<b>Reading</b>
|
||||
<p>
|
||||
<ol>
|
||||
<p><li> Add material about AdaBoost and Gradient boosting</li>
|
||||
<p><li> <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/Textbooks/elementsstat.pdf" target="_blank">Hastie et al, chapter 10.1-10.10</a></li>
|
||||
</ol>
|
||||
</div>
|
||||
</section>
|
||||
@@ -231,25 +233,27 @@ MathJax.Hub.Config({
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class="highlight" style="background: #eeeedd">
|
||||
<pre style="font-size: 80%; line-height: 125%;"><span style="color: #228B22"># Common imports</span>
|
||||
<pre style="font-size: 80%; line-height: 125%;">%matplotlib inline
|
||||
|
||||
<span style="color: #228B22"># Common imports</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">IPython.display</span> <span style="color: #8B008B; font-weight: bold">import</span> Image
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">pydot</span> <span style="color: #8B008B; font-weight: bold">import</span> graph_from_dot_data
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.tree</span> <span style="color: #8B008B; font-weight: bold">import</span> DecisionTreeClassifier
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.tree</span> <span style="color: #8B008B; font-weight: bold">import</span> DecisionTreeRegressor
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.tree</span> <span style="color: #8B008B; font-weight: bold">import</span> export_graphviz
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler, OneHotEncoder
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.compose</span> <span style="color: #8B008B; font-weight: bold">import</span> ColumnTransformer
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">IPython.display</span> <span style="color: #8B008B; font-weight: bold">import</span> Image
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">pydot</span> <span style="color: #8B008B; font-weight: bold">import</span> graph_from_dot_data
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_breast_cancer
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.svm</span> <span style="color: #8B008B; font-weight: bold">import</span> SVC
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LogisticRegression
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.ensemble</span> <span style="color: #8B008B; font-weight: bold">import</span> BaggingClassifier
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.ensemble</span> <span style="color: #8B008B; font-weight: bold">import</span> RandomForestClassifier
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> LabelEncoder
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> cross_validate
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">scikitplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">skplt</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">os</span>
|
||||
|
||||
<span style="color: #228B22"># Where to save the figures and data files</span>
|
||||
@@ -282,37 +286,31 @@ X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,ra
|
||||
<span style="color: #658b00">print</span>(X_train.shape)
|
||||
<span style="color: #658b00">print</span>(X_test.shape)
|
||||
<span style="color: #228B22">#Scale the data</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
<span style="color: #228B22">#define methods</span>
|
||||
<span style="color: #228B22"># Logistic Regression</span>
|
||||
logreg = LogisticRegression(solver=<span style="color: #CD5555">'lbfgs'</span>)
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy Logistic Regression with scaled data: {:.2f}"</span>.format(logreg.score(X_test_scaled,y_test)))
|
||||
<span style="color: #228B22"># Support Vector Machine</span>
|
||||
svm.fit(X_train_scaled, y_train)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy SVM with scaled data: {:.2f}"</span>.format(logreg.score(X_test_scaled,y_test)))
|
||||
<span style="color: #228B22"># Decision Trees</span>
|
||||
deep_tree_clf = DecisionTreeClassifier(max_depth=<span style="color: #8B008B; font-weight: bold">None</span>)
|
||||
deep_tree_clf.fit(X_train_scaled, y_train)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy with Decision Trees and scaled data: {:.2f}"</span>.format(deep_tree_clf.score(X_test_scaled,y_test)))
|
||||
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.ensemble</span> <span style="color: #8B008B; font-weight: bold">import</span> RandomForestClassifier
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> LabelEncoder
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> cross_validate
|
||||
<span style="color: #228B22"># Data set not specificied</span>
|
||||
<span style="color: #228B22"># Support Vector Machine</span>
|
||||
svm = SVC(gamma=<span style="color: #CD5555">'auto'</span>, C=<span style="color: #B452CD">100</span>)
|
||||
svm.fit(X_train_scaled, y_train)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy SVM with scaled data: {:.2f}"</span>.format(logreg.score(X_test_scaled,y_test)))
|
||||
<span style="color: #228B22"># Random forests</span>
|
||||
<span style="color: #228B22">#Instantiate the model with 500 trees and entropy as splitting criteria</span>
|
||||
Random_Forest_model = RandomForestClassifier(n_estimators=<span style="color: #B452CD">500</span>,criterion=<span style="color: #CD5555">"entropy"</span>)
|
||||
Random_Forest_model.fit(X_train_scaled, y_train)
|
||||
<span style="color: #228B22">#Cross validation</span>
|
||||
accuracy = cross_validate(Random_Forest_model,X_test_scaled,y_test,cv=<span style="color: #B452CD">10</span>)[<span style="color: #CD5555">'test_score'</span>]
|
||||
<span style="color: #658b00">print</span>(accuracy)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy with Random Forests and scaled data: {:.2f}"</span>.format(Random_Forest_model.score(X_test_scaled,y_test)))
|
||||
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">scikitplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">skplt</span>
|
||||
|
||||
y_pred = Random_Forest_model.predict(X_test_scaled)
|
||||
skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=<span style="color: #8B008B; font-weight: bold">True</span>)
|
||||
plt.show()
|
||||
|
||||
@@ -176,7 +176,7 @@ MathJax.Hub.Config({
|
||||
|
||||
<ul>
|
||||
<li> Thursday: Boosting methods, froma AdaBoost to Gradient boosting</li>
|
||||
<li> Friday: Gradient boosting and discussion of Decision trees and ensemble methods</li>
|
||||
<li> Friday: Gradient boosting and discussion of Decision trees and ensemble methods. Wrapping up trees and start discussing Support Vector Machines</li>
|
||||
</ul>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b>Videos</b>
|
||||
@@ -184,6 +184,8 @@ MathJax.Hub.Config({
|
||||
<ol>
|
||||
<li> <a href="https://www.youtube.com/watch?v=RmajweUFKvM&ab_channel=Simplilearn" target="_blank">Video on Decision trees</a></li>
|
||||
<li> <a href="https://www.youtube.com/watch?v=wPqtzj5VZus&ab_channel=H2O.ai" target="_blank">Video on boosting methods by Hastie</a>.</li>
|
||||
<li> <a href="https://www.youtube.com/watch?v=LsK-xG1cLYA" target="_blank">Video on AdaBoost</a></li>
|
||||
<li> <a href="https://www.youtube.com/watch?v=3CC4N4z3GJc" target="_blank">Video on Gradient boost, part 1, parts 2-4 follows</a></li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
@@ -192,7 +194,7 @@ MathJax.Hub.Config({
|
||||
<b>Reading</b>
|
||||
<p>
|
||||
<ol>
|
||||
<li> Add material about AdaBoost and Gradient boosting</li>
|
||||
<li> <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/Textbooks/elementsstat.pdf" target="_blank">Hastie et al, chapter 10.1-10.10</a></li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
@@ -207,25 +209,27 @@ MathJax.Hub.Config({
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class="highlight" style="background: #eeeedd">
|
||||
<pre style="line-height: 125%;"><span style="color: #228B22"># Common imports</span>
|
||||
<pre style="line-height: 125%;">%matplotlib inline
|
||||
|
||||
<span style="color: #228B22"># Common imports</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">IPython.display</span> <span style="color: #8B008B; font-weight: bold">import</span> Image
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">pydot</span> <span style="color: #8B008B; font-weight: bold">import</span> graph_from_dot_data
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.tree</span> <span style="color: #8B008B; font-weight: bold">import</span> DecisionTreeClassifier
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.tree</span> <span style="color: #8B008B; font-weight: bold">import</span> DecisionTreeRegressor
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.tree</span> <span style="color: #8B008B; font-weight: bold">import</span> export_graphviz
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler, OneHotEncoder
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.compose</span> <span style="color: #8B008B; font-weight: bold">import</span> ColumnTransformer
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">IPython.display</span> <span style="color: #8B008B; font-weight: bold">import</span> Image
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">pydot</span> <span style="color: #8B008B; font-weight: bold">import</span> graph_from_dot_data
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_breast_cancer
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.svm</span> <span style="color: #8B008B; font-weight: bold">import</span> SVC
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LogisticRegression
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.ensemble</span> <span style="color: #8B008B; font-weight: bold">import</span> BaggingClassifier
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.ensemble</span> <span style="color: #8B008B; font-weight: bold">import</span> RandomForestClassifier
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> LabelEncoder
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> cross_validate
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">scikitplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">skplt</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">os</span>
|
||||
|
||||
<span style="color: #228B22"># Where to save the figures and data files</span>
|
||||
@@ -258,37 +262,31 @@ X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,ra
|
||||
<span style="color: #658b00">print</span>(X_train.shape)
|
||||
<span style="color: #658b00">print</span>(X_test.shape)
|
||||
<span style="color: #228B22">#Scale the data</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
<span style="color: #228B22">#define methods</span>
|
||||
<span style="color: #228B22"># Logistic Regression</span>
|
||||
logreg = LogisticRegression(solver=<span style="color: #CD5555">'lbfgs'</span>)
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy Logistic Regression with scaled data: {:.2f}"</span>.format(logreg.score(X_test_scaled,y_test)))
|
||||
<span style="color: #228B22"># Support Vector Machine</span>
|
||||
svm.fit(X_train_scaled, y_train)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy SVM with scaled data: {:.2f}"</span>.format(logreg.score(X_test_scaled,y_test)))
|
||||
<span style="color: #228B22"># Decision Trees</span>
|
||||
deep_tree_clf = DecisionTreeClassifier(max_depth=<span style="color: #8B008B; font-weight: bold">None</span>)
|
||||
deep_tree_clf.fit(X_train_scaled, y_train)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy with Decision Trees and scaled data: {:.2f}"</span>.format(deep_tree_clf.score(X_test_scaled,y_test)))
|
||||
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.ensemble</span> <span style="color: #8B008B; font-weight: bold">import</span> RandomForestClassifier
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> LabelEncoder
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> cross_validate
|
||||
<span style="color: #228B22"># Data set not specificied</span>
|
||||
<span style="color: #228B22"># Support Vector Machine</span>
|
||||
svm = SVC(gamma=<span style="color: #CD5555">'auto'</span>, C=<span style="color: #B452CD">100</span>)
|
||||
svm.fit(X_train_scaled, y_train)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy SVM with scaled data: {:.2f}"</span>.format(logreg.score(X_test_scaled,y_test)))
|
||||
<span style="color: #228B22"># Random forests</span>
|
||||
<span style="color: #228B22">#Instantiate the model with 500 trees and entropy as splitting criteria</span>
|
||||
Random_Forest_model = RandomForestClassifier(n_estimators=<span style="color: #B452CD">500</span>,criterion=<span style="color: #CD5555">"entropy"</span>)
|
||||
Random_Forest_model.fit(X_train_scaled, y_train)
|
||||
<span style="color: #228B22">#Cross validation</span>
|
||||
accuracy = cross_validate(Random_Forest_model,X_test_scaled,y_test,cv=<span style="color: #B452CD">10</span>)[<span style="color: #CD5555">'test_score'</span>]
|
||||
<span style="color: #658b00">print</span>(accuracy)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy with Random Forests and scaled data: {:.2f}"</span>.format(Random_Forest_model.score(X_test_scaled,y_test)))
|
||||
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">scikitplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">skplt</span>
|
||||
|
||||
y_pred = Random_Forest_model.predict(X_test_scaled)
|
||||
skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=<span style="color: #8B008B; font-weight: bold">True</span>)
|
||||
plt.show()
|
||||
|
||||
@@ -253,7 +253,7 @@ MathJax.Hub.Config({
|
||||
|
||||
<ul>
|
||||
<li> Thursday: Boosting methods, froma AdaBoost to Gradient boosting</li>
|
||||
<li> Friday: Gradient boosting and discussion of Decision trees and ensemble methods</li>
|
||||
<li> Friday: Gradient boosting and discussion of Decision trees and ensemble methods. Wrapping up trees and start discussing Support Vector Machines</li>
|
||||
</ul>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b>Videos</b>
|
||||
@@ -261,6 +261,8 @@ MathJax.Hub.Config({
|
||||
<ol>
|
||||
<li> <a href="https://www.youtube.com/watch?v=RmajweUFKvM&ab_channel=Simplilearn" target="_blank">Video on Decision trees</a></li>
|
||||
<li> <a href="https://www.youtube.com/watch?v=wPqtzj5VZus&ab_channel=H2O.ai" target="_blank">Video on boosting methods by Hastie</a>.</li>
|
||||
<li> <a href="https://www.youtube.com/watch?v=LsK-xG1cLYA" target="_blank">Video on AdaBoost</a></li>
|
||||
<li> <a href="https://www.youtube.com/watch?v=3CC4N4z3GJc" target="_blank">Video on Gradient boost, part 1, parts 2-4 follows</a></li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
@@ -269,7 +271,7 @@ MathJax.Hub.Config({
|
||||
<b>Reading</b>
|
||||
<p>
|
||||
<ol>
|
||||
<li> Add material about AdaBoost and Gradient boosting</li>
|
||||
<li> <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/Textbooks/elementsstat.pdf" target="_blank">Hastie et al, chapter 10.1-10.10</a></li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
@@ -284,25 +286,27 @@ MathJax.Hub.Config({
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class="highlight" style="background: #f8f8f8">
|
||||
<pre style="line-height: 125%;"><span style="color: #408080; font-style: italic"># Common imports</span>
|
||||
<pre style="line-height: 125%;"><span style="color: #666666">%</span>matplotlib inline
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Common imports</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> Image
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pydot</span> <span style="color: #008000; font-weight: bold">import</span> graph_from_dot_data
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeClassifier
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeRegressor
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> export_graphviz
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler, OneHotEncoder
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.compose</span> <span style="color: #008000; font-weight: bold">import</span> ColumnTransformer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> Image
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pydot</span> <span style="color: #008000; font-weight: bold">import</span> graph_from_dot_data
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> BaggingClassifier
|
||||
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> RandomForestClassifier
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> LabelEncoder
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> cross_validate
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">os</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Where to save the figures and data files</span>
|
||||
@@ -335,37 +339,31 @@ X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_tes
|
||||
<span style="color: #008000">print</span>(X_train<span style="color: #666666">.</span>shape)
|
||||
<span style="color: #008000">print</span>(X_test<span style="color: #666666">.</span>shape)
|
||||
<span style="color: #408080; font-style: italic">#Scale the data</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
|
||||
scaler <span style="color: #666666">=</span> StandardScaler()
|
||||
scaler<span style="color: #666666">.</span>fit(X_train)
|
||||
X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_train)
|
||||
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
|
||||
<span style="color: #408080; font-style: italic">#define methods</span>
|
||||
<span style="color: #408080; font-style: italic"># Logistic Regression</span>
|
||||
logreg <span style="color: #666666">=</span> LogisticRegression(solver<span style="color: #666666">=</span><span style="color: #BA2121">'lbfgs'</span>)
|
||||
logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Test set accuracy Logistic Regression with scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
<span style="color: #408080; font-style: italic"># Support Vector Machine</span>
|
||||
svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Test set accuracy SVM with scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
<span style="color: #408080; font-style: italic"># Decision Trees</span>
|
||||
deep_tree_clf <span style="color: #666666">=</span> DecisionTreeClassifier(max_depth<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">None</span>)
|
||||
deep_tree_clf<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Test set accuracy with Decision Trees and scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(deep_tree_clf<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
|
||||
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> RandomForestClassifier
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> LabelEncoder
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> cross_validate
|
||||
<span style="color: #408080; font-style: italic"># Data set not specificied</span>
|
||||
<span style="color: #408080; font-style: italic"># Support Vector Machine</span>
|
||||
svm <span style="color: #666666">=</span> SVC(gamma<span style="color: #666666">=</span><span style="color: #BA2121">'auto'</span>, C<span style="color: #666666">=100</span>)
|
||||
svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Test set accuracy SVM with scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
<span style="color: #408080; font-style: italic"># Random forests</span>
|
||||
<span style="color: #408080; font-style: italic">#Instantiate the model with 500 trees and entropy as splitting criteria</span>
|
||||
Random_Forest_model <span style="color: #666666">=</span> RandomForestClassifier(n_estimators<span style="color: #666666">=500</span>,criterion<span style="color: #666666">=</span><span style="color: #BA2121">"entropy"</span>)
|
||||
Random_Forest_model<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #408080; font-style: italic">#Cross validation</span>
|
||||
accuracy <span style="color: #666666">=</span> cross_validate(Random_Forest_model,X_test_scaled,y_test,cv<span style="color: #666666">=10</span>)[<span style="color: #BA2121">'test_score'</span>]
|
||||
<span style="color: #008000">print</span>(accuracy)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Test set accuracy with Random Forests and scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(Random_Forest_model<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
|
||||
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</span>
|
||||
|
||||
y_pred <span style="color: #666666">=</span> Random_Forest_model<span style="color: #666666">.</span>predict(X_test_scaled)
|
||||
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_confusion_matrix(y_test, y_pred, normalize<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 39 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 43 KiB |
|
Before Width: | Height: | Size: 8.1 KiB After Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 17 KiB |
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 42 KiB |
@@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "06bf33a6",
|
||||
"id": "5d0f5baa",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -14,7 +14,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b1777bc2",
|
||||
"id": "a1cc809f",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -29,7 +29,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f97929b1",
|
||||
"id": "71a0b699",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -38,7 +38,7 @@
|
||||
"\n",
|
||||
"* Thursday: Boosting methods, froma AdaBoost to Gradient boosting\n",
|
||||
"\n",
|
||||
"* Friday: Gradient boosting and discussion of Decision trees and ensemble methods\n",
|
||||
"* Friday: Gradient boosting and discussion of Decision trees and ensemble methods. Wrapping up trees and start discussing Support Vector Machines\n",
|
||||
"\n",
|
||||
"**Videos.**\n",
|
||||
"\n",
|
||||
@@ -46,14 +46,18 @@
|
||||
"\n",
|
||||
"2. [Video on boosting methods by Hastie](https://www.youtube.com/watch?v=wPqtzj5VZus&ab_channel=H2O.ai).\n",
|
||||
"\n",
|
||||
"3. [Video on AdaBoost](https://www.youtube.com/watch?v=LsK-xG1cLYA)\n",
|
||||
"\n",
|
||||
"4. [Video on Gradient boost, part 1, parts 2-4 follows](https://www.youtube.com/watch?v=3CC4N4z3GJc)\n",
|
||||
"\n",
|
||||
"**Reading.**\n",
|
||||
"\n",
|
||||
"1. Add material about AdaBoost and Gradient boosting"
|
||||
"1. [Hastie et al, chapter 10.1-10.10](https://github.com/CompPhysics/MachineLearning/blob/master/doc/Textbooks/elementsstat.pdf)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "da2cba17",
|
||||
"id": "39e049b0",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -64,13 +68,15 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "1054a91d",
|
||||
"id": "ebd16dd3",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%matplotlib inline\n",
|
||||
"\n",
|
||||
"%matplotlib inline\n",
|
||||
"\n",
|
||||
"# Common imports\n",
|
||||
@@ -79,19 +85,19 @@
|
||||
"import pandas as pd\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"from sklearn.tree import DecisionTreeClassifier\n",
|
||||
"from sklearn.tree import DecisionTreeRegressor\n",
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
"from sklearn.tree import export_graphviz\n",
|
||||
"from sklearn.preprocessing import StandardScaler, OneHotEncoder\n",
|
||||
"from sklearn.compose import ColumnTransformer\n",
|
||||
"from IPython.display import Image \n",
|
||||
"from pydot import graph_from_dot_data\n",
|
||||
"from sklearn.datasets import load_breast_cancer\n",
|
||||
"from sklearn.svm import SVC\n",
|
||||
"from sklearn.linear_model import LogisticRegression\n",
|
||||
"from sklearn.ensemble import BaggingClassifier\n",
|
||||
"\n",
|
||||
"from sklearn.ensemble import RandomForestClassifier\n",
|
||||
"from sklearn.preprocessing import LabelEncoder\n",
|
||||
"from sklearn.model_selection import cross_validate\n",
|
||||
"import scikitplot as skplt\n",
|
||||
"from sklearn.preprocessing import StandardScaler\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"# Where to save the figures and data files\n",
|
||||
@@ -124,37 +130,31 @@
|
||||
"print(X_train.shape)\n",
|
||||
"print(X_test.shape)\n",
|
||||
"#Scale the data\n",
|
||||
"from sklearn.preprocessing import StandardScaler\n",
|
||||
"scaler = StandardScaler()\n",
|
||||
"scaler.fit(X_train)\n",
|
||||
"X_train_scaled = scaler.transform(X_train)\n",
|
||||
"X_test_scaled = scaler.transform(X_test)\n",
|
||||
"#define methods\n",
|
||||
"# Logistic Regression\n",
|
||||
"logreg = LogisticRegression(solver='lbfgs')\n",
|
||||
"logreg.fit(X_train_scaled, y_train)\n",
|
||||
"print(\"Test set accuracy Logistic Regression with scaled data: {:.2f}\".format(logreg.score(X_test_scaled,y_test)))\n",
|
||||
"# Support Vector Machine\n",
|
||||
"svm.fit(X_train_scaled, y_train)\n",
|
||||
"print(\"Test set accuracy SVM with scaled data: {:.2f}\".format(logreg.score(X_test_scaled,y_test)))\n",
|
||||
"# Decision Trees\n",
|
||||
"deep_tree_clf = DecisionTreeClassifier(max_depth=None)\n",
|
||||
"deep_tree_clf.fit(X_train_scaled, y_train)\n",
|
||||
"print(\"Test set accuracy with Decision Trees and scaled data: {:.2f}\".format(deep_tree_clf.score(X_test_scaled,y_test)))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"from sklearn.ensemble import RandomForestClassifier\n",
|
||||
"from sklearn.preprocessing import LabelEncoder\n",
|
||||
"from sklearn.model_selection import cross_validate\n",
|
||||
"# Data set not specificied\n",
|
||||
"# Support Vector Machine\n",
|
||||
"svm = SVC(gamma='auto', C=100)\n",
|
||||
"svm.fit(X_train_scaled, y_train)\n",
|
||||
"print(\"Test set accuracy SVM with scaled data: {:.2f}\".format(logreg.score(X_test_scaled,y_test)))\n",
|
||||
"# Random forests\n",
|
||||
"#Instantiate the model with 500 trees and entropy as splitting criteria\n",
|
||||
"Random_Forest_model = RandomForestClassifier(n_estimators=500,criterion=\"entropy\")\n",
|
||||
"Random_Forest_model.fit(X_train_scaled, y_train)\n",
|
||||
"#Cross validation\n",
|
||||
"accuracy = cross_validate(Random_Forest_model,X_test_scaled,y_test,cv=10)['test_score']\n",
|
||||
"print(accuracy)\n",
|
||||
"print(\"Test set accuracy with Random Forests and scaled data: {:.2f}\".format(Random_Forest_model.score(X_test_scaled,y_test)))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"import scikitplot as skplt\n",
|
||||
"\n",
|
||||
"y_pred = Random_Forest_model.predict(X_test_scaled)\n",
|
||||
"skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True)\n",
|
||||
"plt.show()\n",
|
||||
@@ -167,7 +167,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b647351f",
|
||||
"id": "d703c0d6",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -187,7 +187,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e7dbffcd",
|
||||
"id": "ccee85a0",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -201,7 +201,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "494a82af",
|
||||
"id": "a519ddc4",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -213,7 +213,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d27f68f8",
|
||||
"id": "db6968e5",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -230,7 +230,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4dabbeb4",
|
||||
"id": "e7132763",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -242,7 +242,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7937173f",
|
||||
"id": "647540de",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -256,7 +256,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8872158c",
|
||||
"id": "fe9d52b3",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -268,7 +268,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1e5b19b8",
|
||||
"id": "00d09af0",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -281,7 +281,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9c2e02d1",
|
||||
"id": "722da425",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -293,7 +293,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4244f14f",
|
||||
"id": "3954663b",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -303,7 +303,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "91147e5f",
|
||||
"id": "f63c1e38",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -331,7 +331,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "af34f35f",
|
||||
"id": "d8d5c7ac",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -347,7 +347,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cc2ea75e",
|
||||
"id": "96cf5cd8",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -359,7 +359,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5a5b4d62",
|
||||
"id": "439579c9",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -370,7 +370,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "daddba87",
|
||||
"id": "c38ab9a3",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -382,7 +382,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2a6e0f8d",
|
||||
"id": "8e21cd6d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -392,7 +392,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f39ea127",
|
||||
"id": "704b3592",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -404,7 +404,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "62a2184d",
|
||||
"id": "7476b8be",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -414,7 +414,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a2781707",
|
||||
"id": "03d12a19",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -426,7 +426,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "95c004bf",
|
||||
"id": "7fb4048b",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -436,7 +436,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4694fd52",
|
||||
"id": "0c1adb18",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -448,7 +448,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "06aea446",
|
||||
"id": "60880562",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -462,7 +462,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "20d085e6",
|
||||
"id": "372bc35c",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -478,7 +478,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "acc85c07",
|
||||
"id": "b65e2b72",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -490,7 +490,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "16f291d8",
|
||||
"id": "12464e0a",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -506,7 +506,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c3a7df55",
|
||||
"id": "efc71414",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -518,7 +518,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9104a175",
|
||||
"id": "bc2c815d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -528,7 +528,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "86ddaaa4",
|
||||
"id": "de0906dc",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -540,7 +540,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ec36807d",
|
||||
"id": "fc08da05",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -552,7 +552,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f102ce80",
|
||||
"id": "1ad3aec7",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -564,7 +564,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d7b0991f",
|
||||
"id": "4d661142",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -575,7 +575,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9c1752b5",
|
||||
"id": "373fd6db",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -587,7 +587,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7a9e3ab4",
|
||||
"id": "241ade2a",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -598,7 +598,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6fb6c431",
|
||||
"id": "ced9d9f4",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -610,7 +610,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "46ccbf78",
|
||||
"id": "9b361f88",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -620,7 +620,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cc303451",
|
||||
"id": "e0665101",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -632,7 +632,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b1c2be38",
|
||||
"id": "a8189cf6",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -644,7 +644,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e45307bf",
|
||||
"id": "509020f9",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -656,7 +656,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "edef26a5",
|
||||
"id": "1ea5083e",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -668,7 +668,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ef3f9795",
|
||||
"id": "f021311a",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -678,7 +678,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6772b2a5",
|
||||
"id": "714b2b90",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -690,7 +690,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "645ab1ac",
|
||||
"id": "57df0a90",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -700,7 +700,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "36afb932",
|
||||
"id": "7dd87728",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -712,7 +712,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a0e70734",
|
||||
"id": "647ba1c0",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -722,7 +722,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a20d1c75",
|
||||
"id": "499ab775",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -734,7 +734,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "415dcee2",
|
||||
"id": "6fd9ba90",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -744,7 +744,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "18c44408",
|
||||
"id": "e2318f01",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -756,7 +756,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2bf44e31",
|
||||
"id": "461b32c3",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -766,7 +766,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3cfed10d",
|
||||
"id": "c8b03689",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -778,7 +778,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "84f4d3f8",
|
||||
"id": "47d84c49",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -798,7 +798,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4a886572",
|
||||
"id": "16159393",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -810,7 +810,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e2da363e",
|
||||
"id": "297ad278",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -820,7 +820,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "aec10b1a",
|
||||
"id": "4c619128",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -836,7 +836,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1814cd1d",
|
||||
"id": "41cd0b50",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -848,7 +848,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "72b3a813",
|
||||
"id": "9ca95984",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -876,7 +876,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7ec1c900",
|
||||
"id": "1044743b",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -889,7 +889,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "341c3079",
|
||||
"id": "fcf973b1",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -921,7 +921,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ddaaaa66",
|
||||
"id": "2ec8ee43",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -939,7 +939,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "97b3c420",
|
||||
"id": "740c50e2",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -952,7 +952,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3e0cffa8",
|
||||
"id": "be50ed96",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -964,7 +964,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "49e25f7c",
|
||||
"id": "b247b123",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -974,7 +974,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f4eda3ea",
|
||||
"id": "8a250bd9",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -986,7 +986,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6d9e5f3b",
|
||||
"id": "01ec8e23",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -996,7 +996,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f924c7ad",
|
||||
"id": "43f1e9ca",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1008,7 +1008,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "78272ab5",
|
||||
"id": "f097d76e",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1021,7 +1021,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0c5911f7",
|
||||
"id": "c164dbbf",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1033,7 +1033,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "476e645a",
|
||||
"id": "fa91537a",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1045,7 +1045,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0f989a46",
|
||||
"id": "1b7f5606",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1057,7 +1057,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b2668d41",
|
||||
"id": "953f7aa6",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1067,7 +1067,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7f5965b6",
|
||||
"id": "08b92a46",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1079,7 +1079,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "32e6877d",
|
||||
"id": "36cf052c",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1089,7 +1089,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "61785a3c",
|
||||
"id": "554af8ba",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1105,7 +1105,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "df29a69e",
|
||||
"id": "ba8b8439",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1117,7 +1117,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d8b61f23",
|
||||
"id": "8d9c4b52",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1138,7 +1138,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d067eeac",
|
||||
"id": "597eaf22",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1149,7 +1149,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "2c80d8e8",
|
||||
"id": "251b3392",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -1201,7 +1201,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4128dc16",
|
||||
"id": "bab59665",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1212,7 +1212,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "ecc2c3bf",
|
||||
"id": "d1b0876c",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -1263,7 +1263,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7aa38b8c",
|
||||
"id": "dd7e281f",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1286,7 +1286,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6cf5f0de",
|
||||
"id": "d2ac7037",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1297,7 +1297,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "3d356194",
|
||||
"id": "fd8fdb2a",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -1349,7 +1349,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ed420a20",
|
||||
"id": "ba15ae38",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -1362,7 +1362,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "4de8cf9f",
|
||||
"id": "f6f53545",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
|
||||
@@ -6,15 +6,17 @@ DATE: today
|
||||
===== Overview of week 45 =====
|
||||
|
||||
* Thursday: Boosting methods, froma AdaBoost to Gradient boosting
|
||||
* Friday: Gradient boosting and discussion of Decision trees and ensemble methods
|
||||
* Friday: Gradient boosting and discussion of Decision trees and ensemble methods. Wrapping up trees and start discussing Support Vector Machines
|
||||
|
||||
!bblock Videos
|
||||
o "Video on Decision trees":"https://www.youtube.com/watch?v=RmajweUFKvM&ab_channel=Simplilearn"
|
||||
o "Video on boosting methods by Hastie":"https://www.youtube.com/watch?v=wPqtzj5VZus&ab_channel=H2O.ai".
|
||||
o "Video on AdaBoost":"https://www.youtube.com/watch?v=LsK-xG1cLYA"
|
||||
o "Video on Gradient boost, part 1, parts 2-4 follows":"https://www.youtube.com/watch?v=3CC4N4z3GJc"
|
||||
!eblock
|
||||
|
||||
!bblock Reading
|
||||
o Add material about AdaBoost and Gradient boosting
|
||||
o "Hastie et al, chapter 10.1-10.10":"https://github.com/CompPhysics/MachineLearning/blob/master/doc/Textbooks/elementsstat.pdf"
|
||||
!eblock
|
||||
|
||||
|
||||
@@ -22,25 +24,27 @@ o Add material about AdaBoost and Gradient boosting
|
||||
===== Brief code reminder from last wekk =====
|
||||
|
||||
!bc pycod
|
||||
%matplotlib inline
|
||||
|
||||
# Common imports
|
||||
from IPython.display import Image
|
||||
from pydot import graph_from_dot_data
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
from sklearn.tree import DecisionTreeRegressor
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.tree import export_graphviz
|
||||
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
||||
from sklearn.compose import ColumnTransformer
|
||||
from IPython.display import Image
|
||||
from pydot import graph_from_dot_data
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
from sklearn.svm import SVC
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.ensemble import BaggingClassifier
|
||||
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
from sklearn.preprocessing import LabelEncoder
|
||||
from sklearn.model_selection import cross_validate
|
||||
import scikitplot as skplt
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
import os
|
||||
|
||||
# Where to save the figures and data files
|
||||
@@ -73,37 +77,31 @@ X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,ra
|
||||
print(X_train.shape)
|
||||
print(X_test.shape)
|
||||
#Scale the data
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
#define methods
|
||||
# Logistic Regression
|
||||
logreg = LogisticRegression(solver='lbfgs')
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
|
||||
# Support Vector Machine
|
||||
svm.fit(X_train_scaled, y_train)
|
||||
print("Test set accuracy SVM with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
|
||||
# Decision Trees
|
||||
deep_tree_clf = DecisionTreeClassifier(max_depth=None)
|
||||
deep_tree_clf.fit(X_train_scaled, y_train)
|
||||
print("Test set accuracy with Decision Trees and scaled data: {:.2f}".format(deep_tree_clf.score(X_test_scaled,y_test)))
|
||||
|
||||
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
from sklearn.preprocessing import LabelEncoder
|
||||
from sklearn.model_selection import cross_validate
|
||||
# Data set not specificied
|
||||
# Support Vector Machine
|
||||
svm = SVC(gamma='auto', C=100)
|
||||
svm.fit(X_train_scaled, y_train)
|
||||
print("Test set accuracy SVM with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
|
||||
# Random forests
|
||||
#Instantiate the model with 500 trees and entropy as splitting criteria
|
||||
Random_Forest_model = RandomForestClassifier(n_estimators=500,criterion="entropy")
|
||||
Random_Forest_model.fit(X_train_scaled, y_train)
|
||||
#Cross validation
|
||||
accuracy = cross_validate(Random_Forest_model,X_test_scaled,y_test,cv=10)['test_score']
|
||||
print(accuracy)
|
||||
print("Test set accuracy with Random Forests and scaled data: {:.2f}".format(Random_Forest_model.score(X_test_scaled,y_test)))
|
||||
|
||||
|
||||
import scikitplot as skplt
|
||||
|
||||
y_pred = Random_Forest_model.predict(X_test_scaled)
|
||||
skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True)
|
||||
plt.show()
|
||||
@@ -112,7 +110,6 @@ skplt.metrics.plot_roc(y_test, y_probas)
|
||||
plt.show()
|
||||
skplt.metrics.plot_cumulative_gain(y_test, y_probas)
|
||||
plt.show()
|
||||
|
||||
!ec
|
||||
|
||||
|
||||
|
||||