<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Afb's Blog]]></title><description><![CDATA[Afb's Blog]]></description><link>https://afb.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 10 Sep 2026 22:23:01 GMT</lastBuildDate><atom:link href="https://afb.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Evaluate Machine Learning Models With Cross-Validation]]></title><description><![CDATA[What is Cross-validation?
Cross-validation is a method for evaluating how well a machine-learning model performs on new data. To do this, the data is regularly divided into training and testing sets.
The model is then trained on the training set, and...]]></description><link>https://afb.hashnode.dev/how-to-evaluate-machine-learning-models-with-cross-validation</link><guid isPermaLink="true">https://afb.hashnode.dev/how-to-evaluate-machine-learning-models-with-cross-validation</guid><category><![CDATA[cross-validation]]></category><category><![CDATA[machine learning models]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[data]]></category><category><![CDATA[Data Science]]></category><dc:creator><![CDATA[Afolabi Mahmood Olalekan]]></dc:creator><pubDate>Fri, 09 Jun 2023 17:34:40 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/yekGLpc3vro/upload/7700ba2162794ac39dd31fb7b00109cd.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-is-cross-validation">What is Cross-validation?</h2>
<p>Cross-validation is a method for evaluating how well a machine-learning model performs on new data. To do this, the data is regularly divided into training and testing sets.</p>
<p>The model is then trained on the training set, and its performance is assessed on the testing set.</p>
<p>To estimate the model's performance, we carry out this process several times, and average the results.</p>
<h2 id="heading-why-is-cross-validation-important"><strong>Why is cross-validation important?</strong></h2>
<p>It is important because it allows a more thorough and trustworthy evaluation of machine learning models.</p>
<p>It evaluates model performance on various data samples by splitting the data into various subsets and systematically rotating them as training and validation sets.</p>
<p>With this method, the risk of overfitting is reduced, and it's easier to predict how well a model will generalize to new data.</p>
<p>And it also allows practitioners to confidently analyze the model's capabilities, select the right model, and tune the hyperparameters, thereby improving the accuracy and dependability of machine learning solutions.</p>
<h2 id="heading-types-of-cross-validation">Types of cross-validation</h2>
<p>There are many different types of cross-validation, but the most common ones are:</p>
<ol>
<li><p>K-fold cross-validation</p>
</li>
<li><p>Stratified K-fold cross-validation</p>
</li>
<li><p>Hold-out based validation</p>
</li>
</ol>
<h2 id="heading-1-k-fold-cross-validation"><strong>1. K-fold cross-validation</strong></h2>
<p>K-fold cross-validation works by dividing the data into k equal portions. The model is then evaluated on the remaining portion of the data after training on the first k-1 parts of the data.</p>
<p>Each of the remaining portions of the data is used as the test set once during this operation, which is repeated k times. The model's performance is then estimated using the average of the k-test results.</p>
<p>K-fold cross-validation is a common technique for assessing machine learning models because it is simple to construct and offers a trustworthy estimate of the model's performance.</p>
<p>Check the code sample below:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> sklearn.model_selection <span class="hljs-keyword">import</span> KFold

<span class="hljs-comment"># Reset indices of X and y</span>
X = X.reset_index(drop=<span class="hljs-literal">True</span>)
y = y.reset_index(drop=<span class="hljs-literal">True</span>)

<span class="hljs-comment"># Define the number of folds (K)</span>
k = <span class="hljs-number">5</span>

<span class="hljs-comment"># Create a KFold object</span>
kf = KFold(n_splits=k)

<span class="hljs-comment"># Iterate over the folds</span>
<span class="hljs-keyword">for</span> train_index, val_index <span class="hljs-keyword">in</span> kf.split(X):
    <span class="hljs-comment"># Split the data into training and validation sets</span>
    X_train, X_val = X.loc[train_index], X.loc[val_index]
    y_train, y_val = y.loc[train_index], y.loc[val_index]

    <span class="hljs-comment"># Train and evaluate the model on the current fold</span>
    model.fit(X_train, y_train)
    score = model.score(X_val, y_val)

    <span class="hljs-comment"># Print the performance on the current fold</span>
    print(<span class="hljs-string">"Validation score:"</span>, score)
</code></pre>
<p>In the code above, the <code>KFold</code> class is created with a <code>n_splits</code> argument of 5. This means that the data will be split into 5 folds. The <code>for</code> loop then iterates over the folds, and for each fold, the data is split into training and validation sets. The model is then trained on the training set and evaluated on the validation set. The performance of the model on the validation set is then printed.</p>
<h2 id="heading-2-stratified-k-fold-cross-validation"><strong>2. Stratified K-fold cross-validation</strong></h2>
<p>Stratified K-fold cross-validation is a technique that improves the assessment of machine learning models by maintaining the class distribution within each fold.</p>
<p>This technique is very helpful for datasets with imbalances since it divides the dataset into K subsets while maintaining the proportion of each class.</p>
<p>Stratified K-fold cross-validation delivers more accurate estimates of model performance and helps prevent the biased evaluation by guaranteeing a representative distribution of classes in each fold.</p>
<p>Check the code sample below:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> sklearn.model_selection <span class="hljs-keyword">import</span> StratifiedKFold
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

