This commit is contained in:
Morten Hjorth-Jensen
2024-11-23 22:40:00 +01:00
parent 6da2fbe71b
commit c3fac15bef
7 changed files with 204 additions and 564 deletions
+50 -52
View File
@@ -111,7 +111,6 @@ doconce format html week48.do.txt --html_style=bootstrap --pygments_html_style=d
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
@@ -300,56 +299,55 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._week48-bs021.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs022.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs023.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs024.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs025.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs026.html#gradient-boosting-making-our-own-code-for-a-regression-case" style="font-size: 80%;">Gradient boosting, making our own code for a regression case</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs027.html#summary-of-course" style="font-size: 80%;">Summary of course</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs028.html#what-me-worry-no-final-exam-in-this-course" style="font-size: 80%;">What? Me worry? No final exam in this course!</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs029.html#topics-we-have-covered-this-year" style="font-size: 80%;">Topics we have covered this year</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs030.html#statistical-analysis-and-optimization-of-data" style="font-size: 80%;">Statistical analysis and optimization of data</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs031.html#machine-learning" style="font-size: 80%;">Machine learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs032.html#learning-outcomes-and-overarching-aims-of-this-course" style="font-size: 80%;">Learning outcomes and overarching aims of this course</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs033.html#perspective-on-machine-learning" style="font-size: 80%;">Perspective on Machine Learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs034.html#machine-learning-research" style="font-size: 80%;">Machine Learning Research</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs035.html#starting-your-machine-learning-project" style="font-size: 80%;">Starting your Machine Learning Project</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs036.html#choose-a-model-and-algorithm" style="font-size: 80%;">Choose a Model and Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs037.html#preparing-your-data" style="font-size: 80%;">Preparing Your Data</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs038.html#which-activation-and-weights-to-choose-in-neural-networks" style="font-size: 80%;">Which activation and weights to choose in neural networks</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs039.html#optimization-methods-and-hyperparameters" style="font-size: 80%;">Optimization Methods and Hyperparameters</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs040.html#resampling" style="font-size: 80%;">Resampling</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs041.html#other-courses-on-data-science-and-machine-learning-at-uio" style="font-size: 80%;">Other courses on Data science and Machine Learning at UiO</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs042.html#additional-courses-of-interest" style="font-size: 80%;">Additional courses of interest</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs043.html#what-s-the-future-like" style="font-size: 80%;">What's the future like?</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs044.html#types-of-machine-learning-a-repetition" style="font-size: 80%;">Types of Machine Learning, a repetition</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs045.html#why-boltzmann-machines" style="font-size: 80%;">Why Boltzmann machines?</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs046.html#boltzmann-machines" style="font-size: 80%;">Boltzmann Machines</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs047.html#some-similarities-and-differences-from-dnns" style="font-size: 80%;">Some similarities and differences from DNNs</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs048.html#boltzmann-machines-bm" style="font-size: 80%;">Boltzmann machines (BM)</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs049.html#a-standard-bm-setup" style="font-size: 80%;">A standard BM setup</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs050.html#the-structure-of-the-rbm-network" style="font-size: 80%;">The structure of the RBM network</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs051.html#the-network" style="font-size: 80%;">The network</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs052.html#goals" style="font-size: 80%;">Goals</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs053.html#joint-distribution" style="font-size: 80%;">Joint distribution</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs054.html#network-elements-the-energy-function" style="font-size: 80%;">Network Elements, the energy function</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs055.html#defining-different-types-of-rbms" style="font-size: 80%;">Defining different types of RBMs</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs056.html#more-about-rbms" style="font-size: 80%;">More about RBMs</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs057.html#autoencoders-overarching-view" style="font-size: 80%;">Autoencoders: Overarching view</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs058.html#bayesian-machine-learning" style="font-size: 80%;">Bayesian Machine Learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs059.html#reinforcement-learning" style="font-size: 80%;">Reinforcement Learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs060.html#transfer-learning" style="font-size: 80%;">Transfer learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs061.html#adversarial-learning" style="font-size: 80%;">Adversarial learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs062.html#dual-learning" style="font-size: 80%;">Dual learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs063.html#distributed-machine-learning" style="font-size: 80%;">Distributed machine learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs064.html#meta-learning" style="font-size: 80%;">Meta learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs065.html#the-challenges-facing-machine-learning" style="font-size: 80%;">The Challenges Facing Machine Learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs066.html#explainable-machine-learning" style="font-size: 80%;">Explainable machine learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs067.html#quantum-machine-learning" style="font-size: 80%;">Quantum machine learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs068.html#quantum-machine-learning-algorithms-based-on-linear-algebra" style="font-size: 80%;">Quantum machine learning algorithms based on linear algebra</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs069.html#quantum-reinforcement-learning" style="font-size: 80%;">Quantum reinforcement learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs070.html#quantum-deep-learning" style="font-size: 80%;">Quantum deep learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs071.html#social-machine-learning" style="font-size: 80%;">Social machine learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs072.html#the-last-words" style="font-size: 80%;">The last words?</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs073.html#best-wishes-to-you-all-and-thanks-so-much-for-your-heroic-efforts-this-semester" style="font-size: 80%;">Best wishes to you all and thanks so much for your heroic efforts this semester</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs024.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs025.html#gradient-boosting-making-our-own-code-for-a-regression-case" style="font-size: 80%;">Gradient boosting, making our own code for a regression case</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs026.html#summary-of-course" style="font-size: 80%;">Summary of course</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs027.html#what-me-worry-no-final-exam-in-this-course" style="font-size: 80%;">What? Me worry? No final exam in this course!</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs028.html#topics-we-have-covered-this-year" style="font-size: 80%;">Topics we have covered this year</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs029.html#statistical-analysis-and-optimization-of-data" style="font-size: 80%;">Statistical analysis and optimization of data</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs030.html#machine-learning" style="font-size: 80%;">Machine learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs031.html#learning-outcomes-and-overarching-aims-of-this-course" style="font-size: 80%;">Learning outcomes and overarching aims of this course</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs032.html#perspective-on-machine-learning" style="font-size: 80%;">Perspective on Machine Learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs033.html#machine-learning-research" style="font-size: 80%;">Machine Learning Research</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs034.html#starting-your-machine-learning-project" style="font-size: 80%;">Starting your Machine Learning Project</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs035.html#choose-a-model-and-algorithm" style="font-size: 80%;">Choose a Model and Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs036.html#preparing-your-data" style="font-size: 80%;">Preparing Your Data</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs037.html#which-activation-and-weights-to-choose-in-neural-networks" style="font-size: 80%;">Which activation and weights to choose in neural networks</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs038.html#optimization-methods-and-hyperparameters" style="font-size: 80%;">Optimization Methods and Hyperparameters</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs039.html#resampling" style="font-size: 80%;">Resampling</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs040.html#other-courses-on-data-science-and-machine-learning-at-uio" style="font-size: 80%;">Other courses on Data science and Machine Learning at UiO</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs041.html#additional-courses-of-interest" style="font-size: 80%;">Additional courses of interest</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs042.html#what-s-the-future-like" style="font-size: 80%;">What's the future like?</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs043.html#types-of-machine-learning-a-repetition" style="font-size: 80%;">Types of Machine Learning, a repetition</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs044.html#why-boltzmann-machines" style="font-size: 80%;">Why Boltzmann machines?</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs045.html#boltzmann-machines" style="font-size: 80%;">Boltzmann Machines</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs046.html#some-similarities-and-differences-from-dnns" style="font-size: 80%;">Some similarities and differences from DNNs</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs047.html#boltzmann-machines-bm" style="font-size: 80%;">Boltzmann machines (BM)</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs048.html#a-standard-bm-setup" style="font-size: 80%;">A standard BM setup</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs049.html#the-structure-of-the-rbm-network" style="font-size: 80%;">The structure of the RBM network</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs050.html#the-network" style="font-size: 80%;">The network</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs051.html#goals" style="font-size: 80%;">Goals</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs052.html#joint-distribution" style="font-size: 80%;">Joint distribution</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs053.html#network-elements-the-energy-function" style="font-size: 80%;">Network Elements, the energy function</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs054.html#defining-different-types-of-rbms" style="font-size: 80%;">Defining different types of RBMs</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs055.html#more-about-rbms" style="font-size: 80%;">More about RBMs</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs056.html#autoencoders-overarching-view" style="font-size: 80%;">Autoencoders: Overarching view</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs057.html#bayesian-machine-learning" style="font-size: 80%;">Bayesian Machine Learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs058.html#reinforcement-learning" style="font-size: 80%;">Reinforcement Learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs059.html#transfer-learning" style="font-size: 80%;">Transfer learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs060.html#adversarial-learning" style="font-size: 80%;">Adversarial learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs061.html#dual-learning" style="font-size: 80%;">Dual learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs062.html#distributed-machine-learning" style="font-size: 80%;">Distributed machine learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs063.html#meta-learning" style="font-size: 80%;">Meta learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs064.html#the-challenges-facing-machine-learning" style="font-size: 80%;">The Challenges Facing Machine Learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs065.html#explainable-machine-learning" style="font-size: 80%;">Explainable machine learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs066.html#quantum-machine-learning" style="font-size: 80%;">Quantum machine learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs067.html#quantum-machine-learning-algorithms-based-on-linear-algebra" style="font-size: 80%;">Quantum machine learning algorithms based on linear algebra</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs068.html#quantum-reinforcement-learning" style="font-size: 80%;">Quantum reinforcement learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs069.html#quantum-deep-learning" style="font-size: 80%;">Quantum deep learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs070.html#social-machine-learning" style="font-size: 80%;">Social machine learning</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs071.html#the-last-words" style="font-size: 80%;">The last words?</a></li>
<!-- navigation toc: --> <li><a href="._week48-bs072.html#best-wishes-to-you-all-and-thanks-so-much-for-your-heroic-efforts-this-semester" style="font-size: 80%;">Best wishes to you all and thanks so much for your heroic efforts this semester</a></li>
</ul>
</li>
@@ -401,7 +399,7 @@ MathJax.Hub.Config({
<li><a href="._week48-bs008.html">9</a></li>
<li><a href="._week48-bs009.html">10</a></li>
<li><a href="">...</a></li>
<li><a href="._week48-bs073.html">74</a></li>
<li><a href="._week48-bs072.html">73</a></li>
<li><a href="._week48-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
-76
View File
@@ -1046,7 +1046,6 @@ plt.plot(polydegree, error, label=<span style="color: #CD5555">&#39;Error&#39;</
plt.plot(polydegree, bias, label=<span style="color: #CD5555">&#39;bias&#39;</span>)
plt.plot(polydegree, variance, label=<span style="color: #CD5555">&#39;Variance&#39;</span>)
plt.legend()
save_fig(<span style="color: #CD5555">&quot;gdregression&quot;</span>)
plt.show()
</pre>
</div>
@@ -1104,14 +1103,11 @@ accuracy = cross_validate(gd_clf,X_test_scaled,y_test,cv=<span style="color: #B4
<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 = gd_clf.predict(X_test_scaled)
skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=<span style="color: #8B008B; font-weight: bold">True</span>)
save_fig(<span style="color: #CD5555">&quot;gdclassiffierconfusion&quot;</span>)
plt.show()
y_probas = gd_clf.predict_proba(X_test_scaled)
skplt.metrics.plot_roc(y_test, y_probas)
save_fig(<span style="color: #CD5555">&quot;gdclassiffierroc&quot;</span>)
plt.show()
skplt.metrics.plot_cumulative_gain(y_test, y_probas)
save_fig(<span style="color: #CD5555">&quot;gdclassiffiercgain&quot;</span>)
plt.show()
</pre>
</div>
@@ -1148,73 +1144,6 @@ sketch for efficient proposal calculation. It introduces a novel sparsity-aware
<p>It is now the algorithm which wins essentially all ML competitions!!!</p>
</section>
<section>
<h2 id="regression-case">Regression Case </h2>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<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: #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">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">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">import</span> <span style="color: #008b45; text-decoration: underline">xgboost</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">xgb</span>
<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.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> mean_squared_error
n = <span style="color: #B452CD">100</span>
maxdegree = <span style="color: #B452CD">6</span>
<span style="color: #228B22"># Make data set.</span>
x = np.linspace(-<span style="color: #B452CD">3</span>, <span style="color: #B452CD">3</span>, n).reshape(-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>)
y = np.exp(-x**<span style="color: #B452CD">2</span>) + <span style="color: #B452CD">1.5</span> * np.exp(-(x-<span style="color: #B452CD">2</span>)**<span style="color: #B452CD">2</span>)+ np.random.normal(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">0.1</span>, x.shape)
error = np.zeros(maxdegree)
bias = np.zeros(maxdegree)
variance = np.zeros(maxdegree)
polydegree = np.zeros(maxdegree)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=<span style="color: #B452CD">0.2</span>)
<span style="color: #8B008B; font-weight: bold">for</span> degree <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(maxdegree):
model = xgb.XGBRegressor(objective =<span style="color: #CD5555">&#39;reg:squarederror&#39;</span>, colsaobjective =<span style="color: #CD5555">&#39;reg:squarederror&#39;</span>, colsample_bytree = <span style="color: #B452CD">0.3</span>, learning_rate = <span style="color: #B452CD">0.1</span>,max_depth = degree, alpha = <span style="color: #B452CD">10</span>, n_estimators = <span style="color: #B452CD">200</span>)
model.fit(X_train,y_train)
y_pred = model.predict(X_test)
polydegree[degree] = degree
error[degree] = np.mean( np.mean((y_test - y_pred)**<span style="color: #B452CD">2</span>) )
bias[degree] = np.mean( (y_test - np.mean(y_pred))**<span style="color: #B452CD">2</span> )
variance[degree] = np.mean( np.var(y_pred) )
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;Max depth:&#39;</span>, degree)
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;Error:&#39;</span>, error[degree])
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;Bias^2:&#39;</span>, bias[degree])
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;Var:&#39;</span>, variance[degree])
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;{} &gt;= {} + {} = {}&#39;</span>.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
plt.xlim(<span style="color: #B452CD">1</span>,maxdegree-<span style="color: #B452CD">1</span>)
plt.plot(polydegree, error, label=<span style="color: #CD5555">&#39;Error&#39;</span>)
plt.plot(polydegree, bias, label=<span style="color: #CD5555">&#39;bias&#39;</span>)
plt.plot(polydegree, variance, label=<span style="color: #CD5555">&#39;Variance&#39;</span>)
plt.legend()
plt.show()
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 id="xgboost-on-the-cancer-data">Xgboost on the Cancer Data </h2>
@@ -1257,25 +1186,20 @@ y_test = xg_clf.predict(X_test_scaled)
<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 = xg_clf.predict(X_test_scaled)
skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=<span style="color: #8B008B; font-weight: bold">True</span>)
save_fig(<span style="color: #CD5555">&quot;xdclassiffierconfusion&quot;</span>)
plt.show()
y_probas = xg_clf.predict_proba(X_test_scaled)
skplt.metrics.plot_roc(y_test, y_probas)
save_fig(<span style="color: #CD5555">&quot;xdclassiffierroc&quot;</span>)
plt.show()
skplt.metrics.plot_cumulative_gain(y_test, y_probas)
save_fig(<span style="color: #CD5555">&quot;gdclassiffiercgain&quot;</span>)
plt.show()
xgb.plot_tree(xg_clf,num_trees=<span style="color: #B452CD">0</span>)
plt.rcParams[<span style="color: #CD5555">&#39;figure.figsize&#39;</span>] = [<span style="color: #B452CD">50</span>, <span style="color: #B452CD">10</span>]
save_fig(<span style="color: #CD5555">&quot;xgtree&quot;</span>)
plt.show()
xgb.plot_importance(xg_clf)
plt.rcParams[<span style="color: #CD5555">&#39;figure.figsize&#39;</span>] = [<span style="color: #B452CD">5</span>, <span style="color: #B452CD">5</span>]
save_fig(<span style="color: #CD5555">&quot;xgparams&quot;</span>)
plt.show()
</pre>
</div>
-77
View File
@@ -138,7 +138,6 @@ div.toc p,a {
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
@@ -1070,7 +1069,6 @@ plt.plot(polydegree, error, label=<span style="color: #CD5555">&#39;Error&#39;</
plt.plot(polydegree, bias, label=<span style="color: #CD5555">&#39;bias&#39;</span>)
plt.plot(polydegree, variance, label=<span style="color: #CD5555">&#39;Variance&#39;</span>)
plt.legend()
save_fig(<span style="color: #CD5555">&quot;gdregression&quot;</span>)
plt.show()
</pre>
</div>
@@ -1128,14 +1126,11 @@ accuracy = cross_validate(gd_clf,X_test_scaled,y_test,cv=<span style="color: #B4
<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 = gd_clf.predict(X_test_scaled)
skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=<span style="color: #8B008B; font-weight: bold">True</span>)
save_fig(<span style="color: #CD5555">&quot;gdclassiffierconfusion&quot;</span>)
plt.show()
y_probas = gd_clf.predict_proba(X_test_scaled)
skplt.metrics.plot_roc(y_test, y_probas)
save_fig(<span style="color: #CD5555">&quot;gdclassiffierroc&quot;</span>)
plt.show()
skplt.metrics.plot_cumulative_gain(y_test, y_probas)
save_fig(<span style="color: #CD5555">&quot;gdclassiffiercgain&quot;</span>)
plt.show()
</pre>
</div>
@@ -1171,73 +1166,6 @@ sketch for efficient proposal calculation. It introduces a novel sparsity-aware
<p>It is now the algorithm which wins essentially all ML competitions!!!</p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="regression-case">Regression Case </h2>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #eeeedd">
<pre style="line-height: 125%;"><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">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">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">import</span> <span style="color: #008b45; text-decoration: underline">xgboost</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">xgb</span>
<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.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> mean_squared_error
n = <span style="color: #B452CD">100</span>
maxdegree = <span style="color: #B452CD">6</span>
<span style="color: #228B22"># Make data set.</span>
x = np.linspace(-<span style="color: #B452CD">3</span>, <span style="color: #B452CD">3</span>, n).reshape(-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>)
y = np.exp(-x**<span style="color: #B452CD">2</span>) + <span style="color: #B452CD">1.5</span> * np.exp(-(x-<span style="color: #B452CD">2</span>)**<span style="color: #B452CD">2</span>)+ np.random.normal(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">0.1</span>, x.shape)
error = np.zeros(maxdegree)
bias = np.zeros(maxdegree)
variance = np.zeros(maxdegree)
polydegree = np.zeros(maxdegree)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=<span style="color: #B452CD">0.2</span>)
<span style="color: #8B008B; font-weight: bold">for</span> degree <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(maxdegree):
model = xgb.XGBRegressor(objective =<span style="color: #CD5555">&#39;reg:squarederror&#39;</span>, colsaobjective =<span style="color: #CD5555">&#39;reg:squarederror&#39;</span>, colsample_bytree = <span style="color: #B452CD">0.3</span>, learning_rate = <span style="color: #B452CD">0.1</span>,max_depth = degree, alpha = <span style="color: #B452CD">10</span>, n_estimators = <span style="color: #B452CD">200</span>)
model.fit(X_train,y_train)
y_pred = model.predict(X_test)
polydegree[degree] = degree
error[degree] = np.mean( np.mean((y_test - y_pred)**<span style="color: #B452CD">2</span>) )
bias[degree] = np.mean( (y_test - np.mean(y_pred))**<span style="color: #B452CD">2</span> )
variance[degree] = np.mean( np.var(y_pred) )
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;Max depth:&#39;</span>, degree)
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;Error:&#39;</span>, error[degree])
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;Bias^2:&#39;</span>, bias[degree])
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;Var:&#39;</span>, variance[degree])
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;{} &gt;= {} + {} = {}&#39;</span>.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
plt.xlim(<span style="color: #B452CD">1</span>,maxdegree-<span style="color: #B452CD">1</span>)
plt.plot(polydegree, error, label=<span style="color: #CD5555">&#39;Error&#39;</span>)
plt.plot(polydegree, bias, label=<span style="color: #CD5555">&#39;bias&#39;</span>)
plt.plot(polydegree, variance, label=<span style="color: #CD5555">&#39;Variance&#39;</span>)
plt.legend()
plt.show()
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="xgboost-on-the-cancer-data">Xgboost on the Cancer Data </h2>
@@ -1280,25 +1208,20 @@ y_test = xg_clf.predict(X_test_scaled)
<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 = xg_clf.predict(X_test_scaled)
skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=<span style="color: #8B008B; font-weight: bold">True</span>)
save_fig(<span style="color: #CD5555">&quot;xdclassiffierconfusion&quot;</span>)
plt.show()
y_probas = xg_clf.predict_proba(X_test_scaled)
skplt.metrics.plot_roc(y_test, y_probas)
save_fig(<span style="color: #CD5555">&quot;xdclassiffierroc&quot;</span>)
plt.show()
skplt.metrics.plot_cumulative_gain(y_test, y_probas)
save_fig(<span style="color: #CD5555">&quot;gdclassiffiercgain&quot;</span>)
plt.show()
xgb.plot_tree(xg_clf,num_trees=<span style="color: #B452CD">0</span>)
plt.rcParams[<span style="color: #CD5555">&#39;figure.figsize&#39;</span>] = [<span style="color: #B452CD">50</span>, <span style="color: #B452CD">10</span>]
save_fig(<span style="color: #CD5555">&quot;xgtree&quot;</span>)
plt.show()
xgb.plot_importance(xg_clf)
plt.rcParams[<span style="color: #CD5555">&#39;figure.figsize&#39;</span>] = [<span style="color: #B452CD">5</span>, <span style="color: #B452CD">5</span>]
save_fig(<span style="color: #CD5555">&quot;xgparams&quot;</span>)
plt.show()
</pre>
</div>
-77
View File
@@ -215,7 +215,6 @@ div.toc p,a {
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
@@ -1147,7 +1146,6 @@ plt<span style="color: #666666">.</span>plot(polydegree, error, label<span style
plt<span style="color: #666666">.</span>plot(polydegree, bias, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;bias&#39;</span>)
plt<span style="color: #666666">.</span>plot(polydegree, variance, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;Variance&#39;</span>)
plt<span style="color: #666666">.</span>legend()
save_fig(<span style="color: #BA2121">&quot;gdregression&quot;</span>)
plt<span style="color: #666666">.</span>show()
</pre>
</div>
@@ -1205,14 +1203,11 @@ accuracy <span style="color: #666666">=</span> cross_validate(gd_clf,X_test_scal
<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> gd_clf<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>)
save_fig(<span style="color: #BA2121">&quot;gdclassiffierconfusion&quot;</span>)
plt<span style="color: #666666">.</span>show()
y_probas <span style="color: #666666">=</span> gd_clf<span style="color: #666666">.</span>predict_proba(X_test_scaled)
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_roc(y_test, y_probas)
save_fig(<span style="color: #BA2121">&quot;gdclassiffierroc&quot;</span>)
plt<span style="color: #666666">.</span>show()
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_cumulative_gain(y_test, y_probas)
save_fig(<span style="color: #BA2121">&quot;gdclassiffiercgain&quot;</span>)
plt<span style="color: #666666">.</span>show()
</pre>
</div>
@@ -1248,73 +1243,6 @@ sketch for efficient proposal calculation. It introduces a novel sparsity-aware
<p>It is now the algorithm which wins essentially all ML competitions!!!</p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="regression-case">Regression Case </h2>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><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">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">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">import</span> <span style="color: #0000FF; font-weight: bold">xgboost</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">xgb</span>
<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.metrics</span> <span style="color: #008000; font-weight: bold">import</span> mean_squared_error
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
maxdegree <span style="color: #666666">=</span> <span style="color: #666666">6</span>
<span style="color: #408080; font-style: italic"># Make data set.</span>
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">-3</span>, <span style="color: #666666">3</span>, n)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>x<span style="color: #666666">**2</span>) <span style="color: #666666">+</span> <span style="color: #666666">1.5</span> <span style="color: #666666">*</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(x<span style="color: #666666">-2</span>)<span style="color: #666666">**2</span>)<span style="color: #666666">+</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(<span style="color: #666666">0</span>, <span style="color: #666666">0.1</span>, x<span style="color: #666666">.</span>shape)
error <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
bias <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
variance <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
polydegree <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(x, y, test_size<span style="color: #666666">=0.2</span>)
<span style="color: #008000; font-weight: bold">for</span> degree <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(maxdegree):
model <span style="color: #666666">=</span> xgb<span style="color: #666666">.</span>XGBRegressor(objective <span style="color: #666666">=</span><span style="color: #BA2121">&#39;reg:squarederror&#39;</span>, colsaobjective <span style="color: #666666">=</span><span style="color: #BA2121">&#39;reg:squarederror&#39;</span>, colsample_bytree <span style="color: #666666">=</span> <span style="color: #666666">0.3</span>, learning_rate <span style="color: #666666">=</span> <span style="color: #666666">0.1</span>,max_depth <span style="color: #666666">=</span> degree, alpha <span style="color: #666666">=</span> <span style="color: #666666">10</span>, n_estimators <span style="color: #666666">=</span> <span style="color: #666666">200</span>)
model<span style="color: #666666">.</span>fit(X_train,y_train)
y_pred <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(X_test)
polydegree[degree] <span style="color: #666666">=</span> degree
error[degree] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( np<span style="color: #666666">.</span>mean((y_test <span style="color: #666666">-</span> y_pred)<span style="color: #666666">**2</span>) )
bias[degree] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( (y_test <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(y_pred))<span style="color: #666666">**2</span> )
variance[degree] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( np<span style="color: #666666">.</span>var(y_pred) )
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Max depth:&#39;</span>, degree)
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Error:&#39;</span>, error[degree])
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Bias^2:&#39;</span>, bias[degree])
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Var:&#39;</span>, variance[degree])
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> &gt;= </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> + </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> = </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">&#39;</span><span style="color: #666666">.</span>format(error[degree], bias[degree], variance[degree], bias[degree]<span style="color: #666666">+</span>variance[degree]))
plt<span style="color: #666666">.</span>xlim(<span style="color: #666666">1</span>,maxdegree<span style="color: #666666">-1</span>)
plt<span style="color: #666666">.</span>plot(polydegree, error, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;Error&#39;</span>)
plt<span style="color: #666666">.</span>plot(polydegree, bias, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;bias&#39;</span>)
plt<span style="color: #666666">.</span>plot(polydegree, variance, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;Variance&#39;</span>)
plt<span style="color: #666666">.</span>legend()
plt<span style="color: #666666">.</span>show()
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="xgboost-on-the-cancer-data">Xgboost on the Cancer Data </h2>
@@ -1357,25 +1285,20 @@ y_test <span style="color: #666666">=</span> xg_clf<span style="color: #666666">
<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> xg_clf<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>)
save_fig(<span style="color: #BA2121">&quot;xdclassiffierconfusion&quot;</span>)
plt<span style="color: #666666">.</span>show()
y_probas <span style="color: #666666">=</span> xg_clf<span style="color: #666666">.</span>predict_proba(X_test_scaled)
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_roc(y_test, y_probas)
save_fig(<span style="color: #BA2121">&quot;xdclassiffierroc&quot;</span>)
plt<span style="color: #666666">.</span>show()
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_cumulative_gain(y_test, y_probas)
save_fig(<span style="color: #BA2121">&quot;gdclassiffiercgain&quot;</span>)
plt<span style="color: #666666">.</span>show()
xgb<span style="color: #666666">.</span>plot_tree(xg_clf,num_trees<span style="color: #666666">=0</span>)
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;figure.figsize&#39;</span>] <span style="color: #666666">=</span> [<span style="color: #666666">50</span>, <span style="color: #666666">10</span>]
save_fig(<span style="color: #BA2121">&quot;xgtree&quot;</span>)
plt<span style="color: #666666">.</span>show()
xgb<span style="color: #666666">.</span>plot_importance(xg_clf)
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;figure.figsize&#39;</span>] <span style="color: #666666">=</span> [<span style="color: #666666">5</span>, <span style="color: #666666">5</span>]
save_fig(<span style="color: #BA2121">&quot;xgparams&quot;</span>)
plt<span style="color: #666666">.</span>show()
</pre>
</div>
Binary file not shown.
File diff suppressed because it is too large Load Diff
-56
View File
@@ -686,7 +686,6 @@ plt.plot(polydegree, error, label='Error')
plt.plot(polydegree, bias, label='bias')
plt.plot(polydegree, variance, label='Variance')
plt.legend()
save_fig("gdregression")
plt.show()
!ec
@@ -725,14 +724,11 @@ print("Test set accuracy with Gradient boosting and scaled data: {:.2f}".format(
import scikitplot as skplt
y_pred = gd_clf.predict(X_test_scaled)
skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True)
save_fig("gdclassiffierconfusion")
plt.show()
y_probas = gd_clf.predict_proba(X_test_scaled)
skplt.metrics.plot_roc(y_test, y_probas)
save_fig("gdclassiffierroc")
plt.show()
skplt.metrics.plot_cumulative_gain(y_test, y_probas)
save_fig("gdclassiffiercgain")
plt.show()
!ec
@@ -754,53 +750,6 @@ sketch for efficient proposal calculation. It introduces a novel sparsity-aware
It is now the algorithm which wins essentially all ML competitions!!!
!split
===== Regression Case =====
!bc pycod
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
import xgboost as xgb
import scikitplot as skplt
from sklearn.metrics import mean_squared_error
n = 100
maxdegree = 6
# Make data set.
x = np.linspace(-3, 3, n).reshape(-1, 1)
y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
error = np.zeros(maxdegree)
bias = np.zeros(maxdegree)
variance = np.zeros(maxdegree)
polydegree = np.zeros(maxdegree)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
for degree in range(maxdegree):
model = xgb.XGBRegressor(objective ='reg:squarederror', colsaobjective ='reg:squarederror', colsample_bytree = 0.3, learning_rate = 0.1,max_depth = degree, alpha = 10, n_estimators = 200)
model.fit(X_train,y_train)
y_pred = model.predict(X_test)
polydegree[degree] = degree
error[degree] = np.mean( np.mean((y_test - y_pred)**2) )
bias[degree] = np.mean( (y_test - np.mean(y_pred))**2 )
variance[degree] = np.mean( np.var(y_pred) )
print('Max depth:', degree)
print('Error:', error[degree])
print('Bias^2:', bias[degree])
print('Var:', variance[degree])
print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
plt.xlim(1,maxdegree-1)
plt.plot(polydegree, error, label='Error')
plt.plot(polydegree, bias, label='bias')
plt.plot(polydegree, variance, label='Variance')
plt.legend()
plt.show()
!ec
!split
===== Xgboost on the Cancer Data =====
@@ -839,25 +788,20 @@ print("Test set accuracy with Gradient Boosting and scaled data: {:.2f}".format(
import scikitplot as skplt
y_pred = xg_clf.predict(X_test_scaled)
skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True)
save_fig("xdclassiffierconfusion")
plt.show()
y_probas = xg_clf.predict_proba(X_test_scaled)
skplt.metrics.plot_roc(y_test, y_probas)
save_fig("xdclassiffierroc")
plt.show()
skplt.metrics.plot_cumulative_gain(y_test, y_probas)
save_fig("gdclassiffiercgain")
plt.show()
xgb.plot_tree(xg_clf,num_trees=0)
plt.rcParams['figure.figsize'] = [50, 10]
save_fig("xgtree")
plt.show()
xgb.plot_importance(xg_clf)
plt.rcParams['figure.figsize'] = [5, 5]
save_fig("xgparams")
plt.show()
!ec