<?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 Alexey Khrustalev on Medium]]></title>
        <description><![CDATA[Stories by Alexey Khrustalev on Medium]]></description>
        <link>https://medium.com/@alexeykhrustalev?source=rss-d98b818da882------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8NATome1pbFIBLMoPbYTXA.png</url>
            <title>Stories by Alexey Khrustalev on Medium</title>
            <link>https://medium.com/@alexeykhrustalev?source=rss-d98b818da882------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 15:49:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alexeykhrustalev/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[Your metrics lie to you.]]></title>
            <link>https://medium.com/@alexeykhrustalev/your-metrics-lie-to-you-abfd8438d4c9?source=rss-d98b818da882------2</link>
            <guid isPermaLink="false">https://medium.com/p/abfd8438d4c9</guid>
            <category><![CDATA[explainability]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[impact]]></category>
            <dc:creator><![CDATA[Alexey Khrustalev]]></dc:creator>
            <pubDate>Thu, 29 Aug 2024 14:51:18 GMT</pubDate>
            <atom:updated>2024-08-29T14:51:18.678Z</atom:updated>
            <content:encoded><![CDATA[<p>Every beginner in Data Science faces this harsh truth at some point: accuracy (as in the number of correctly predicted samples/number of samples) rarely matters. Luckily, this problem has a solution. There are dozens of metrics available, some of them account for a class imbalance, and others prioritize precision or recall, depending on the business needs. All of them are great and work well in a lot of cases, but can one be sure that if a metric looks good, then a model has truly learned the data? I don’t think so.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fGsZ1x8xcJ11z3U4" /><figcaption>Photo by <a href="https://unsplash.com/@possessedphotography?utm_source=medium&amp;utm_medium=referral">Possessed Photography</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Sometimes a wrong path leads to a right answer, and exactly this scenario might happen when we — Data Scientists — choose to rely solely on the metrics of choice.</p><p>As a solution to this problem, I think everyone should include the following step in their model development process: explainability. Now, this might sound obvious to a lot of experienced folks out there, but I’ve met enough people who were not aware of such a problem that it became clear this information should be shared more.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1wHqNSq2qCstrQyq" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Further, I will mention the most common and simple explainability techniques, but firstly, I would like to stress that explainability makes sense when a Data Scientist has domain knowledge. Imagine this: your model predicts peoples’ height mostly correctly, but when you look at, e.g., feature importance, you see that the most important feature is the month when the measurement was made. Sounds strange, right? We can all use our common sense to figure out that it’s most likely wrong, but if we are talking about less common sense-related things, it might be not that easy to conclude. That’s why one needs to understand whatever business problem one tries to solve well enough to make educated judgments.</p><p>Let’s return to the example of predicting people’s height. Imagine if, instead of using a model explainability technique, you only focused on a single metric — say, accuracy. You might be thrilled to see that your model has a 95% accuracy rate, but without understanding the “why” behind this performance, you’re essentially flying blind. The feature importance analysis, which I mentioned earlier, revealed that the month when the measurement was taken was the most important feature. This should immediately raise a red flag.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nyi8_lcz3jtgQ5Tx" /><figcaption>Photo by <a href="https://unsplash.com/@towfiqu999999?utm_source=medium&amp;utm_medium=referral">Towfiqu barbhuiya</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>What’s happening here is an overfitting to a specific pattern that coincidentally matches the training data but doesn’t generalize to new data. Without explainability, you might deploy this model and see a sharp drop in performance when it encounters new data where the “month” feature doesn’t align with the same patterns. This is why it’s crucial to go beyond just metrics. Metrics can tell you if a model works well, but explainability can help you understand why it works — or doesn’t work.</p><h3>Common Explainability Techniques</h3><p>There are various explainability techniques that you can apply, each with its strengths and weaknesses. Here are a few of the most commonly used ones:</p><h4>1. Feature Importance:</h4><p>• What it is: This technique ranks features based on their contribution to the model’s predictions.</p><p>• When to use it: It’s useful when you want a global understanding of which features are most influential across all predictions.</p><p>Example using a Random Forest model</p><pre>from sklearn.ensemble import RandomForestClassifier<br>from sklearn.datasets import load_iris<br>import pandas as pd<br># Load dataset and fit model<br>data = load_iris()<br>X, y = data.data, data.target<br>model = RandomForestClassifier()<br>model.fit(X, y)<br># Feature importance<br>feature_importance = pd.DataFrame({<br> &#39;Feature&#39;: data.feature_names,<br> &#39;Importance&#39;: model.feature_importances_<br>}).sort_values(by=&#39;Importance&#39;, ascending=False)<br>print(feature_importance)</pre><p>This will give you a sorted list of features by their importance, offering insight into which features the model considers most critical.</p><h4>2. SHAP (SHapley Additive exPlanations):</h4><p>• What it is: SHAP values offer a way to break down a prediction into the contribution of each feature, making it a powerful tool for local explainability.</p><p>• When to use it: It’s particularly useful when you want to explain individual predictions.</p><p>Example using SHAP with a Gradient Boosting model:</p><pre>import shap<br>from sklearn.ensemble import GradientBoostingClassifier<br># Fit model<br>model = GradientBoostingClassifier()<br>model.fit(X, y)<br># SHAP values<br>explainer = shap.TreeExplainer(model)<br>shap_values = explainer.shap_values(X)<br># Visualize SHAP values for a single prediction<br>shap.initjs()<br>shap.force_plot(explainer.expected_value[0], shap_values[0][0], X[0])</pre><p>The SHAP force plot will visually display how each feature contributes to a single prediction, helping you understand the model’s decision-making</p><h3>Why Explainability is Crucial for Business</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*UBGbU_oFWXsLKhxO" /><figcaption>Photo by <a href="https://unsplash.com/@austindistel?utm_source=medium&amp;utm_medium=referral">Austin Distel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Explainability isn’t just a technical luxury; it’s a necessity for translating model performance into business impact. When stakeholders understand why a model makes certain decisions, they’re more likely to trust it and adopt it in critical processes. Moreover, in regulated industries like finance or healthcare, explainability is often a legal requirement.</p><p>Imagine deploying a credit scoring model that rejects loan applications with no explanation. If customers demand reasons, or if auditors come knocking, a black-box model won’t suffice. However, a model that can explain its decisions not only builds trust but also provides insights into potential biases or flaws, enabling continuous improvement.</p><h3>Conclusion</h3><p>In summary, while metrics like accuracy, precision, and recall are important, they are not the full story. A model with excellent metrics but poor explainability can lead to overconfidence and missteps when the model is put into production. By incorporating explainability techniques such as Feature Importance, SHAP, and others, you can better understand your model’s decision-making process, ensuring it aligns with the business problem and operates as expected. As a Data Scientist, your goal should always be to create models that not only perform well but also make sense, providing value and trustworthiness to stakeholders.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=abfd8438d4c9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to apply a CNN from PyTorch to your images.]]></title>
            <link>https://medium.com/data-science/how-to-apply-a-cnn-from-pytorch-to-your-images-18515416bba1?source=rss-d98b818da882------2</link>
            <guid isPermaLink="false">https://medium.com/p/18515416bba1</guid>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[cnn]]></category>
            <dc:creator><![CDATA[Alexey Khrustalev]]></dc:creator>
            <pubDate>Sat, 29 Aug 2020 15:27:06 GMT</pubDate>
            <atom:updated>2020-08-29T19:18:05.450Z</atom:updated>
            <content:encoded><![CDATA[<h4>You will learn how to upload downloaded images to PyTorch in a matter of minutes.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*s6WEPH8zPuM1JWAo" /><figcaption>Photo by <a href="https://unsplash.com/@marnhe?utm_source=medium&amp;utm_medium=referral">Marnhe du Plooy</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Hello everyone! Today I’d like to talk about uploading your images into your PyTorch CNN.</p><p>Today we will cover the following:</p><ul><li>How to store images properly, so that you can easily get your data labeled.</li><li>How to access the data using PyTorch and make some preprocessing on the way.</li><li>In the end, we will use a very simple CNN to classify our images.</li></ul><h3>First things first, how to store the data?</h3><p>I will use <a href="https://www.kaggle.com/chetankv/dogs-cats-images">this</a> data set, which contains the images of cats and dogs.</p><p>The easiest way to store your images is to create a folder for each class, naming the folder with the name of the class.</p><p>Let me show an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hLVWZZ0pYr-r_zB4Tzr4Pg.png" /><figcaption>Image by Author</figcaption></figure><p>By simply naming your folders properly, you’ll let PyTorch know to which class to assert an image.</p><h3>Now, let’s go into the code.</h3><pre>import matplotlib.pyplot as plt<br>from torchvision import datasets, transforms<br>from torch.utils.data import DataLoader<br>import torch.nn as nn<br>import torch.nn.functional as F<br>import torch.optim as optim<br>import torch</pre><pre>def get_data():<br>    data_dir = &#39;/your_path/Data/&#39;<br><br>    train_set = datasets.ImageFolder(data_dir + &#39;/training_set&#39;)<br>    test_set = datasets.ImageFolder(data_dir + &#39;/test_set&#39;)<br><br>    return train_set, test_set</pre><p>The function above gets the data from the directory. You have to simply specify the path to your <strong>Train Set</strong> and <strong>Test Set</strong> folders. PyTorch will then automatically assign the labels to images, using the names of the folders in the specified directory.</p><p>However, you might want to make some preprocessing before using the images, so let’s do it and, furthermore, let’s create a <strong>DataLoader</strong> right away. To do so, let’s add some new lines to the code above.</p><pre>def get_data():<br>    data_dir = &#39;/your_path/Data/&#39;<br>   <br>    <strong>transform</strong> = transforms.Compose([<br>        #transforms.RandomRotation(20),<br>        transforms.RandomResizedCrop(128),<br>        #transforms.RandomHorizontalFlip(),<br>        transforms.ToTensor()])<br><br>    train_set = datasets.ImageFolder(data_dir + &#39;/training_set&#39;, <strong>transform=transform</strong>)<br>    test_set = datasets.ImageFolder(data_dir + &#39;/test_set&#39;, <strong>transform=transform</strong>)<br><br>    train = <strong>DataLoader</strong>(train_set, batch_size=32, shuffle=True)<br>    test = <strong>DataLoader</strong>(test_set, batch_size=32, shuffle=True)<br><br>    return train, test</pre><p>As the transformations, you may want to crop, flip, resize, rotate, etc the images. To do so you can specify all the things you need in one place, using <strong>transforms</strong>. Then you can create the <strong>DataLoader</strong> with your images uploaded.</p><h3>That’s where a CNN comes into play.</h3><p>Now, when you have the data ready, you might want to take a quick look at it. To do so, you can use this simple function, which will show the first 5 images.</p><pre>def train_imshow():<br>    classes = (&#39;cat&#39;, &#39;dog&#39;) # Defining the classes we have<br>    dataiter = iter(train)<br>    images, labels = dataiter.next()<br>    fig, axes = plt.subplots(figsize=(10, 4), ncols=5)<br>    for i in range(5):<br>        ax = axes[i]<br>        ax.imshow(images[i].permute(1, 2, 0)) <br>        ax.title.set_text(&#39; &#39;.join(&#39;%5s&#39; % classes[labels[i]]))<br>    plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*97yorD_88mkJsG975MeimQ.png" /><figcaption>Image by Author</figcaption></figure><p>Let’s define our Convolutional Neural Network:</p><pre>class Net(nn.Module):<br>    def __init__(self):<br>        super(Net, self).__init__()<br>        self.conv1 = nn.Conv2d(3, 6, 5)<br>        self.pool = nn.MaxPool2d(8, 8)<br>        self.conv2 = nn.Conv2d(6, 16, 5)<br>        self.fc1 = nn.Linear(16, 100)<br>        self.fc2 = nn.Linear(100, 50)<br>        self.fc3 = nn.Linear(50, 2)<br><br>    def forward(self, x):<br>        x = self.pool(F.relu(self.conv1(x)))<br>        x = self.pool(F.relu(self.conv2(x)))<br>        x = x.view(-1, 16)<br>        x = F.relu(self.fc1(x))<br>        x = F.relu(self.fc2(x))<br>        x = self.fc3(x)<br>        return x</pre><p>I used the network defined <a href="https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html">here</a> and modified it a bit so that now it can work with my images.</p><p>Now we can specify the criterion, optimizer, learning rate, and train the network.</p><pre>criterion = nn.CrossEntropyLoss()<br>optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)</pre><pre>if torch.cuda.is_available(): # Checking if we can use GPU<br>    model = net.cuda()<br>    criterion = criterion.cuda()<br><br><br>def train_net(n_epoch): # Training our network<br>    losses = []<br>    for epoch in range(n_epoch):  # loop over the dataset multiple times<br>        running_loss = 0.0<br>        for i, data in enumerate(train, 0):<br>            # get the inputs; data is a list of [inputs, labels]<br>            inputs, labels = data<br><br>            # zero the parameter gradients<br>            optimizer.zero_grad()<br><br>            # forward + backward + optimize<br>            outputs = net(inputs)<br>            loss = criterion(outputs, labels)<br>            loss.backward()<br>            optimizer.step()<br><br>            # print statistics<br>            losses.append(loss)<br>            running_loss += loss.item()<br>            if i % 100 == 99:  # print every 2000 mini-batches<br>                print(&#39;[%d, %5d] loss: %.10f&#39; %<br>                      (epoch + 1, i + 1, running_loss / 2000))<br>                running_loss = 0.0<br>    plt.plot(losses, label=&#39;Training loss&#39;)<br>    plt.show()<br>    print(&#39;Finished Training&#39;)</pre><p>You can save and load your trained network.</p><pre>PATH = &#39;./cat_dog_net.pth&#39;<br>torch.save(net.state_dict(), PATH)<br><br># Loading the trained network<br>net.load_state_dict(torch.load(PATH))</pre><p>Let’s feed our test images into the network.</p><pre>correct = 0<br>total = 0<br>with torch.no_grad():<br>    for data in test:<br>        images, labels = data<br>        outputs = net(images)<br>        _, predicted = torch.max(outputs.data, 1)<br>        total += labels.size(0)<br>        correct += (predicted == labels).sum().item()<br><br>print(&#39;Accuracy of the network on the %d test images: %d %%&#39; % (len(test),<br>    100 * correct / total))</pre><p>The test set error is <strong>66%</strong>, which is not bad, though getting the best possible accuracy is not the goal of this article.</p><p><a href="https://github.com/Einnmann/DS_projects/blob/master/cats_and_dogs.py">Here</a> you can find the complete code.</p><p>Well done! Now you are ready to practice in deep learning using your images!</p><p>Thank you for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18515416bba1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/how-to-apply-a-cnn-from-pytorch-to-your-images-18515416bba1">How to apply a CNN from PyTorch to your images.</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making beautiful visualizations for a Django HTML page using Plotly]]></title>
            <link>https://medium.com/data-science/making-beautiful-visualizations-for-a-django-html-page-using-plotly-f4e97d3f6bc7?source=rss-d98b818da882------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4e97d3f6bc7</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[plotly]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[visualization]]></category>
            <dc:creator><![CDATA[Alexey Khrustalev]]></dc:creator>
            <pubDate>Sun, 02 Aug 2020 18:39:34 GMT</pubDate>
            <atom:updated>2020-08-05T13:05:04.294Z</atom:updated>
            <content:encoded><![CDATA[<h4>You don’t have to know Python or HTML to create an interactive visualization</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplotly.com%2F%7Eeinnmann%2F7.embed%3Fautosize%3Dtrue&amp;display_name=Plotly&amp;url=https%3A%2F%2Fchart-studio.plotly.com%2F%7Eeinnmann%2F7%2F&amp;image=https%3A%2F%2Fchart-studio.plotly.com%2Fstatic%2Fwebapp%2Fimages%2Fplotly-logo.8d56a320dbb8.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=plotly" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/58b1fc3f035219bab14c279070522cfe/href">https://medium.com/media/58b1fc3f035219bab14c279070522cfe/href</a></iframe><p>Hello everyone! Recently I had to do some visualizations for my university project, I’ve done some googling and haven’t found any simple guides on how to put Plotly plots on an HTML page.</p><p>Before we start I assume that you have running the Django HTML page. Why Plotly? Plotly allows you to make <strong>interactive</strong>, beautifully designed plots in a matter of minutes. Despite that originally Plotly is a Python library, we won’t use any Python programming today.</p><h4>For those who don’t have much time, here is the short guide:</h4><ul><li>Go to <a href="https://chart-studio.plotly.com/">Plotly Chart Studio</a>, create your account.</li><li>Click on the “Create” button and choose “Chart”.</li><li>Import your data and click on the “Trace” button.</li><li>Choose the type of plot you want.</li><li>Once you made your dream-plot, save it, setting all the settings to “Public”.</li><li>Share your plot, by copying iframe code from the “Embed” tab and past it to the body of your HTML page.</li></ul><p>Now for those who have time, let’s see in detail how we can get the same result, as you saw at the beginning.</p><h3>Step 1. Creating an account and uploading the data.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*QyF7uvf2DEmV7zdXvd5TOA.png" /><figcaption>Image by Author</figcaption></figure><p>We will use<a href="https://chart-studio.plotly.com/"> Plotly Chart Studio</a>. Once you’ve signed up, you’ll see “Create” in the upper-right corner.</p><p>To create our visualizations we have to import our data first. The data can be either CSV, single-sheet Excel file, link to a CSV file or the data got using SQL and Falcon SQL client.</p><p>Once you uploaded your data, you’ll see it in the table (red square) on the top of your screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LLdwuir_k8BhqdhRRKcJrA.png" /><figcaption>Plotly Studio, Image by Author</figcaption></figure><h3>Step 2. Choosing the type of plot and tracing the data.</h3><p>Now let’s look at the blue square on the picture and trace some data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H1-oY-ZP7HY7tSm8ZUrIMw.png" /><figcaption>Image by Author</figcaption></figure><p>You can choose between different types of plots. Here I chose the Choropleth Atlas Map since it allows me to get this nice Earth visualization.</p><h3>Step 3. Specifying location and values.</h3><p>Then you have to specify columns that will be used as locations and values. You also have to choose the location format, since it can be countries’ names or abbreviations and so on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R_VhMKeecsbXmYxp3SdEiA.png" /><figcaption>Image by Author</figcaption></figure><p>Here you can see all my settings, which you can use to get the same result as mine.</p><h3>Step 4. Saving and sharing the plot.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yhN_DNSGfT69uQMPhGLRDQ.png" /><figcaption>Image by Author</figcaption></figure><p>We are almost there! Click the “Save” button and choose “Public” for Plot and Grid.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a3XrxMOITOyGj3ONE1XMZA.png" /><figcaption>Image by Author</figcaption></figure><p>Finally, you click the “Share” button, choose “Embed” and copy iframe HTML code, which you can pass in the body of your HTML page.</p><p>Thank you for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4e97d3f6bc7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/making-beautiful-visualizations-for-a-django-html-page-using-plotly-f4e97d3f6bc7">Making beautiful visualizations for a Django HTML page using Plotly</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>