<span class="hljs-comment"># Define the number of folds (K)</span>
K = <span class="hljs-number">5</span>

<span class="hljs-comment"># X: features, y: labels</span>
skf = StratifiedKFold(n_splits=K)

<span class="hljs-comment"># Convert X and y to numpy arrays if they are not already</span>
X = np.array(X)
y = np.array(y)

<span class="hljs-comment"># Iterate over the folds</span>
<span class="hljs-keyword">for</span> train_index, test_index <span class="hljs-keyword">in</span> skf.split(X, y):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]

    <span class="hljs-comment"># Train the model using the current fold</span>
    model.fit(X_train, y_train)

    <span class="hljs-comment"># Evaluate the model</span>
    score = model.score(X_test, y_test)
    print(<span class="hljs-string">"Validation score:"</span>, score)
</code></pre>
<h2 id="heading-3-hold-out-based-validation">3. Hold-out-based validation</h2>
<p>Hold-out-based cross-validation is a simple method of evaluating a machine-learning model by splitting the data into a training set and a test set. The model is trained on the training set and then evaluated on the test set.</p>
<p>This is easy to implement but can be less accurate than other types of cross-validation, such as k-fold cross-validation.</p>
<p>Check the code sample below:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> sklearn.model_selection <span class="hljs-keyword">import</span> train_test_split
<span class="hljs-keyword">from</span> sklearn.ensemble <span class="hljs-keyword">import</span> RandomForestRegressor

<span class="hljs-comment"># Split the dataset into training and test sets</span>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=<span class="hljs-number">0.3</span>, random_state=<span class="hljs-number">42</span>)

model = RandomForestRegressor()

<span class="hljs-comment"># Train the model using the training set</span>
model.fit(X_train, y_train)

