<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>xgboost | DataWim</title><link>https://www.datawim.com/tag/xgboost/</link><atom:link href="https://www.datawim.com/tag/xgboost/index.xml" rel="self" type="application/rss+xml"/><description>xgboost</description><generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><lastBuildDate>Fri, 31 Dec 2021 00:00:00 +0000</lastBuildDate><image><url>https://www.datawim.com/media/icon_hua2ec155b4296a9c9791d015323e16eb5_11927_512x512_fill_lanczos_center_3.png</url><title>xgboost</title><link>https://www.datawim.com/tag/xgboost/</link></image><item><title>Crop Recommendation based on Weather variables and Fertilizers</title><link>https://www.datawim.com/post/crop-recommendation-based-on-weather-fertilizers/</link><pubDate>Fri, 31 Dec 2021 00:00:00 +0000</pubDate><guid>https://www.datawim.com/post/crop-recommendation-based-on-weather-fertilizers/</guid><description>&lt;p>&lt;a href="Crop_recommendation.csv">Download the tutorial CSV&lt;/a> · &lt;a href="https://www.datawim.com/downloads/crop-recommendation-based-on-weather-fertilizers/index.en.Rmd" download>Download the R Markdown source&lt;/a>
&lt;/p>
&lt;p>In this blog post, I&amp;rsquo;ll show that how we can use different classification algorithms to perform crop recommendation based on weather variables such as Temperature, Humidity and Rainfall, and fertilizers (Nitrogen, Phosphorus and Potassium). For the classification purpose, I will use &lt;a href="https://www.kaggle.com/atharvaingle/crop-recommendation-dataset" target="_blank" rel="noopener">crop data&lt;/a> for only the agronomic and commercially grown crops. The data can be downloaded using this &lt;a href="https://www.kaggle.com/atharvaingle/crop-recommendation-dataset" target="_blank" rel="noopener">link&lt;/a>.&lt;/p>
&lt;p>At first, I&amp;rsquo;ll run some exploratory data analysis (EDA) to understand patterns and trends in the data set and then will move towards data analysis. In this post, I&amp;rsquo;ll use &lt;code>random forest&lt;/code>, &lt;code>support vector machine&lt;/code> and &lt;code>Xgboost&lt;/code> models in the &lt;code>tidymodels&lt;/code> framework to recommend crops based on weather variables and fertilizers.&lt;/p>
&lt;h2 id="load-libraries">Load libraries&lt;/h2>
&lt;pre>&lt;code class="language-r"># Data science
library(tidyverse)
library(janitor)
library(DataExplorer)
library(skimr)
# Modeling
library(tidymodels)
# Visualization
library(ggrepel)
library(GGally)
library(vip)
library(patchwork)
# Tables
library(gt)
&lt;/code>&lt;/pre>
&lt;h2 id="upload-data">Upload data&lt;/h2>
&lt;pre>&lt;code class="language-r">crop &amp;lt;- read_csv(&amp;quot;Crop_recommendation.csv&amp;quot;)
&lt;/code>&lt;/pre>
&lt;h2 id="exploratory-data-analysis">Exploratory data analysis&lt;/h2>
&lt;p>Exploratory data analysis (EDA) is vital in swiftly understanding the data structure and patterns in it. I&amp;rsquo;ll demonstrate different steps in EDA.&lt;/p>
&lt;h2 id="data-structure">Data structure&lt;/h2>
&lt;ul>
&lt;li>In this step of understanding data structure, I&amp;rsquo;ll use the &lt;code>skim&lt;/code> package that provides a very handy &lt;code>skimr&lt;/code> function that can quickly provide an abstract summary of the data.&lt;/li>
&lt;/ul>
&lt;pre>&lt;code class="language-r">skim(crop)
&lt;/code>&lt;/pre>
&lt;ul>
&lt;li>The data contain information of 7 unique features for 22 unique crops.&lt;/li>
&lt;li>Now, I&amp;rsquo;ll explore these features in general and by each crop.&lt;/li>
&lt;/ul>
&lt;h2 id="numeric-variables">Numeric variables&lt;/h2>
&lt;pre>&lt;code class="language-r">crop %&amp;gt;%
plot_histogram(ggtheme = theme_bw(), geom_histogram_args = list(fill = &amp;quot;steelblue&amp;quot;))
&lt;/code>&lt;/pre>
&lt;h2 id="nominal-variables">Nominal variables&lt;/h2>
&lt;pre>&lt;code class="language-r">crop %&amp;gt;%
plot_bar(ggtheme = theme_bw())
&lt;/code>&lt;/pre>
&lt;ul>
&lt;li>Each crop has around 100 observations.&lt;/li>
&lt;/ul>
&lt;h2 id="agronomic-crops">Agronomic crops&lt;/h2>
&lt;p>Now, I&amp;rsquo;ll filter the data for the agronomic crops or commercially grown crops only. These crops include:&lt;/p>
&lt;pre>&lt;code>1. Chickpea
2. Coffee
3. Cotton
4. Lentil
5. Maize
6. Rice
&lt;/code>&lt;/pre>
&lt;pre>&lt;code class="language-r">agro &amp;lt;- crop %&amp;gt;%
filter(label %in% c(&amp;quot;chickpea&amp;quot;, &amp;quot;coffee&amp;quot;, &amp;quot;cotton&amp;quot;, &amp;quot;lentil&amp;quot;, &amp;quot;maize&amp;quot;, &amp;quot;rice&amp;quot;)) %&amp;gt;%
mutate(label = factor(label))
&lt;/code>&lt;/pre>
&lt;h2 id="features-distribution">Features distribution&lt;/h2>
&lt;pre>&lt;code class="language-r">agro %&amp;gt;%
pivot_longer(N:rainfall, names_to = &amp;quot;Feature&amp;quot;, values_to = &amp;quot;value&amp;quot;) %&amp;gt;%
ggplot(aes(x = value)) +
geom_histogram(fill = &amp;quot;steelblue&amp;quot;) +
labs(x = &amp;quot;Value&amp;quot;, y = &amp;quot;Count&amp;quot;, fill = NULL) +
theme_bw() +
facet_wrap(~Feature, scales = &amp;quot;free&amp;quot;)
&lt;/code>&lt;/pre>
&lt;h4 id="by-crop">By Crop&lt;/h4>
&lt;pre>&lt;code class="language-r">agro %&amp;gt;%
pivot_longer(N:rainfall, names_to = &amp;quot;Feature&amp;quot;, values_to = &amp;quot;value&amp;quot;) %&amp;gt;%
ggplot(aes(x = value, fill = label)) +
geom_histogram(alpha = 0.65) +
labs(x = &amp;quot;Value&amp;quot;, y = &amp;quot;Count&amp;quot;, fill = NULL) +
theme_test() +
facet_wrap(~Feature, scales = &amp;quot;free&amp;quot;)
&lt;/code>&lt;/pre>
&lt;ul>
&lt;li>Histograms clearly reveals that distributions of weather and fertilizer variables are quite different for each crop.&lt;/li>
&lt;/ul>
&lt;h2 id="density-charts">Density charts&lt;/h2>
&lt;pre>&lt;code class="language-r">agro %&amp;gt;%
pivot_longer(N:rainfall, names_to = &amp;quot;Feature&amp;quot;, values_to = &amp;quot;value&amp;quot;) %&amp;gt;%
ggplot(aes(x = value, fill = label)) +
geom_density(alpha = 0.5) +
labs(fill = NULL) +
theme_test() +
facet_wrap(~Feature, scales = &amp;quot;free&amp;quot;)
&lt;/code>&lt;/pre>
&lt;h2 id="box-plots">Box plots&lt;/h2>
&lt;pre>&lt;code class="language-r">agro %&amp;gt;%
pivot_longer(N:rainfall, names_to = &amp;quot;Feature&amp;quot;, values_to = &amp;quot;value&amp;quot;) %&amp;gt;%
ggplot(aes(x = label, y = value)) +
geom_boxplot() +
labs(x = &amp;quot;Population&amp;quot;, y = &amp;quot;Count&amp;quot;, fill = NULL) +
theme_bw() +
coord_flip() +
facet_wrap(~Feature, scales = &amp;quot;free_x&amp;quot;, ncol = 4) +
theme(legend.position = &amp;quot;top&amp;quot;)
&lt;/code>&lt;/pre>
&lt;h2 id="correlation-between-features">Correlation between features&lt;/h2>
&lt;h2 id="pooled-data">Pooled data&lt;/h2>
&lt;ul>
&lt;li>The features are significantly correlated in the pooled data.&lt;/li>
&lt;/ul>
&lt;pre>&lt;code class="language-r">agro %&amp;gt;%
ggpairs(columns = 1:7)
&lt;/code>&lt;/pre>
&lt;h2 id="by-crop-1">By crop&lt;/h2>
&lt;ul>
&lt;li>When the features were grouped by crop type, the significance of the relationship between features has been diminished.&lt;/li>
&lt;/ul>
&lt;pre>&lt;code class="language-r">agro %&amp;gt;%
ggpairs(columns = 1:7, ggplot2::aes(colour=label))
&lt;/code>&lt;/pre>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img alt="Pairwise relationships between weather and soil variables by crop" srcset="
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-12-1_hu096374e04f188ce7d4b8feab4fe2c560_1068044_84bbefff101d0566917abbc332181270.png 400w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-12-1_hu096374e04f188ce7d4b8feab4fe2c560_1068044_a821d95c418314ee8267580ab3bcb10a.png 760w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-12-1_hu096374e04f188ce7d4b8feab4fe2c560_1068044_1200x1200_fit_lanczos_3.png 1200w"
src="https://www.datawim.com/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-12-1_hu096374e04f188ce7d4b8feab4fe2c560_1068044_84bbefff101d0566917abbc332181270.png"
width="760"
height="760"
loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;ul>
&lt;li>Clusters of features are clearly visible which means that there is highly likely that the majority of crops will be accurately classified.&lt;/li>
&lt;/ul>
&lt;h2 id="data-analysis">Data Analysis&lt;/h2>
&lt;h2 id="data-splitting-and-resampling">Data splitting and Resampling&lt;/h2>
&lt;pre>&lt;code class="language-r">set.seed(12345)
# split the data into trainng (75%) and testing (25%)
agro_split &amp;lt;- initial_split(agro, prop = 3/4)
agro_split
# extract training and testing sets
agro_train &amp;lt;- training(agro_split)
agro_test &amp;lt;- testing(agro_split)
&lt;/code>&lt;/pre>
&lt;h3 id="splitting-data-for-cross-validation">Splitting data for Cross Validation&lt;/h3>
&lt;pre>&lt;code class="language-r"># create CV object from training data
agro_cv &amp;lt;- vfold_cv(agro_train)
&lt;/code>&lt;/pre>
&lt;h2 id="define-the-recipe">Define the recipe&lt;/h2>
&lt;pre>&lt;code class="language-r"># define the recipe
agro_recipe &amp;lt;- recipe(label ~ ., data = agro) %&amp;gt;%
step_normalize(all_numeric())
&lt;/code>&lt;/pre>
&lt;h2 id="random-forest">Random Forest&lt;/h2>
&lt;pre>&lt;code class="language-r">rf_model &amp;lt;-
rand_forest() %&amp;gt;%
set_args(mtry = tune()) %&amp;gt;%
set_engine(&amp;quot;ranger&amp;quot;, importance = &amp;quot;permutation&amp;quot;) %&amp;gt;%
set_mode(&amp;quot;classification&amp;quot;)
rf_model
# show what will be tuned
rf_model %&amp;gt;%
parameters()
&lt;/code>&lt;/pre>
&lt;h2 id="workflow">Workflow&lt;/h2>
&lt;pre>&lt;code class="language-r">rf_workflow &amp;lt;- workflow() %&amp;gt;%
add_model(rf_model) %&amp;gt;%
add_recipe(agro_recipe)
&lt;/code>&lt;/pre>
&lt;h2 id="model-tuning">Model tuning&lt;/h2>
&lt;pre>&lt;code class="language-r"># specify which values want to try
rf_grid &amp;lt;- expand.grid(mtry = c(2, 3, 4, 5, 6))
# extract results
rf_tune_results &amp;lt;- rf_workflow %&amp;gt;%
tune_grid(resamples = agro_cv, #CV object
grid = rf_grid, # grid of values to try
metrics = metric_set(accuracy, precision, recall) # metrics we care about
)
&lt;/code>&lt;/pre>
&lt;h4 id="model-metrics">Model metrics&lt;/h4>
&lt;pre>&lt;code class="language-r"># print results
rf_tune_results %&amp;gt;%
collect_metrics()
# Accuracy
rf_tune_results %&amp;gt;%
show_best(metric = &amp;quot;accuracy&amp;quot;)
# Plotting metrics
autoplot(rf_tune_results)
&lt;/code>&lt;/pre>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img alt="Random forest cross-validation metrics" srcset="
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-19-1_huf133dddd025213378bf4da86db9fd0df_114470_bf7bbc00aea97c171f482849849de9c5.png 400w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-19-1_huf133dddd025213378bf4da86db9fd0df_114470_80f9dd79c822e0887dee39b98f889f26.png 760w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-19-1_huf133dddd025213378bf4da86db9fd0df_114470_1200x1200_fit_lanczos_3.png 1200w"
src="https://www.datawim.com/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-19-1_huf133dddd025213378bf4da86db9fd0df_114470_bf7bbc00aea97c171f482849849de9c5.png"
width="760"
height="543"
loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;h3 id="finalize-the-workflow">Finalize the workflow&lt;/h3>
&lt;h4 id="select-the-best-model">Select the best model&lt;/h4>
&lt;ul>
&lt;li>I chose the best model based on accuracy.&lt;/li>
&lt;/ul>
&lt;pre>&lt;code class="language-r">param_final &amp;lt;- rf_tune_results %&amp;gt;%
select_best(metric = &amp;quot;accuracy&amp;quot;)
param_final
&lt;/code>&lt;/pre>
&lt;h4 id="final-model">Final model&lt;/h4>
&lt;pre>&lt;code class="language-r">rf_workflow &amp;lt;- rf_workflow %&amp;gt;%
finalize_workflow(param_final)
&lt;/code>&lt;/pre>
&lt;h3 id="model-testing">Model testing&lt;/h3>
&lt;pre>&lt;code class="language-r"># fit on the training set and evaluate on test set
rf_fit &amp;lt;- rf_workflow %&amp;gt;%
last_fit(agro_split)
# Metrics
test_performance &amp;lt;- rf_fit %&amp;gt;%
collect_metrics()
test_performance
&lt;/code>&lt;/pre>
&lt;ul>
&lt;li>Model is predicting crops with high accuracy.&lt;/li>
&lt;/ul>
&lt;h4 id="generate-predictions-from-the-test-set">Generate predictions from the test set&lt;/h4>
&lt;pre>&lt;code class="language-r">test_predictions &amp;lt;- rf_fit %&amp;gt;%
collect_predictions()
test_predictions
&lt;/code>&lt;/pre>
&lt;h4 id="confusion-matrix">Confusion matrix&lt;/h4>
&lt;pre>&lt;code class="language-r">cm_rf &amp;lt;- test_predictions %&amp;gt;%
conf_mat(truth = label, estimate = .pred_class)
# Plot the confusion matrix
p1 &amp;lt;- autoplot(cm_rf, type = &amp;quot;heatmap&amp;quot;) +
scale_fill_gradient(low=&amp;quot;#D6EAF8&amp;quot;,high = &amp;quot;#2E86C1&amp;quot;) +
labs(title = &amp;quot;Random Forest&amp;quot;) +
theme(plot.title = element_text(hjust = 0.5))
p1
&lt;/code>&lt;/pre>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img alt="Random forest confusion matrix on the test data" srcset="
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-24-1_hud465c49cec7f7aa9b8c275e795925998_105753_d0515d7ef67db9d5adf5726ab3b8e7a1.png 400w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-24-1_hud465c49cec7f7aa9b8c275e795925998_105753_6499be520d13429054762c88ea4dd84f.png 760w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-24-1_hud465c49cec7f7aa9b8c275e795925998_105753_1200x1200_fit_lanczos_3.png 1200w"
src="https://www.datawim.com/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-24-1_hud465c49cec7f7aa9b8c275e795925998_105753_d0515d7ef67db9d5adf5726ab3b8e7a1.png"
width="760"
height="543"
loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;h3 id="fitting-and-using-final-model">Fitting and using final model&lt;/h3>
&lt;pre>&lt;code class="language-r">final_model &amp;lt;- fit(rf_workflow, agro)
final_model
&lt;/code>&lt;/pre>
&lt;h3 id="variable-importance">Variable importance&lt;/h3>
&lt;pre>&lt;code class="language-r">rf_fit %&amp;gt;%
pluck(&amp;quot;.workflow&amp;quot;, 1) %&amp;gt;%
extract_fit_parsnip() %&amp;gt;%
vip(aesthetics = list(fill = &amp;quot;steelblue&amp;quot;)) +
theme_minimal()
&lt;/code>&lt;/pre>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img alt="Random forest variable importance" srcset="
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-26-1_hu2dd1d881fe020d31dc86adb7c9e81a89_67917_4dd02c6828d83c77913bb076b65b8bbd.png 400w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-26-1_hu2dd1d881fe020d31dc86adb7c9e81a89_67917_8270cc192ca732ce9ba1f4d630e01473.png 760w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-26-1_hu2dd1d881fe020d31dc86adb7c9e81a89_67917_1200x1200_fit_lanczos_3.png 1200w"
src="https://www.datawim.com/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-26-1_hu2dd1d881fe020d31dc86adb7c9e81a89_67917_4dd02c6828d83c77913bb076b65b8bbd.png"
width="760"
height="543"
loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;ul>
&lt;li>Both humidity and rainfall are highly important when classifying agronomic crops.&lt;/li>
&lt;/ul>
&lt;h2 id="support-vector-machine">Support Vector Machine&lt;/h2>
&lt;pre>&lt;code class="language-r">## SVM
svm_model &amp;lt;-
svm_poly() %&amp;gt;%
set_engine(&amp;quot;kernlab&amp;quot;) %&amp;gt;%
set_mode(&amp;quot;classification&amp;quot;)
svm_model
# show what will be tuned
svm_model %&amp;gt;%
parameters()
&lt;/code>&lt;/pre>
&lt;h3 id="workflow-1">Workflow&lt;/h3>
&lt;pre>&lt;code class="language-r">svm_workflow &amp;lt;- workflow() %&amp;gt;%
add_model(svm_model) %&amp;gt;%
add_recipe(agro_recipe)
&lt;/code>&lt;/pre>
&lt;h3 id="model-testing-1">Model testing&lt;/h3>
&lt;pre>&lt;code class="language-r"># fit on the training set and evaluate on test set
svm_fit &amp;lt;- svm_workflow %&amp;gt;%
last_fit(agro_split)
&lt;/code>&lt;/pre>
&lt;h4 id="model-metrics-1">Model metrics&lt;/h4>
&lt;pre>&lt;code class="language-r">svm_performance &amp;lt;- svm_fit %&amp;gt;%
collect_metrics()
svm_performance
&lt;/code>&lt;/pre>
&lt;ul>
&lt;li>Model is predicting crops with high accuracy.&lt;/li>
&lt;/ul>
&lt;h4 id="generate-predictions-from-the-test-set-1">Generate predictions from the test set&lt;/h4>
&lt;pre>&lt;code class="language-r">test_predictions &amp;lt;- svm_fit %&amp;gt;%
collect_predictions()
test_predictions
&lt;/code>&lt;/pre>
&lt;h4 id="confusion-matrix-1">Confusion matrix&lt;/h4>
&lt;pre>&lt;code class="language-r">cm_svm &amp;lt;- test_predictions %&amp;gt;%
conf_mat(truth = label, estimate = .pred_class)
# Plot the confusion matrix
p2 &amp;lt;- autoplot(cm_svm, type = &amp;quot;heatmap&amp;quot;) +
scale_fill_gradient(low=&amp;quot;#D6EAF8&amp;quot;,high = &amp;quot;#2E86C1&amp;quot;) +
labs(title = &amp;quot;Support Vector Machine&amp;quot;) +
theme(plot.title = element_text(hjust = 0.5))
p2
&lt;/code>&lt;/pre>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img alt="Support vector machine confusion matrix on the test data" srcset="
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-32-1_hu6cafe720ab41715bb45595d7aad743d0_109712_e773a73c1303ba9aef241f04cc8f4f87.png 400w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-32-1_hu6cafe720ab41715bb45595d7aad743d0_109712_85b2ab89b09921a52315873419285a50.png 760w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-32-1_hu6cafe720ab41715bb45595d7aad743d0_109712_1200x1200_fit_lanczos_3.png 1200w"
src="https://www.datawim.com/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-32-1_hu6cafe720ab41715bb45595d7aad743d0_109712_e773a73c1303ba9aef241f04cc8f4f87.png"
width="760"
height="543"
loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;h3 id="fitting-and-using-final-model-1">Fitting and using final model&lt;/h3>
&lt;pre>&lt;code class="language-r">final_model &amp;lt;- fit(svm_workflow, agro)
final_model
&lt;/code>&lt;/pre>
&lt;h2 id="xgboost">XGBoost&lt;/h2>
&lt;pre>&lt;code class="language-r">xgb_model &amp;lt;- boost_tree() %&amp;gt;%
set_args(mtry = tune()) %&amp;gt;%
set_engine(&amp;quot;xgboost&amp;quot;) %&amp;gt;%
set_mode(&amp;quot;classification&amp;quot;)
xgb_model
# show what will be tuned
xgb_model %&amp;gt;%
parameters()
&lt;/code>&lt;/pre>
&lt;h3 id="workflow-2">Workflow&lt;/h3>
&lt;pre>&lt;code class="language-r">xgb_workflow &amp;lt;- workflow() %&amp;gt;%
add_formula(label ~ .) %&amp;gt;%
add_model(xgb_model)
xgb_workflow
&lt;/code>&lt;/pre>
&lt;h3 id="model-tuning-1">Model tuning&lt;/h3>
&lt;pre>&lt;code class="language-r">xgb_grid &amp;lt;- grid_latin_hypercube(
finalize(mtry(), agro_train),
size = 30
)
xgb_tune_results &amp;lt;- tune_grid(
xgb_workflow,
resamples = agro_cv,
grid = xgb_grid,
metrics = metric_set(accuracy, precision, recall) # metrics we care about
)
&lt;/code>&lt;/pre>
&lt;h4 id="model-metrics-2">Model metrics&lt;/h4>
&lt;pre>&lt;code class="language-r"># print results
xgb_tune_results %&amp;gt;%
collect_metrics()
# Accuracy
xgb_tune_results %&amp;gt;%
show_best(metric = &amp;quot;accuracy&amp;quot;)
# Plotting metrics
autoplot(xgb_tune_results)
&lt;/code>&lt;/pre>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img alt="XGBoost cross-validation metrics" srcset="
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-37-1_hu2f52d54b6e644c8b68eb0a8398575334_113066_b4e4d017dc35f067e7a45809c31adb14.png 400w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-37-1_hu2f52d54b6e644c8b68eb0a8398575334_113066_340005546315e3232c1e5009d630c24b.png 760w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-37-1_hu2f52d54b6e644c8b68eb0a8398575334_113066_1200x1200_fit_lanczos_3.png 1200w"
src="https://www.datawim.com/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-37-1_hu2f52d54b6e644c8b68eb0a8398575334_113066_b4e4d017dc35f067e7a45809c31adb14.png"
width="760"
height="543"
loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;h3 id="finalize-the-workflow-1">Finalize the workflow&lt;/h3>
&lt;h4 id="select-the-best-model-1">Select the best model&lt;/h4>
&lt;pre>&lt;code class="language-r">param_final &amp;lt;- xgb_tune_results %&amp;gt;%
select_best(metric = &amp;quot;accuracy&amp;quot;)
param_final
&lt;/code>&lt;/pre>
&lt;h4 id="final-model-1">Final model&lt;/h4>
&lt;pre>&lt;code class="language-r">xgb_workflow &amp;lt;- xgb_workflow %&amp;gt;%
finalize_workflow(param_final)
&lt;/code>&lt;/pre>
&lt;h3 id="model-testing-2">Model testing&lt;/h3>
&lt;pre>&lt;code class="language-r"># fit on the training set and evaluate on test set
xgb_fit &amp;lt;- xgb_workflow %&amp;gt;%
last_fit(agro_split)
# Metrics
test_performance &amp;lt;- xgb_fit %&amp;gt;%
collect_metrics()
test_performance
&lt;/code>&lt;/pre>
&lt;ul>
&lt;li>Model is predicting crops with high accuracy.&lt;/li>
&lt;/ul>
&lt;h4 id="generate-predictions-from-the-test-set-2">Generate predictions from the test set&lt;/h4>
&lt;pre>&lt;code class="language-r">test_predictions &amp;lt;- xgb_fit %&amp;gt;%
collect_predictions()
test_predictions
&lt;/code>&lt;/pre>
&lt;h4 id="confusion-matrix-2">Confusion matrix&lt;/h4>
&lt;pre>&lt;code class="language-r">cm_xgb &amp;lt;- test_predictions %&amp;gt;%
conf_mat(truth = label, estimate = .pred_class)
# Plot the confusion matrix
p3 &amp;lt;- autoplot(cm_xgb, type = &amp;quot;heatmap&amp;quot;) +
scale_fill_gradient(low=&amp;quot;#D6EAF8&amp;quot;,high = &amp;quot;#2E86C1&amp;quot;) +
labs(title = &amp;quot;XGBoost&amp;quot;) +
theme(plot.title = element_text(hjust = 0.5))
p3
&lt;/code>&lt;/pre>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img alt="XGBoost confusion matrix on the test data" srcset="
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-42-1_huebfae9aa9782b06fa1960fb8064541ea_106434_d2cbfc9a36de87de5d63d5d698e37dc7.png 400w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-42-1_huebfae9aa9782b06fa1960fb8064541ea_106434_988a87d566a3b4f8b5ccc82b6c895887.png 760w,
/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-42-1_huebfae9aa9782b06fa1960fb8064541ea_106434_1200x1200_fit_lanczos_3.png 1200w"
src="https://www.datawim.com/post/crop-recommendation-based-on-weather-fertilizers/index.en_files/figure-html/unnamed-chunk-42-1_huebfae9aa9782b06fa1960fb8064541ea_106434_d2cbfc9a36de87de5d63d5d698e37dc7.png"
width="760"
height="543"
loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;h2 id="all-models">All Models&lt;/h2>
&lt;pre>&lt;code class="language-r">p1 + p2 + p3
&lt;/code>&lt;/pre>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img alt="Test-set confusion matrices for the three models" srcset="
/post/crop-recommendation-based-on-weather-fertilizers/featured_hu413c5df6c08d6e0a33cda4d0818cc3db_186656_2a0c5e8180e9285b55f6c91c05c741b1.png 400w,
/post/crop-recommendation-based-on-weather-fertilizers/featured_hu413c5df6c08d6e0a33cda4d0818cc3db_186656_d4d4c6e620f4c6d3fdd8786ec7b12a59.png 760w,
/post/crop-recommendation-based-on-weather-fertilizers/featured_hu413c5df6c08d6e0a33cda4d0818cc3db_186656_1200x1200_fit_lanczos_3.png 1200w"
src="https://www.datawim.com/post/crop-recommendation-based-on-weather-fertilizers/featured_hu413c5df6c08d6e0a33cda4d0818cc3db_186656_2a0c5e8180e9285b55f6c91c05c741b1.png"
width="760"
height="253"
loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;h2 id="conclusion">Conclusion&lt;/h2>
&lt;ul>
&lt;li>All of the tested models accurately classified agronomic crops.&lt;/li>
&lt;li>I anticipated the same when plotted the distributions of the explanatory variables.&lt;/li>
&lt;li>Both weather and fertilizer variables had distinct distributions in each crop.&lt;/li>
&lt;li>And these models might have optimally learned this information, thus, classifying the crops with excellent accuracy.&lt;/li>
&lt;li>I will use these models in my upcoming notebooks to evaluate their performance in predicting diseased and healthy plots in crop fields.&lt;/li>
&lt;li>Stay Tuned!!!&lt;/li>
&lt;/ul></description></item></channel></rss>