<?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 alessandro guarnieri on Medium]]></title>
        <description><![CDATA[Stories by alessandro guarnieri on Medium]]></description>
        <link>https://medium.com/@aleguarnieri91?source=rss-bd2c6995b736------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7RbhlS3eufUyBiSs6RHwbQ.jpeg</url>
            <title>Stories by alessandro guarnieri on Medium</title>
            <link>https://medium.com/@aleguarnieri91?source=rss-bd2c6995b736------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 16:38:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aleguarnieri91/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[Why a physics degree prepares you well to become a Data Scientist]]></title>
            <link>https://medium.com/codex/why-a-physics-degree-prepares-you-well-to-become-a-data-scientist-d096f625f6c4?source=rss-bd2c6995b736------2</link>
            <guid isPermaLink="false">https://medium.com/p/d096f625f6c4</guid>
            <category><![CDATA[physics]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[alessandro guarnieri]]></dc:creator>
            <pubDate>Fri, 27 Aug 2021 14:48:53 GMT</pubDate>
            <atom:updated>2021-08-30T00:58:52.705Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*6P6Q6nT9h_ivao5R14Ym1Q.jpeg" /></figure><p>Everybody knows what physics is and what it means to take a degree on this subject: a lot of maths, formulas, cerebral exercises, abstruse concepts, and complex laboratory experiments.</p><p>Of course, there is a complete world behind physics and the capacities needed to complete a physics degree depends on the University itself but one of the most important lessons I learned from graduating in physics is how to tackle unknown problems scientifically and that “It’s never over until it’s over”.</p><h3>The Science part of “Data science”</h3><p>Most people nowadays are talking about “data”: how to leverage them, how to secure them, how to get something out of them. What about the “science” part?</p><p>Throughout my years as a physics student, what fascinated me the most were the laboratory experiments when I could see and touch the physics concepts I was studying in the books. During every experiment what we were usually asked to do was to solve a problem by applying the concepts learned during the lessons.</p><h4>Experiments: Data Science and Physics</h4><p>During many simple laboratory experiences, we were starting from a physics law or concept and we were trying to creatively set up an experiment, trying to demonstrate if that law was still valid or not. We were not specifically told which instrument to use or what was the best way to proceed but the starting point (the physics law) and the end goal (demonstrate if it is valid or not within error margins) were clear.</p><p>So the first concept which I find being in common with Data Science is the importance of <strong>defining your starting point and your endpoint</strong>: the business problem to solve and what its solution will bring to your business.</p><p>Secondly what I think was the most exciting moment during the entire journey from the first year until the final dissertation at the end of the master&#39;s degree was the thesis period. During those months it was finally possible to perform real experiments and extensively trying to demonstrate a hypothesis.</p><p>In my case, as I was interested in biophysics and nanotechnology, my goal during the master thesis was to demonstrate if a certain dye was suitable to understand the behavior of calcium protein pumps at the single-molecule level. As said before, the problem and the goal were very clear from the beginning but what I want to highlight now is the iterative process I had to follow. First of all, it was necessary to decide the experiment condition, which means deciding what I wanted to measure, and then setting anything that could affect the measurement. After this, I had to prepare the instruments used during the experiment, perform the experiment, collect the results and analyze them. After the analysis, I had to summarize what I found and decide what was the next step.</p><p>Also in Data Science, it is necessary to perform experiments and what is more than necessary are the <strong>iterations </strong>done to reach a certain evaluation metric. These iterations depend on the specific problem which is being analyzed but generally, they are represented by changes to the features or the model hyperparameters based on the results of the previous experiment. This iterative process is well described by the <strong>CRISP-DM approach.</strong></p><h4>Common background</h4><p>This one is more obvious but not less important: the common knowledge background necessary to understand and apply physics or data science concepts.</p><p>Studying physics means studying nature’s laws, trying to describe, understand, and to a certain extent predict the behaviors of certain natural phenomena. The language we use to read the natural phenomena is, guess what, mathematics! All the physics laws are permeated with mathematical concepts and it is more than necessary to first know calculus, linear algebra, probability, statistics, and informatics to understand well physics.</p><p>It is not a surprise to see that all this knowledge is considered the basic foundation of any course of Data Science. This is because machine learning models, pipelines, recommendation systems, or other Data Science topics can be tackled and solved using tools that are built on mathematical and statistical concepts.</p><h4>The pleasure of discovery</h4><p>Another important common aspect between physics and data science is the fact that both try to understand or predict something that is not known and this definitely satisfies curious minds.</p><h3>Differences</h3><p>After pointing out what are in my opinion some of the common points between physics and data science, I want to pinpoint a difference: their goal. Physics is hard science and its goal is to expand human knowledge towards the natural phenomena, from micro to macro scales; on the other hand, data science has the goal of trying to solve business problems and making business processes more productive and efficient, is it applied to marketing or sells.</p><h3>Call to action</h3><p>I want to end this short overview with one or two questions: what are going to be data science applications in the future? Are they going to be only related to business or could they be useful to solve also another type of problems, may be useful for our society?</p><p>I think that motivation is what keeps you interested in what you do as a job so I always ask myself these questions, trying to reflect on which kind of impact any application of data science could have in the future. I would definitely like to know what you think about it!</p><p><a href="https://www.linkedin.com/in/alessandro-guarnieri-ag/">https://www.linkedin.com/in/alessandro-guarnieri-ag/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d096f625f6c4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/why-a-physics-degree-prepares-you-well-to-become-a-data-scientist-d096f625f6c4">Why a physics degree prepares you well to become a Data Scientist</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[When music is your passion…]]></title>
            <link>https://medium.com/analytics-vidhya/when-music-is-your-passion-9837aea155ea?source=rss-bd2c6995b736------2</link>
            <guid isPermaLink="false">https://medium.com/p/9837aea155ea</guid>
            <category><![CDATA[supervised-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[pyspark]]></category>
            <category><![CDATA[classification-algorithms]]></category>
            <dc:creator><![CDATA[alessandro guarnieri]]></dc:creator>
            <pubDate>Fri, 20 Aug 2021 13:58:02 GMT</pubDate>
            <atom:updated>2021-08-28T22:27:55.058Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ckVdfqqEdBn-57VE5Yyt4Q.jpeg" /><figcaption>Churn prediction with Spark ML</figcaption></figure><h3><strong>Project Overview</strong></h3><p>We all, at least once, listened to music through a streaming application and we enjoyed the possibility to quickly find our favorite singer and listen to her/his music as long as we wanted.</p><p>As for every application, there are some premium features available only if you decide to pay a certain amount of money and become a premium member.</p><p>As long as you are happy with the features offered by the app, you keep paying to keep your premium features but it can happen that after a while, for some reason, you decide to stop your payment or worse you decide to unsubscribe.</p><p><strong>What if we can leverage machine learning to predict if a user will unsubscribe?</strong></p><p>In the following paragraphs, I will show how I approached this mentioned problem, which is a common business problem called “churn prediction”. I will show how the available data were processed and how a machine learning pipeline, based on supervised machine learning techniques, was implemented.</p><p>Using Machine Learning models and pipelines to predict user’s churn here is a good way of proceeding because we can implement a model which can predict what the single user will do based on the previous behavior. Statistical methods and tests can give useful results but are not as granular and detailed as a machine learning model.</p><p>The final goal is to produce a model which can be used to classify users and predict if they will churn or not.</p><h3>Data Exploration and Visualization</h3><p>The dataset used in this project contains a subset of data related to users registered to Sparkify, a fictitious application for music streaming.</p><p>Data were loaded into a Spark dataframe to be analyzed. The schema, as follows, contains an ID for each user and session together with information about the user itself (gender, location…) and the actions done (visited page…), ordered by timestamp.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*cfZlZoAVk5aMW5QYlhwBzA.jpeg" /><figcaption>Dataframe schema</figcaption></figure><p>To start our analysis, EDA was done to understand better the content of the dataset.</p><p>The dataset contains <strong>278154 rows </strong>and <strong>225 different users.</strong> The <strong>features </strong>available before feature engineering are <strong>17</strong>, excluding the userId column.</p><p>Here are some results from the exploration (more can be found in the related <a href="https://github.com/AleGuarnieri/Sparkify-Capstone">Jupyter notebook</a>):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*6vMjYVr1Tqs2-sTyN4eYtg.jpeg" /><figcaption>Details on some columns of the dataframe</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*rNyP6aE2eC14p_WamNZUJA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*SbVD6XFHXHNh5T2ZuYMMbw.jpeg" /></figure><p>The most important data cleaning done during this phase was the removal of rows that had a null User Id. Moreover, it was interesting to notice that there were some null values for certain columns, since the action of the user was not related to a song, so for those rows the song’s author, name and duration were not populated.</p><h3>Data preprocessing and implementation</h3><h4>Label definition</h4><p>The goal of our analysis is to predict when a user will churn, so we added a column, the label, that defines if a user decided to cancel the subscription or not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*umgtL0FFxEZqDNALaMF1_Q.jpeg" /><figcaption>pySpark code to define label column “Churn”</figcaption></figure><p>The column “Churn” will be used as the target column for our model so that we can use it together with the feature columns to train and then test the machine learning models.</p><h4>Feature Engineering</h4><p>The features available in the dataset need to be manipulated to be used in a Machine Learning model.</p><p><strong>Categorical features</strong></p><p>“gender” was the only categorical feature used. In this case, it was necessary to convert the feature from categorical to numerical, using the One-Hot-Encoding technique.</p><p><strong>Numerical features</strong></p><p>The “page” column was used to build some of the numerical features, creating a count of the page actions for each user. Besides this, some other numerical features were added such as: “number of sessions per user”, “number of listened songs per user”, “average number of listened songs per user per session”</p><p><strong>Note on data leakage</strong></p><p>It was important to take into account data leakage when performing feature engineering, for example, it was necessary not to include “Cancellation confirmation” or “Cancel” in the features to avoid overfitting.</p><h3>Model Evaluation and Validation</h3><p>The ML models used in this project were Logistic Regression and Random Forest. This choice was done taking into account the nature of the problem, which is a classification problem, and the fact that the classes are imbalanced.</p><p>The training was performed on 80% of the data, while the remaining 20% was dedicated to validation.</p><h4>Hyper-parameters tuning</h4><p>Cross-Validation was performed on the training set so that we could tune the hyperparameters of the models and choose the best ones.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*HxL7oULcbhV2dau9wqjX3A.jpeg" /><figcaption>Definition of Cross-Validation, after defining pipeline, hyperparameters and evaluator</figcaption></figure><p>Here are the best values for the parameters after hyper-parameters tuning</p><p><strong>Logistic Regression:</strong></p><ul><li>regParam: 0.1</li><li>elasticNetParam: 0.0</li></ul><p><strong>Random Forest:</strong></p><ul><li>numTrees: 50</li><li>maxDepth: 15</li></ul><h4><strong>Metrics</strong></h4><p>The metrics used during cross-validation were f1-score and AreaUnderPR because the classes were imbalanced and predicting well the positive one was more important.</p><p>On the best model, other metrics were also computed for the positive class, as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*_7aasV4-sZrJPBH2_TayIQ.jpeg" /><figcaption>Metrics on the test dataset, after training (evaluation metric: f1-score)</figcaption></figure><p>The trained Random Forest model was used also to predict labels of the training set and as it is shown below, the metric’s values on the training set are higher so the model is maybe overfitting:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*OzPP0-hxDNlITiUwqfxs6A.jpeg" /></figure><h4>Side notes on evaluation</h4><p><strong>Parameters responsible for overfitting</strong></p><p>It is interesting to notice that out of the two parameters tuned on the Random Forest model, the one more affecting the overfitting was the maxDepth one. It was very important to tune it correctly and for this was necessary to perform different experiments.</p><p><strong>F1-score in pySpark: challenges</strong></p><p>The F1-score metric was used as an evaluation metric and for fine-tuning of the model. The Spark class MulticlassClassificationMetrics performs an average of the f1-scores of the classes so we had an f1-score of 0.58 for Logistic Regression and 0.66 for Random Forest.</p><p>Despite this, if we look at the f1-score for the positive class (shown above), we see that the f1-score of the Logistic Regression is 0. This has an impact both on the cross-validation process and on the perception we have of the Logistic Regression model, which could seem better than it is in reality.</p><p><strong>Validation</strong></p><p>Using <em>collectSubModels </em>during cross-validation, it was possible to keep the models of each fold and then evaluate them as well to test the robustness of the best model. The selected model appears not to be robust and a bit too dependent on the chosen dataset. This issue could be due to class imbalance and could be also solved using more data.</p><h3>Final reflections and improvements</h3><p>The chosen model to solve our churn prediction problem is the Random Forest one because as shown above it outperformed the Logistic Regression one, based on the evaluation metrics used. After hyper-parameters tuning the model scored a 0.66 F1-score and specifically for the positive class a 0.28 F1-score. Out of all the features used, the most important ones to predict a churn were “ThumbsDown” and “ThumbsUp”.</p><p>The most important part of the whole process was the hyper-parameter tuning: it was important to understand the meaning of the metrics used in relation to the business problem to solve and it was important to check not only the overall F1-score but also the one of the positive class.</p><p><em>Are we finally ready to help Sparkify predict if their user will churn or not?</em></p><p>Well, a big step was done but there could be many other steps to improve both the preprocessing and the model tuning.</p><p>For example, it could be useful to run this code into a distributed cluster to leverage the power of Spark and see if a better performance could be achieved.</p><p>It would also be interesting to correct the class imbalance or to add additional features, for example, a categorical feature such as the last status available for each user (free or paid) or other numerical features.</p><p>Finally, it could also be useful to use different models or create an ensemble of models.</p><p><a href="https://github.com/AleGuarnieri/Sparkify-Capstone">You can find the complete Jupyter Notebook here</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9837aea155ea" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/when-music-is-your-passion-9837aea155ea">When music is your passion…</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What about Airbnb in Milan?]]></title>
            <link>https://medium.com/analytics-vidhya/what-about-airbnb-in-milan-737752c14680?source=rss-bd2c6995b736------2</link>
            <guid isPermaLink="false">https://medium.com/p/737752c14680</guid>
            <category><![CDATA[choropleth-map]]></category>
            <category><![CDATA[milan]]></category>
            <category><![CDATA[airbnb]]></category>
            <category><![CDATA[crisp-dm]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[alessandro guarnieri]]></dc:creator>
            <pubDate>Wed, 02 Sep 2020 21:08:24 GMT</pubDate>
            <atom:updated>2021-08-28T22:25:39.761Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*JAkhtCNXcEHC52GzANKxQg.jpeg" /></figure><p>Short business trips? Holidays to escape from your daily life? Just moved in town and looking for an apartment? Smart working by the sea? Airbnb is an answer to all these questions but how to decide which apartment or neighborhood is the right one for you (and your pockets)?</p><p>I live in Milan and I know the city but I asked myself, from the perspective of a tourist, where would I want to book an Airbnb accommodation, so I came up with some questions:</p><ol><li>Which are the <strong>most expensive neighborhoods</strong> in Milan for Airbnb accommodations and where are they located?</li><li>Which are the <strong>neighborhoods with the highest density</strong> of Airbnb accommodations in Milan?</li><li>Is there a <strong>correlation between price and density</strong> of Airbnb accommodations in this area?</li><li>Is it possible to <strong>predict the price of an Airbnb accommodation</strong> given some of the information on its listing on the Airbnb site?</li></ol><h4>Distribution of Airbnb accommodations’ prices</h4><p>As you might expect, the most expensive Airbnb accommodations are all located in the city center. There are only a few exceptions, as highlighted in the following map, where two or three neighborhoods show a higher average price compared to the surroundings in the south and east areas.</p><p>Is there anything else to say about that high average price in that small neighborhood of the city center?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*y_27-y78CXavTuqpc48BGA.png" /></figure><h4>Comparison between density and prices distribution</h4><p>Most of the Airbnb accommodations are located in the city center, as shown in the following graph. Around 54% of the accommodations are all located in only a few (10) of the city center neighborhoods, which are also the most expensive ones.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*zIXFrXpO2w7B_S9sq25ZaQ.png" /></figure><p>At a first sight, it is possible to say that the city center is both dense and with the highest average prices for Airbnb accommodations but taking into consideration the seven neighborhoods with the highest average price, there are a few exceptions to consider:</p><ul><li>The neighborhoods with the two highest average prices (QUINTOSOLE and GIARDINI PORTA VENEZIA) correspond to the lowest density class.</li><li>Only two neighborhoods are both highly dense and with a high average price (DUOMO and BRERA), the remaining three are in lower density classes.</li></ul><h4>You can now book your Airbnb accommodation</h4><p>The neighborhoods with the most expensive Airbnb accommodations, which are also among the ones with the highest density, are DUOMO and BRERA. No worries, there is always space in Milan if you are willing to spend a little bit more than average!</p><p>GARIBALDI REPUBBLICA, GUASTALLA and GHISOLFA have a lower average price and a lower density compared to the previous two. In my opinion, GARIBALDI would then be the perfect option, considering also the nice places to visit and the nightlife in the very near neighborhood ISOLA.</p><p>The most expensive one is GIARDINI PORTA VENEZIA, which has also a low amount of available places: it seems that this can be called literally an <em>exclusive </em>neighbourhood.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/433/1*Ihm277YdJQoRzto2LDMuxg.png" /><figcaption>The average price of the most expensive neighborhoods and related number of listings - June 2020</figcaption></figure><p>If you are curious about the data and the processing behind this post, feel free to reach my GitHub repository <a href="https://github.com/AleGuarnieri/Airbnb-accomodations-analysis-in-Milan">https://github.com/AleGuarnieri/Airbnb-accomodations-analysis-in-Milan</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=737752c14680" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/what-about-airbnb-in-milan-737752c14680">What about Airbnb in Milan?</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>