<span class="hljs-comment"># Evaluate the model on the test set</span>
accuracy = model.score(X_test, y_test)
</code></pre>
<h2 id="heading-references">References</h2>
<p><a target="_blank" href="https://towardsdatascience.com/what-is-cross-validation-60c01f9d9e75">https://towardsdatascience.com/what-is-cross-validation-60c01f9d9e75</a></p>
<p><a target="_blank" href="https://towardsdatascience.com/cross-validation-430d9a5fee22">https://towardsdatascience.com/cross-validation-430d9a5fee22</a></p>
<p><a target="_blank" href="https://towardsdatascience.com/understanding-8-types-of-cross-validation-80c935a4976d">https://towardsdatascience.com/understanding-8-types-of-cross-validation-80c935a4976d</a></p>
<p><a target="_blank" href="https://scikit-learn.org/stable/modules/cross_validation.html">https://scikit-learn.org/stable/modules/cross_validation.html</a></p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>Congratulations! You now possess the fundamental knowledge of cross-validation methods, giving you the confidence to improve the accuracy and dependability of your machine-learning models.</p>
]]></content:encoded></item><item><title><![CDATA[How to Download Bulk Images Using Bing Image Downloader]]></title><description><![CDATA[Introduction
Bing image downloader is an open-source Python library created by Guru Prasad Singh. It makes it easy to download bulk Bing images without the stress of downloading them one by one.
According to Pypi.org, the library uses async Url which...]]></description><link>https://afb.hashnode.dev/how-to-download-bulk-images-using-bing-image-downloader</link><guid isPermaLink="true">https://afb.hashnode.dev/how-to-download-bulk-images-using-bing-image-downloader</guid><category><![CDATA[image classification]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[classification]]></category><category><![CDATA[Data Science]]></category><dc:creator><![CDATA[Afolabi Mahmood Olalekan]]></dc:creator><pubDate>Fri, 03 Mar 2023 09:31:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/jf1EomjlQi0/upload/884dc16ac43e3aa55d2f3aed520fbb9e.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction"><strong>Introduction</strong></h3>
<p>Bing image downloader is an open-source Python library created by <a target="_blank" href="https://github.com/gurugaurav">Guru Prasad Singh.</a> It makes it easy to download bulk Bing images without the stress of downloading them one by one.</p>
<p>According to <a target="_blank" href="http://Pypi.org">Pypi.org</a>, the library uses async Url which makes it very fast while downloading. You will learn how to download Bulk images by following the code snippets below.</p>
<p><a target="_blank" href="https://pypi.org/project/bing-image-downloader/"><em>Disclaimer</em></a><em>: This program lets you download tons of images from Bing. Please do not download or use any image that violates its copyright terms.</em></p>
<h2 id="heading-prerequisites"><strong>Prerequisites</strong></h2>
<p>You can run the code snippets using <strong>Vscode</strong>, <strong>Jupyter Notebook</strong>, or <strong>Google Colab</strong>.</p>
<p><em>Note: we used Google Colab, Jupyter Notebook, and Vscode to test the code below.</em></p>
<h2 id="heading-step-1-installation">Step 1 - Installation</h2>
<p>The first step here is to install the library we are going to use which is Bing image downloader.</p>
<p>Copy the code below and paste it into your Notebook.</p>
<pre><code class="lang-python">pip install bing-image-downloader
</code></pre>
<h2 id="heading-step-2-import-the-library">Step 2 - Import the library</h2>
<p>After the Installation, importing the library will be the next step.</p>
<p>Copy the code below and paste it into your Notebook.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> bing_image_downloader <span class="hljs-keyword">import</span> downloader
</code></pre>
<h2 id="heading-step-3-inputting-the-necessary-parameters"><strong>Step 3 - Inputting the necessary parameters</strong></h2>
<p>Let's say we want to do an image classification project for three animals. We need to download <strong>30</strong> images each for horses, goats and donkeys. And we will store each of them in separate files named <strong>horse dataset</strong>, <strong>goat dataset</strong> and <strong>donkey dataset.</strong></p>
<p><em>Note: we cannot download the three datasets in a single snippet of code. we have to split it into three for each of the datasets and run each snippet of code.</em></p>
<p><strong>For the Horse dataset :</strong></p>
<p>Copy the code below and paste it into your Notebook.</p>
<pre><code class="lang-python">downloader.download(<span class="hljs-string">"horse"</span>, limit=<span class="hljs-number">30</span>, output_dir=<span class="hljs-string">"horse_dataset"</span>, adult_filter_off=<span class="hljs-literal">True</span>)
</code></pre>
<p>After a few minutes, you can open the file created during the process, named <strong>horse dataset</strong>, to view the downloaded images.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1677744310660/266fec01-95c6-4487-aa07-c232e8b373e7.png" alt class="image--center mx-auto" /></p>
<p><strong>For the Goat dataset :</strong></p>
<p>Copy the code below and paste it into your Notebook.</p>
<pre><code class="lang-python">downloader.download(<span class="hljs-string">"Goat"</span>, limit=<span class="hljs-number">30</span>, output_dir=<span class="hljs-string">"Goat_dataset"</span>, adult_filter_off=<span class="hljs-literal">True</span>)
</code></pre>
<p>After a few minutes, you can open the file created during the process, named <strong>Goat dataset</strong>, to view the downloaded images.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1677744366230/0bc45bb2-69b3-42c3-9d5b-f43ee26158a9.png" alt class="image--center mx-auto" /></p>
<p><strong>For the Donkey dataset :</strong></p>
<p>Copy the code below and paste it into your Notebook.</p>
<pre><code class="lang-python">downloader.download(<span class="hljs-string">"Donkey"</span>, limit=<span class="hljs-number">30</span>, output_dir=<span class="hljs-string">"Donkey_dataset"</span>, adult_filter_off=<span class="hljs-literal">True</span>)
</code></pre>
<p>After a few minutes, you can open the file created during the process, named <strong>Donkey dataset</strong>, to view the downloaded images.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1677744470667/ff1a85de-bbf6-4d75-b41d-2b1f3acbc862.png" alt class="image--center mx-auto" /></p>
<table><tbody><tr><td><p><strong>parameters</strong></p></td><td><p><strong>Explanation</strong></p></td></tr><tr><td><p>query_string(The first parameter)</p></td><td><p>A search query like the examples we used above-named horse, goat and donkey. To download the specific images we want.</p></td></tr><tr><td><p>limit</p></td><td><p>The number of images to download, though the default is 100 while I used 30 above. Note it doesn't have a limit.</p></td></tr><tr><td><p>output_dir</p></td><td><p>This is the name of the output directory on your machine, where the images will appear. it is optional while the default is 'dataset'.</p></td></tr><tr><td><p>adult_filter_off</p></td><td><p>It helps to enable or disable adult filtration (it is optional, but the default is True)</p></td></tr></tbody></table>

<p>If you still want to tweak with more parameters, check the link below</p>
<p><a target="_blank" href="https://pypi.org/project/bing-image-downloader/">https://pypi.org/project/bing-image-downloader/</a></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Congratulations! You can now use the Bing image downloader to download large numbers of images from Bing. I hope this article will be useful to you. Finally, enjoy your time learning!</p>
]]></content:encoded></item></channel></rss>