<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tola Alade on Medium]]></title>
        <description><![CDATA[Stories by Tola Alade on Medium]]></description>
        <link>https://medium.com/@tolaalade_48082?source=rss-3b429904850d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*5EXzvfpuSgHy8J3lcqsm6Q.jpeg</url>
            <title>Stories by Tola Alade on Medium</title>
            <link>https://medium.com/@tolaalade_48082?source=rss-3b429904850d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 18:15:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tolaalade_48082/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Probability Calibration]]></title>
            <link>https://medium.com/cambridgespark/probability-calibration-c7252ac123f?source=rss-3b429904850d------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7252ac123f</guid>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[probability]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Tola Alade]]></dc:creator>
            <pubDate>Wed, 14 Aug 2019 13:27:54 GMT</pubDate>
            <atom:updated>2019-12-23T09:22:13.856Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oDpYwZqQqBiz5F_GxZeelg.png" /></figure><h4>How to obtain calibrated probabilities using the credit card defaults dataset</h4><p>In this tutorial, I’ll talk about how to obtain calibrated probabilities using the credit card defaults dataset, which can be found <a href="https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients">here</a>, but first, let’s run through some background on probability calibration and why this might be required.</p><h4>Background</h4><p>Probability calibration is essential if the required output is the true probability returned from a classifier whose probability distribution does not match the expected distribution of the predicted class. This is not the case if the required output from a classifier is the ranking or predicted class i.e. 0 or 1 for a binary classifier.</p><p>Not all classifiers produce well-calibrated probabilities and, for some classifiers, the predicted probability does not match the output of its decision function (for example SVC).</p><p>Logistics regression is popularly known to produce well-calibrated probabilities if and only if the parametric assumptions of the model holds. Naive Bayes is notorious for producing probabilities close to its extremes i.e. 0 and 1, whilst boosting and bagging techniques are known for producing probabilities away from 0 and 1, hence requiring calibration.</p><p>To better understand how well various classifiers are calibrated, I’d strongly recommend this <a href="https://scikit-learn.org/stable/modules/calibration.html">page</a>. To demonstrate the above, let&#39;s use two different models to predict credit card defaults.</p><p><strong>To bring this to life, let’s do the following:</strong></p><ul><li>Load the dataset</li><li>Apply some basic preprocessing</li><li>Build a Random Forest model which predicts the likelihood of an individual defaulting on their credit card payment</li><li>Inspect the probability using a reliability curve</li></ul><p>First, let’s import relevant packages.</p><pre>import pandas as pd<br>import numpy as np</pre><pre>from sklearn.calibration import calibration_curve, CalibratedClassifierCV<br>from sklearn.ensemble import RandomForestClassifier<br>from sklearn.model_selection import train_test_split<br>from sklearn.metrics import roc_auc_score</pre><pre>import matplotlib.pyplot as plt</pre><p>Next, read in the excel dataset. Below is some preprocessing which I have applied. For simplicity, we’ll only be working with numerical features as the goal is to demo calibration techniques and not build the most optimal model.</p><pre>data = pd.read_excel(&quot;credit_card_defaults.xls&quot;, header=1)</pre><pre>model_features = list(set(data.columns).difference({&quot;ID&quot;, &quot;default payment next month&quot;, &quot;SEX&quot;, &quot;EDUCATION&quot;, &quot;MARRIAGE&quot;}))<br>target = [&quot;default payment next month&quot;]<br>X = data[model_features]<br>y = data[target]</pre><pre>x_train, x_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=12)</pre><p>As per good practice, I have split the data into train and test set using Scikit-learn train-test split which can be found <a href="https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html">here</a>. However, as we will be fitting a calibration model, we require another dataset to be used for this fitting to avoid overfitting. To achieve this, I further split the train dataset into train and validation set as below:</p><pre># Create a hold out dataset to train the calibrated model to prevent overfitting<br>x_model_train, x_valid, y_model_train, y_valid = train_test_split(x_train, y_train, stratify=y_train, random_state=12)</pre><p>Next, we fit a Random Forest model to the training dataset. Further work can be done here to identify the optimal parameters for the Random Forest classifier.</p><pre>rf_clf = RandomForestClassifier(n_estimators=100, <br>                                max_depth=6, <br>                                min_samples_split=200, <br>                                random_state=12, <br>                                class_weight=&quot;balanced&quot;)<br>rf_clf.fit(x_model_train, y_model_train)<br>rf_predictions = rf_clf.predict_proba(x_test)<br>rf_roc_score = roc_auc_score(y_test, rf_predictions[:,1])<br># rf_roc_score = 0.77</pre><p>Now let us inspect the output probabilities by looking at the reliability curve. This is a popular way of diagnosing how well-calibrated probabilities are. Fortunately for us, there is an sklearn implementation for this which can be found <a href="https://scikit-learn.org/stable/modules/generated/sklearn.calibration.calibration_curve.html">here</a>.</p><p>A perfectly calibrated classifier has a reliability plot of the form y = x.</p><pre>probablities = np.arange(0, 1.2, 0.2)<br>fraction_of_positives, mean_predicted_value = calibration_curve(y_test, rf_predictions[:,1], n_bins=10)<br>plt.plot(mean_predicted_value, fraction_of_positives, label=&quot;Random forest&quot;)<br>plt.plot(probablities, probablities, &quot;--&quot;, label=&quot;Perfectly calibrated&quot;)<br>plt.legend()<br>plt.xlim([-0.05, 1.05])<br>plt.title(&quot;Reliability curve&quot;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*xOhksREpfx2Dmd3ldWciyA.png" /></figure><p>The curve above shows the output probabilities from the Random Forest could benefit from calibration.</p><p><strong>How do we formally define a well-calibrated probability?</strong></p><blockquote>In very simple terms, these are probabilities which can be interpreted as a confidence interval. Furthermore, a classifier is said to produce well calibrated probabilities if for the instances (data points) receiving probability 0.5, 50% of those instances belongs to the positive class.</blockquote><p><strong>There are 2 popular methods for calibrating probabilities:</strong></p><p><strong><em>1. Platt Scaling</em></strong>: aka logistic calibration is a parametric approach with a sigmoidal calibration map which assumes each class probabilities are normally distributed. In simple terms, this method fits a logistic regression to output probabilities to produce better-calibrated probabilities.</p><p><strong><em>Pro -</em></strong> Works well with a small dataset</p><p><strong><em>Con -</em></strong> Could produce worse probabilities calibration wise if the assumptions do not hold</p><p><strong><em>2. Isotonic regression</em></strong>: this is a non-parametric approach which tends to perform well with as it fits a piece wise non-decreasing function to the predicted probabilities outputted by the base model.</p><p><strong><em>Pro -</em> </strong>Makes no assumption about the input probabilities</p><p><strong><em>Con -</em></strong> Requires more data points to work well</p><p>For this dataset, we will use the Platt Scaling technique to calibrate our probabilities.</p><p><strong>There are 2 ways of using the sklearn <em>CalibratedClassifierCV</em> class :</strong></p><ol><li>Pass a fitted model and thereby setting cv to <em>prefit</em>. It is important to note that the data used in fitting the base estimator and the calibrator is disjoint.</li><li>Fit a base estimator using k-fold cross-validation and the probabilities for each of the folds are then averaged for prediction.</li></ol><p>The <em>method</em> parameter is one of the two methods discussed earlier for calibrating probabilities where <em>sigmoid</em> is Platt Scaling and <em>isotonic</em> for isotonic regression. Sklearn documentation for the <em>CalibratedClassifierCV</em> class can be found <a href="https://scikit-learn.org/stable/modules/generated/sklearn.calibration.CalibratedClassifierCV.html">here</a>.</p><p>For this demo, we will be using the first method using the fitted Random Forest classifier and Platt Scaling.</p><pre>calib_clf = CalibratedClassifierCV(rf_clf, method=&quot;sigmoid&quot;, cv=&quot;prefit&quot;)<br>calib_clf.fit(x_valid, y_valid)<br>calibrated_predictions = calib_clf.predict_proba(x_test)<br>calib_roc_score = roc_auc_score(y_test, calibrated_predictions[:,1])</pre><p>Finally, let us inspect the reliability curve for the Random Forest probabilities, compared to the calibrated ones.</p><pre>fig = plt.figure(figsize=(10, 7))</pre><pre>for outcomes, models, roc_scores in zip([rf_predictions,calibrated_predictions], <br>    [&quot;Random Forest&quot;, &quot;Calibrated Model&quot;],<br>    [rf_roc_score, calib_roc_score]):</pre><pre>    fraction_of_positives, mean_predicted_value =        calibration_curve(y_test, outcomes[:,1])<br>    plt.plot(mean_predicted_value, fraction_of_positives, label=&quot;%s with roc - (%1.2f)&quot; % (models, roc_scores))<br>    plt.legend()<br>    plt.title(&quot;Reliability curve&quot;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*hbB1h0V4LwySfaV8ZtIcrA.png" /></figure><p>Looking at the plot above, it is evident that we get better-calibrated probabilities after applying Platt scaling with no impact on our metric of choice which is ROC score.</p><h4><strong>Beware</strong></h4><p>Because Platt Scaling is a parametric method, if the output model probabilities fail the assumption, it could produce ‘calibrated’ probabilities worse than the original probabilities from the base estimator, and it’s always worth inspecting log loss pre and post-calibration.</p><h4>Other tutorials which may interest you:</h4><ul><li><a href="https://blog.cambridgespark.com/how-to-determine-the-optimal-number-of-clusters-for-k-means-clustering-14f27070048f">How to determine the optimal number of clusters for k-means clustering</a><br>- <a href="https://blog.cambridgespark.com/unit-testing-with-pyspark-fb31671b1ad8">Unit testing with PySpark</a><br>- <a href="https://blog.cambridgespark.com/hyperparameter-tuning-in-xgboost-4ff9100a3b2f">Hyperparameter tuning in XGBoost</a><br>- <a href="https://blog.cambridgespark.com/getting-started-with-xgboost-3ba1488bb7d4">Getting started with XGBoost</a><br>- <a href="https://blog.cambridgespark.com/six-data-science-projects-to-top-up-your-skills-and-knowledge-2f073fd80f55">Six Data Science projects to boost your skills and knowledge</a></li></ul><p>If you’d like to be the first to hear about our new content, including new tutorials, case studies and other useful resources, <a href="https://share.hsforms.com/1uFMXtYFwQu6GbZS7AwA-Yg2gkgi"><strong>click here</strong></a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7252ac123f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cambridgespark/probability-calibration-c7252ac123f">Probability Calibration</a> was originally published in <a href="https://medium.com/cambridgespark">Cambridge Spark</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tutorial: How to determine the optimal number of clusters for k-means clustering]]></title>
            <link>https://medium.com/cambridgespark/how-to-determine-the-optimal-number-of-clusters-for-k-means-clustering-14f27070048f?source=rss-3b429904850d------2</link>
            <guid isPermaLink="false">https://medium.com/p/14f27070048f</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Tola Alade]]></dc:creator>
            <pubDate>Sun, 27 May 2018 12:52:11 GMT</pubDate>
            <atom:updated>2019-12-23T09:29:40.093Z</atom:updated>
            <content:encoded><![CDATA[<h4>By Tola Alade, Data Scientist and Applied Data Science Student at <a href="https://cambridgespark.com/applied-datascience">Cambridge Spark</a></h4><p><strong>Introduction<br></strong> K-means is a type of unsupervised learning and one of the popular methods of clustering unlabelled data into k clusters. One of the trickier tasks in clustering is identifying the appropriate number of clusters k. In this tutorial, we will provide an overview of how k-means works and discuss how to implement your own clusters.</p><p>We will also understand how to use the elbow method as a way to estimate the value k. Another popular method of estimating k is through silhouette analysis, a scikit learn example can be found <a href="http://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_silhouette_analysis.html">here</a>.</p><p>We will use the wholesale customer dataset which can be downloaded <a href="http://archive.ics.uci.edu/ml/datasets/Wholesale+customers">here</a>.</p><p><strong>K-means Overview<br> </strong>Before diving into the dataset, let us briefly discuss how k-means works:</p><ol><li>The process begins with k centroids initialised at random.</li><li>These centroids are used to assign points to its nearest cluster.</li><li>The mean of all points within the cluster is then used to update the position of the centroids.</li><li>The above steps are repeated until the values of the centroids stabilise.</li></ol><p><strong>Getting Started</strong><br> In this tutorial, we will be using the scikit-learn’s implementation of k-means which can be found <a href="http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html">here</a></p><p><strong>The dataset</strong><br> The dataset we will study refers to clients of a wholesale distributor. It contains information such as clients annual spend on fresh product, milk products, grocery products etc. Below is some more information an each feature:</p><ol><li>FRESH: annual spending (m.u.) on fresh products (Continuous)</li><li>MILK: annual spending (m.u.) on milk products (Continuous)</li><li>GROCERY: annual spending (m.u.) on grocery products (Continuous)</li><li>FROZEN: annual spending (m.u.) on frozen products (Continuous)</li><li>DETERGENTS_PAPER: annual spending (m.u.) on detergents and paper products (Continuous)</li><li>DELICATESSEN: annual spending (m.u.) on delicatessen products (Continuous)</li><li>CHANNEL: customer channels - Horeca (Hotel/Restaurant/Cafe) or Retail channel (Nominal)</li><li>REGION: customer regions - Lisnon, Oporto or Other (Nominal)</li></ol><pre># Import required packages<br>import pandas as pd<br>from sklearn.preprocessing import MinMaxScaler<br>from sklearn.cluster import KMeans</pre><pre>import matplotlib.pyplot as plt</pre><p>Read in data and inspect the first 5 records.</p><pre>data = pd.read_csv(&#39;Wholesale customers data.csv&#39;)<br>data.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*-iJbmPN-5k2eyNjQMuAVAg.png" /></figure><p>Below is a split of categorical and continuous features</p><pre>categorical_features = [&#39;Channel&#39;, &#39;Region&#39;]<br>continuous_features = [&#39;Fresh&#39;, &#39;Milk&#39;, &#39;Grocery&#39;, &#39;Frozen&#39;, &#39;Detergents_Paper&#39;, &#39;Delicassen&#39;]</pre><p>Descriptive statistics below shows on average clients spend the most on fresh groceries and the least on delicassen.</p><pre>data[continuous_features].describe()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/779/1*hzBDu4D44Wu01FzI5idG9w.png" /></figure><p>To use the categorical features, we need to convert the categorical features to binary using pandas get dummies.</p><pre>for col in categorical_features:<br>    dummies = pd.get_dummies(data[col], prefix=col)<br>    data = pd.concat([data, dummies], axis=1)<br>    data.drop(col, axis=1, inplace=True)</pre><pre>data.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/943/1*u1ukoDn7DFrHOEz8XUbfHA.png" /></figure><p>To give equal importance to all features, we need to scale the continuous features. We will be using scikit-learn’s MinMaxScaler as the feature matrix is a mix of binary and continuous features . Other alternatives includes StandardScaler.</p><pre>mms = MinMaxScaler()<br>mms.fit(data)<br>data_transformed = mms.transform(data)</pre><p>For each k value, we will initialise k-means and use the inertia attribute to identify the sum of squared distances of samples to the nearest cluster centre.</p><pre>Sum_of_squared_distances = []<br>K = range(1,15)<br>for k in K:<br>    km = KMeans(n_clusters=k)<br>    km = km.fit(data_transformed)<br>    Sum_of_squared_distances.append(km.inertia_)</pre><p>As k increases, the sum of squared distance tends to zero. Imagine we set k to its maximum value n (where n is number of samples) each sample will form its own cluster meaning sum of squared distances equals zero.</p><p>Below is a plot of sum of squared distances for k in the range specified above. If the plot looks like an arm, then the elbow on the arm is optimal k.</p><pre>plt.plot(K, Sum_of_squared_distances, &#39;bx-&#39;)<br>plt.xlabel(&#39;k&#39;)<br>plt.ylabel(&#39;Sum_of_squared_distances&#39;)<br>plt.title(&#39;Elbow Method For Optimal k&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*jWe7Ns_ubBpOaemM.png" /></figure><p>In the plot above the elbow is at k=5 indicating the optimal k for this dataset is 5</p><p><strong>Thanks for reading! If you enjoyed the post, we’d appreciate your support by applauding via the clap (👏🏼) button below or by sharing this article so others can find it.</strong></p><p>If you’d like to be the first to hear about our new content, including new tutorials, case studies and other useful resources, <a href="https://share.hsforms.com/1uFMXtYFwQu6GbZS7AwA-Yg2gkgi"><strong>click here</strong></a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=14f27070048f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cambridgespark/how-to-determine-the-optimal-number-of-clusters-for-k-means-clustering-14f27070048f">Tutorial: How to determine the optimal number of clusters for k-means clustering</a> was originally published in <a href="https://medium.com/cambridgespark">Cambridge Spark</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>