<?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 Umut Kocatas on Medium]]></title>
        <description><![CDATA[Stories by Umut Kocatas on Medium]]></description>
        <link>https://medium.com/@umut.kocatas41?source=rss-44d278718ad0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*x3ZCPnLMZ5LeYNvhnrab8w.jpeg</url>
            <title>Stories by Umut Kocatas on Medium</title>
            <link>https://medium.com/@umut.kocatas41?source=rss-44d278718ad0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 15:02:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@umut.kocatas41/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[Missing Data Imputation Aproaches and Application]]></title>
            <link>https://medium.com/@umut.kocatas41/missing-data-imputation-aproaches-and-application-e3f9d1b06f98?source=rss-44d278718ad0------2</link>
            <guid isPermaLink="false">https://medium.com/p/e3f9d1b06f98</guid>
            <category><![CDATA[missing-data-imputation]]></category>
            <category><![CDATA[multiple-imputation]]></category>
            <category><![CDATA[missing-data]]></category>
            <category><![CDATA[data-imputation]]></category>
            <category><![CDATA[imputation-methods]]></category>
            <dc:creator><![CDATA[Umut Kocatas]]></dc:creator>
            <pubDate>Sat, 01 Jun 2024 14:24:23 GMT</pubDate>
            <atom:updated>2024-06-01T14:24:23.384Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x8F6wPaHlSuTZ829L5I7XQ.jpeg" /></figure><p><strong><em>Hello everyone ! In this article, I will first talk about Data Imputation Approaches. Then, in the application part, I will try to show you the nuances of the results obtained in the Machine Learning modeling part with the help of 4 different data imputation methods and visuals.</em></strong></p><h3>MISSING DATA IMPUTATION APROACHES</h3><h4>Missing Values</h4><p>Missing observations in data analysis can negatively affect the accuracy of analyzes and model performance. Filling in missing observations is important to obtain more accurate and reliable results. Considering that the data is generally scattered, it is possible that there are missing values. In this situation; It can be said that the values ​​are not created as a result of error, have been deleted or do not exist at all.</p><p>Handling missing values ​​is one of the most important steps in the data pre-processing stage. Because in some machine learning models, unfilled data creates problems in the analysis and modeling part. Of course, there are exceptions to this, such as machine learning models such as <strong>Naive Bayes</strong>, <strong>XGBoost</strong> and <strong>LightGBM</strong>.</p><p>In addition, during the completion of missing data, there are 3 critical situations that need to be known regarding the formation of missing data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*2BtVxXYSvcHUfcjjtqG8Xw.jpeg" /></figure><ul><li><strong>Missing Completely at Random (MCAR):</strong> Missing data can be considered completely random if the probability of missing data is the same for each case.</li><li><strong>Missing at Random (MAR):</strong> If the probability of missing data is the same for only observed data, the data can be said to be missing at random.</li><li><strong>Missing not at Random (MNAR):</strong> This is a situation where the reason for missing values ​​is unknown.</li></ul><h4>Data Imputation Techniques</h4><p>There are many ways to approach missing data. Like simply deleting it or assigning it more systematically by spending too much time on it. We will consider these two approaches as the main topic and continue to examine them in more detail with their sub-branches.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YkDvGIIAWPSpOPh4n_GEHA.jpeg" /></figure><p><strong>DATA REMOVAL</strong></p><ul><li><strong>Pairwise Deletions:</strong> Deleting only missing values. It is assumed that these missing values ​​occur completely randomly. (<strong>MCAR</strong>)</li><li><strong>Dropping Entire Rows:</strong> Again, it is assumed that missing values ​​occur completely randomly (<strong>MCAR</strong>) and all rows with missing values ​​are deleted.</li><li><strong>Dropping Entire Columns:</strong> Although this method is not preferred when the number of missing values ​​in the column is quite high and there is a variable that has no correlation with the target variable or is of no importance, the entire column can be deleted.</li></ul><p><strong>DATA IMPUTATION</strong></p><p><strong>— General</strong></p><p>In general terms, assignment is the process of replacing missing values ​​with appropriate values. Although they are generally simple assignment methods, they vary depending on the type of data.</p><p><strong>For non-Time Series</strong></p><p>These are methods used other than time series. In time series, slightly different procedures are applied compared to these imputation methods.</p><ul><li><strong>Imputing with a constant: </strong>Replacing missing values ​​with a specified <strong>constant</strong> value.</li><li><strong>Imputing with mean:</strong> Replacing the <strong>mean</strong> of the column with missing values ​​with the missing values. It is preferred in cases where the distribution is normal.</li><li><strong>Imputing with median:</strong> Replacing the <strong>median</strong> of the column with missing values ​​with the missing values. It is preferred in cases where the distribution is skewed.</li><li><strong>Imputing with mode:</strong> Used in <strong>categorical</strong> variables. It is replacing the <strong>mode</strong> of the column with missing values ​​with the missing values.</li></ul><p><strong>Time Series</strong></p><p>The imputation of null values ​​in time series differs from other cases. There are three approaches used here.</p><ul><li><strong>Forward fill:</strong> The <strong>bfill()</strong> method, used in the pandas library in Python, takes the last value before the empty value and fills it.</li><li><strong>Backfill:</strong> The <strong>bfill()</strong> method, used in the pandas library in Python, fills in the missing values ​​in the data frame backwards.</li><li><strong>Linear Interpolation:</strong> There is an alternative to the forward fill and backfill method that eliminates the problem of filling missing values ​​in time series. This method, called linear interpolation, can be applied on data accurately sorted by timestamp and is a technique used to estimate missing values ​​between 2 known data points.</li></ul><p><strong>— Advanced</strong></p><p><strong>Multivariate Imputation</strong></p><p>Using several concepts such as correlation, covariance, and Euclidean distance between two points, this technique estimates missing values ​​based on the relationship between data in another column.</p><p><strong>Mice(Multivariate Imputation by Chained Equations)</strong></p><p>This method, also known as iterative imputer, estimates missing values ​​using a more complex machine learning model that works with many variables. In this method, the selected missing feature column is defined as the target variable, that is, <strong>y</strong>, and the other feature columns are accepted as <strong>X</strong>. It then performs multiple regression on random data samples, then averages the multiple regression values ​​and uses that value to impute the missing value.</p><p><strong>KNN-Based</strong></p><p>It is a machine learning method in which Euclidean, Manhattan and Mahapolnis distance metrics are used in the calculation metrics. In this method, K-nearest neighbor (KNN) assignment is calculated using values ​​from the n_neighbors nearest neighbors of the column with missing data, which have a value for that column. Characteristics of neighbors are averaged equally or weighted by the distance to each neighbor.</p><blockquote><strong>Advantages</strong><br><strong>-</strong> Machine learning algorithms are used.<br><strong>-</strong> More accurate estimates can be made for missing data.<br><strong>Disadvantages</strong><br><strong>-</strong> In cases where data is large, training times may be longer.<br><strong>-</strong> It may not always perform well.</blockquote><p><strong>Tree-Based</strong></p><p>Another effective approach to filling in missing values ​​is classification and regression tree algorithms (<strong>CART</strong>). These algorithms provide effective methods for filling in missing observations.</p><p>In this method using classification trees or regression trees, classification trees can be used to estimate missing values ​​in categorical variables, while regression trees can be used to estimate missing values ​​in numerical variables. This method allows estimating missing categorical data or missing numerical data based on other available features.</p><p><strong>Steps</strong></p><ul><li>A classification/regression tree model is created using data with no missing observations.</li><li>This model is trained with non-missing data.</li><li>The trained model is used to predict missing values.</li></ul><blockquote><strong>Advantages</strong><br>- These methods generally provide high accuracy because they utilize the entire data to estimate missing values.<br>- Can be used for both categorical and numerical variables.<br><strong>Disadvantages</strong><br>- Training these models on large data sets can be time-consuming and computationally costly.<br>- There is a risk of overfitting of the model, so careful model selection and parameter tuning is required.</blockquote><h4>DATA IMPUTATION APPLICATION</h4><p>For the missing data imputation application, we will use a classification data set called <strong>water_potability</strong> in Kaggle, which contains information about various factors affecting water quality and whose target variable is water drinkable/non-drinkable.</p><pre>df = pd.read_csv(&#39;/kaggle/input/water-probability/water_potability.csv&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*G92oIy2eYQjt08pGso0ewQ.png" /></figure><ul><li>Numbers and percentages of missing values ​​in our data set</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*ghpPuWdMmMbB2E7ZaQ2DmA.jpeg" /></figure><ul><li>Columns with missing values ​​are: ‘<strong>ph’</strong>, ‘<strong>Sulphate’</strong> and ‘<strong>Trihalomethanes’</strong>.</li><li>We will use 4 different methods to assign missing values. We will apply these 4 different methods to 4 different copies we created from the data set.</li></ul><blockquote>— Mean Imputation<br> — K-Nearest Neighbors Imputation<br> — Multiple Imputation (MiceForest)<br> — Predictive Modeling Imputation (RandomForestRegressor)</blockquote><p><strong>1. Mean Imputation</strong></p><pre>for col in df.columns[df.isnull().any()]:<br>      df_mean[col].fillna(df_mean[col].mean(), inplace=True)</pre><p><strong>2. K-Nearest Neighbors Imputation</strong></p><pre>knn_imp = KNNImputer(n_neighbors=4)<br>df_knn = pd.DataFrame(knn_imp.fit_transform(df_knn), columns=df_knn.columns)</pre><p><strong>3. Multiple Imputation (MiceForest)</strong></p><ul><li>To use this method, you must first install <strong><em>miceforest</em></strong> if it is not installed.</li></ul><pre>pip install miceforest</pre><pre>from miceforest import ImputationKernel<br><br>mice_kernel = ImputationKernel(data=df_mice, save_all_iterations=True, random_state=41)<br>mice_kernel.mice(2)<br>df_mice = mice_kernel.complete_data()</pre><p><strong>4. Tree-Based Imputation (RandomForestRegressor)</strong></p><pre>from sklearn.ensemble import RandomForestRegressor<br>rf = RandomForestRegressor()</pre><pre>def fill_missing_numerical(df, col):<br><br>known = df[df[col].notnull()]<br>unknown = df[df[col].isnull()]<br>X = known.drop(df.columns[df.isnull().any()], axis=1) # features<br>y = known[col] #target<br>rf.fit(X, y)<br>unknown[col] = rf.predict(unknown.drop(df.columns[df.isnull().any()], axis=1))<br>df[col].fillna(unknown[col], inplace=True)</pre><pre>for col in df_rf.columns[df_rf.isnull().any()]:<br>    fill_missing_numerical(df_rf, col)</pre><ul><li>We have applied 4 different imputation methods to 3 samples with missing values ​​in the data sets. As a result of the application, we can observe the distribution of 4 different methods in the filled data.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*GUk7Jrlykc6HR88KAXYLEw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/1*lwgphR7JoTAaw8YepsO7RA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/1*_aWUsvYnxUOZIBET-7-AuA.png" /></figure><ul><li>We can also observe the differences between these methods, even if they are small, in their effects on the correlation.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a-bE5AJGvxewVNYETcZ5mg.png" /></figure><ul><li>In the Machine Learning section, we will use 4 different models to observe the different effects of our imputation application.</li></ul><blockquote>— Gradient Boosting Classifier<br> — Random Forest Classifier<br> — XGBoost Classifier<br> — LightGBM</blockquote><ul><li><strong>Confusion Matrix metrics</strong> resulting from the models.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fPlQXXSVnyQphtcoJpuuVQ.png" /></figure><ul><li><strong>ROC-AUC Curves</strong> resulting from the models.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/989/1*zaeiaca9W9gTfaGGOdbfzQ.png" /></figure><p><strong>Conclusion: </strong>When we look at the detailed graphics, we can understand that; Among the 4 different filling methods we chose for this data set, <strong>Random Forest Regressor Imputation</strong>, which is a predictive imputation method, and <strong>Random Forest Classifier</strong>, which is a predictive imputation method, gave better results. These results may increase or decrease depending on different imputation methods or the use and optimization of different machine learning methods.</p><p>For more and details about our code: <br><a href="https://www.kaggle.com/code/umutkocata/water-potability"><strong>https://www.kaggle.com/code/umutkocata/water-potability</strong></a></p><p><strong><em>Thank you for reading.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3f9d1b06f98" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Net Promoter Score (NPS) & Sentiment Analysis and Application]]></title>
            <link>https://medium.com/@umut.kocatas41/net-promoter-score-nps-sentiment-analysis-and-application-a8bbb11e2e9c?source=rss-44d278718ad0------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8bbb11e2e9c</guid>
            <category><![CDATA[nps]]></category>
            <category><![CDATA[sentiment-analysis]]></category>
            <category><![CDATA[net-promoter-score]]></category>
            <dc:creator><![CDATA[Umut Kocatas]]></dc:creator>
            <pubDate>Tue, 05 Mar 2024 13:54:28 GMT</pubDate>
            <atom:updated>2024-03-05T13:54:28.711Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SjBPGVozvVk-lsYekiZbIg.jpeg" /></figure><p><strong><em>Hello everyone ! In this article I will first talk about the Net Promoter Score method. Then, I will explain Sentiment Analysis with an application because it is similar to categorizing the satisfaction of a situation as in Net Promoter Score.</em></strong></p><h4>NET PROMOTER SCORE (NPS)</h4><p><strong>Net Promoter Score</strong> is a scoring method that was introduced by Satmetrix Systems, Bain &amp; Company and Fred Reichheld in the early 2000s, and its recognition increased significantly with an article published in Harvard Business Review in 2003. The purpose of this scoring method is to measure the value or loyalty of customer relationships a business has. It helps company managers to summarize the customer experience with the obtained values.</p><p>Net Promoter Scoring method can be measured between certain periods such as 3–6–12 months through different channels such as mobile applications, websites, e-mails. As a result of these periods, companies obtain an NPS score and some gains can be made accordingly.</p><p><strong>These gains;</strong></p><ul><li>Quick feedback can be received from a large portion of existing customers. This way scores can easily be summed up on a specific scale.</li><li>The change of the score over time can be examined. This helps understand the trend and the factors that influence it, helping the company focus on the issues that matter most to customers.</li><li>Allows you to identify and focus on initiatives aimed at retaining and increasing customers.</li></ul><p><strong>Accordingly, NPS Scores of some companies:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2bgOCGa8-kg0ZRH2SeIjKA.jpeg" /></figure><p><strong>How to Calculate Net Promoter Score?</strong></p><p>The working principle of this scoring method actually starts with those simple questions that everyone knows;</p><p><strong><em>If you were to give a score from 0 to 10…</em></strong></p><ul><li>How likely are you to recommend us to your acquaintances?</li><li>How likely is it that you would recommend our company to a friend or your circle of friends?</li><li>How likely are you to recommend our brand to a friend or your circle of friends?</li><li>Could you rate your satisfaction with the service you received?</li><li>Would you recommend the service you received from our company?</li></ul><p>When calculating the Net Promoter Score, the answers given by users are classified according to their point values ​​as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*NT6RC8-5n470zoptGv7MVg.jpeg" /></figure><p><strong>Promoters:</strong> Consists of users who rate<strong> 9</strong> and <strong>10</strong>. These people are highly satisfied with the situation requested in the survey and are the ones most likely to recommend it to others.<br><strong>Passives:</strong> Consists of users who give values <strong>​​7 </strong>and <strong>8</strong>. These people do not actively recommend the company and do not form an attitude that will negatively affect the company. Additionally, liabilities are not included in NPS calculations.<br><strong>Detractors:</strong> Consists of users who give values ​​between <strong>0</strong> and <strong>6</strong>. These people are people who are dissatisfied with the situation requested in the survey and are least likely to recommend it to others.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*WrVWPl5MzkS87mwQek7-Ww.jpeg" /></figure><p>The Promoters and Detractors numbers obtained as a result of the survey are compared and a percentage score between <strong>-100 </strong>and <strong>+100 </strong>is obtained. This score becomes the company’s NPS score.</p><p><strong>NPS = (Number of Promoters — Number of Detractors) x 100 / (Number of Responders)</strong></p><p><strong>For example;</strong><br>-Number of People Participating in the Survey: <strong>120</strong><br>-Promoters: <strong>66</strong><br>-Detractors: <strong>36</strong></p><p>NPS = (66–36)*100 / 120 = <strong>25 NPS</strong></p><h4>NET PROMOTE SCORE APPLICATING</h4><p>For the <strong>NPS</strong> application, we will use a data set called <strong>Airline Reviews</strong>, which evaluates the airline companies in Kaggle. In this data set, we have a variable called <strong>“Overall_Rating”.</strong> This variable is an evaluation ranging from 0 to 10, measuring the satisfaction of airline companies.</p><pre>df = pd.read_csv(&quot;/kaggle/input/airline-reviews/Airline_review.csv&quot;)</pre><p>After the Exploratory Data Analysis (EDA) process of our data set, we extract the variable we need from our data set and perform the scoring process.</p><pre># Creating a new column with the NPS profile of each client, to facilitate calculating NPS<br>def define_profile(score):<br>    if score &lt;= 6:<br>        return &#39;Detractor&#39;<br>    elif score &lt;= 8:<br>        return &#39;Passive&#39;<br>    else:<br>        return &#39;Promoter&#39;<br><br>df[&#39;PROFILE&#39;] = df[&#39;OVERALL_RATING&#39;].apply(define_profile)</pre><p>We group the obtained score according to passenger classes and visualize the data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*hNVDdEPqvbvfcszTPN8QJQ.png" /></figure><p>We also plot this score over a specific date range and according to passenger classes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TsJ11vT3wbjjF8v09VeL8A.png" /></figure><h4>SENTIMENT ANALYSIS</h4><p>Sentiment Analysis is an analysis method, also known as Opinion Mining, that captures the ideas, emotions and feelings expressed in the comments made by users and converts them into numerical metrics.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*doCMYx-_BnYIWDeWsQ1ieQ.jpeg" /></figure><p>With this method, today, with the automation of processes and natural language processing, customer reviews, financial news, social media, etc. Human emotions and feelings are analyzed by interpreting the data. However, these analyzes can help companies, service providers or created brands in some areas.</p><p><strong>These;</strong></p><ul><li>Real-time detection of customers or users satisfied with the service.</li><li>Identifying positive or negative comments and expressions.</li><li>Taking precautions before crises occur.</li><li>Creating better products and customer services.</li><li>Measuring the performance of the results obtained from the campaigns.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GX8L-4DFKtbrUFn_uJMlEQ.jpeg" /></figure><p><strong>Fine-grained sentiment analysis </strong>aims to divide user satisfaction into more precise and sharp levels. These are classified as very positive, positive, neutral, negative and very negative. In this way, it allows evaluation of the sharpness of the expressions.</p><p><strong>Aspect-based analysis </strong>is used to measure the positive or negative response of users who have used a particular product or service to measure their satisfaction with a specific component of that product. <strong>For example</strong>, a customer’s comment saying that a product’s battery life is low will be perceived by the analysis system only as it relates to the battery, not as a whole.</p><p><strong>Intent-based analysis </strong>consists of interpreting customers’ product or service purchasing preferences while companies conduct market research. <strong>For example</strong>, this method, based on opinion mining, can create customer-based targeted campaigns by companies analyzing comments containing words such as deals and discounts.</p><p><strong>Emotion detection analysis </strong>is a deeper and more complex analysis method. This analysis method is the interpretation of the user’s psychological state while writing the text. These interpretations convey joy, anger, sadness, regret, etc. through one’s choice of words. It consists of emotions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*GiGnjiP_bC18lWmJMQK6rA.jpeg" /></figure><p><strong>Rule-based:</strong> Positive and negative words in the data set are counted. If there are many positive words, the system is positive, if the opposite is negative, a neutral emotional sensitivity is detected.</p><p><strong>Machine Learning:</strong> This method is based on the subsequent analysis of the data set trained with machine learning techniques. In this section, machine learning techniques such as neural networks and deep learning can be used to classify the text according to positive, negative or neutral emotions. In the next step, words are extracted from the desired text.</p><p><strong>Hybrid Approach:</strong> It is the combination of machine learning and rule-based methods. The advantage of this method is that its accuracy is higher.</p><h4>Sentiment Analysis Usage Areas</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*TmQtm__dma3fajZ6CdnmXw.jpeg" /></figure><p><strong>Social Media Monitoring:</strong> Companies can learn how their customers feel and think about themselves on social media. In this way, they can focus on issues such as customer sentiment, potential customers or precautions to be taken.</p><p><strong>Brand Monitoring:</strong> Brands have information about themselves from every channel on the internet. For these brands, this is both statistical data and learning about their feelings. This allows for good management of product launches, campaigns, potential crises, etc.</p><p><strong>Customer Service:</strong> Using text classification, incoming support inquiries can be automatically organized by topic and urgency. In this way, directing them to the right department and handling the most urgent ones immediately ensures the correct interaction between customers and the brand. This increases efficiency, reduces churn and contributes to customer retention.</p><p><strong>Market Research:</strong> It helps brands understand all kinds of market research. These may be some market research such as product diversification, product or service promotion, following trends, comparing the product or service of a rival brand. In this way, some predictions are made, the dynamics are understood and necessary studies can be carried out on the target audience.</p><h4>SENTIMENT ANALYSIS APPLICATING</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*qIMdzNQQdeeJVW03vpESSw.png" /></figure><p>For the Sentiment analysis application, we will continue to use the data set called<strong> Airline Reviews</strong>, which evaluates the airline companies in Kaggle.</p><p>For the application, we need to install the library called <strong>vaderSentiment</strong> first.</p><pre>pip install vaderSentiment</pre><p>Then we call our function called <strong>SentimentIntensityAnalyzer</strong> from <strong>vaderSentiment</strong> in the <strong>vaderSentiment</strong> library and create our object.</p><pre>from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer<br><br>analyzer = SentimentIntensityAnalyzer()</pre><h4>NOTE</h4><p>SentimentIntensityAnalyzer takes a piece of text as input and returns a dictionary called <strong>polarity_scores()</strong> containing the sentiment scores of the text. This dictionary has 4 outputs:</p><blockquote><strong>neg:</strong> the negative sentiment score (between 0 and 1)<br><strong>neu: </strong>the neutral sentiment score (between 0 and 1)<br><strong>pos: </strong>the positive sentiment score (between 0 and 1)<br><strong>compound:</strong> the overall sentiment score (between -1 and 1)</blockquote><p><strong>For example;</strong></p><pre>text = &quot;I love Python!&quot;<br>scores = analyzer.polarity_scores(text)<br>print(scores)</pre><p><strong>Output</strong>.</p><pre>{&#39;neg&#39;: 0.0, &#39;neu&#39;: 0.238, &#39;pos&#39;: 0.762, &#39;compound&#39;: 0.6369}</pre><p>Going back to our data set, we are preparing our function.</p><pre>def analyze_sentiment_vader(text):<br>    sentiment_scores = analyzer.polarity_scores(text)[&#39;compound&#39;]<br>    if sentiment_scores &gt;= 0.05:<br>        return &quot;Positive&quot;<br>    elif sentiment_scores &lt;= -0.05:<br>        return &quot;Negative&quot;<br>    else:<br>        return &quot;Neutral&quot;</pre><p>After Exploratory Data Analysis (EDA) processes of our data set, we will use 2 variables named “<strong>Review</strong>” and “<strong>Review_Title</strong>” as input.</p><pre># Apply sentiment analysis to your DataFrame<br>df[&#39;STMNT_REVIEW_TITLE&#39;] = df[&#39;REVIEW_TITLE&#39;].apply(analyze_sentiment_vader)<br>df[&#39;STMNT_REVIEW&#39;] = df[&#39;REVIEW&#39;].apply(analyze_sentiment_vader)</pre><p>We have two new variables that have 3 classes. <strong>Positive</strong>, <strong>negative</strong> and <strong>neutral</strong>.</p><p>Sentiment analysis of comments and comment titles according to passenger class.<br><strong>Note:</strong> This code is for sentiment analysis of comments. The one for the comment title can be obtained by simply replacing “<strong>STMNT_REVIEW</strong>” with “<strong>STMNT_REVIEW_TITLE</strong>”.</p><pre># Calculating the % of each profile by SEAT_TYPE<br>profile_percentage2 = df.groupby([&#39;SEAT_TYPE&#39;, &#39;STMNT_REVIEW&#39;]).size() / df.groupby(&#39;SEAT_TYPE&#39;).size() * 100<br><br># For &quot;STMNT_REVIEW_TITLE&quot;,<br>#profile_percentage2 = df.groupby([&#39;SEAT_TYPE&#39;, &#39;STMNT_REVIEW_TITLE&#39;]).size() / df.groupby(&#39;SEAT_TYPE&#39;).size() * 100<br><br>profile_percentage2 = profile_percentage2.unstack().fillna(0)</pre><pre># Defining colors for profiles<br>colors = {&#39;Positive&#39;: &#39;green&#39;, &#39;Neutral&#39;: &#39;sandybrown&#39;, &#39;Negative&#39;: &#39;red&#39;}</pre><pre># Plotting the stacked bar chart<br>ax = profile_percentage2.plot(kind=&#39;bar&#39;, stacked=True, color=[colors[col] for col in profile_percentage2.columns])<br><br>plt.xlabel(&#39;Seat Type&#39;)<br>plt.ylabel(&#39;Percentage&#39;)<br>plt.title(&#39;Sensitivity Analysis of Comments Regarding Seat Type&#39;)<br><br># Displaying legend with custom labels<br>handles, labels = ax.get_legend_handles_labels()<br>ax.legend(handles=[plt.Rectangle((0, 0), 1, 1, color=colors[label]) for label in labels], labels=labels)<br><br># Adding labels with percentage values ​​on top of the bars<br>for container in ax.containers:<br>ax.bar_label(container, fmt=&#39;%.1f%%&#39;, label_type=&#39;center&#39;, fontsize=10, color=&#39;white&#39;)<br><br><br>plt.ylim(0, 150)<br>plt.tight_layout()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5_F1UjoV4VbmK5LXmoAKbA.jpeg" /></figure><p>Our code worked very well, and the sentiment analysis method allowed us to observe the sentiment distribution graph of the comments and comment titles made by passengers who preferred the airline, according to passenger classes.</p><p>For more and details about our code: <a href="https://www.kaggle.com/code/umutkocata/airline-review-step-by-step-97-acc"><strong>https://www.kaggle.com/code/umutkocata/airline-review-step-by-step-97-acc</strong></a></p><p><strong><em>Thank you for reading.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8bbb11e2e9c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Recommender Systems & Association Rules with Apriori]]></title>
            <link>https://medium.com/@umut.kocatas41/recommender-systems-association-rules-with-apriori-da15fc0f28ab?source=rss-44d278718ad0------2</link>
            <guid isPermaLink="false">https://medium.com/p/da15fc0f28ab</guid>
            <category><![CDATA[association-rule-mining]]></category>
            <category><![CDATA[market-basket-analysis]]></category>
            <category><![CDATA[apriori-algorithm]]></category>
            <category><![CDATA[recommendation-system]]></category>
            <dc:creator><![CDATA[Umut Kocatas]]></dc:creator>
            <pubDate>Wed, 28 Feb 2024 11:45:05 GMT</pubDate>
            <atom:updated>2024-02-28T11:45:05.027Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mkprT_pw4CUg7YCF5RxLQQ.jpeg" /></figure><p><strong><em>Hello everyone ! In this article, I`m going talk about recommendation systems. Then, I will explain Association Rule Learning, which is the most frequently used area in recommendation systems, and finally its application with the Apriori algorithm.</em></strong></p><h4>RECOMMENDATION SYSTEMS</h4><p>There is a large amount of data on the internet, including the comments users write for products, the scores they give to products, their feedback, or details about their purchases. Recommendation systems are based on analyzing this data from users, making a prediction and recommending the right product to the relevant user. It is especially used in e-commerce sites when the customer recommends another product during shopping or on the sales confirmation screen. In this way, companies can run campaigns for the next product, offer discounts, in short, companies can turn this into a sales advantage.</p><p><strong>Types of Recommender Systems</strong></p><ul><li><strong>Simple Recommender Systems</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*kVwu6IpzDN0tbY70NuhIzg.jpeg" /></figure><p>These are general suggestions made with business knowledge or simple techniques. For example, the highest rated products in their categories, the most liked ones, etc..</p><ul><li><strong>Association Rule Learning</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*tvu07dUXr3zVg-46YzYWWQ.jpeg" /></figure><p>Analyzing data based on the frequency of products being sold together is called <strong>Association Analysis</strong>, and learning the rules created by this analysis is called <strong>Association Rule Learning</strong>. This structure, called basket analysis, provides the opportunity to make certain recommendations based on the probabilities derived after the analysis of the products purchased together.</p><ul><li><strong>Collaborative Filtering</strong></li></ul><p>These are methods that reflect the common opinions of the community on a user or product basis. These methods focus on behaviors. For example, as a result of an analysis of shared movies that two people like, another movie that has not been watched yet can be suggested, assuming that both of them will like it. It is divided into <strong>User-Based</strong>, <strong>Item-Based</strong> or <strong>Matrix Factorization</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*2HTw_Bdj7mTib3dec6nUfQ.jpeg" /></figure><ul><li><strong>Content Based Filtering</strong></li></ul><p>It is a recommendation system based on the products’ features, information, descriptions, in short, their similarities. For example, recommending another product with similar features of a purchased product.</p><h4>ASSOCIATION RULES</h4><p>Association rules are the name given to data mining methods used to determine the relationships between events and their occurrence. These methods enable future studies to be carried out by obtaining the association behaviors that emerge from the analysis of the data to be used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*uuQfQqz_BWwGvdZTqH2C_A.jpeg" /></figure><p><strong>For example;</strong> If the market basket is examined, the relationship between the best-selling products or product groups can be determined from the sales transactions. Thus, the shelf arrangements of the products in the market can be made according to this relationship.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uvU4TfAqjBQaK851OK59FA.jpeg" /></figure><p><strong>To give a concrete example</strong>, a previous study observed that people who buy diapers also buy beer or ice cream sales in summer and the increase in shark attacks is parallel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*pNaVyaJWDhOSqcU_74gihw.jpeg" /></figure><p><strong>To give a specific example</strong>: 75% of the revenue of Netflix, the famous company that sells movies over the internet, is based on the recommendation algorithm. Of course, although it is not that simple, its logic consists of more complex algorithms based on this rule.</p><p>One of the most popular algorithms used in the analysis of these rules is <strong>Apriori.</strong></p><h4>APRIORI ALGORITHM</h4><p><strong>Apriori</strong> is a basket analysis method used to reveal the frequency of occurrence of products together. There are metrics calculated through this algorithm, which can also be used in large data sets. These metrics allow statistical evaluation of the patterns within the data set. These <strong>metrics</strong>;</p><p>Let X and Y be the products in the market, and N be the total number of products.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*h1Ps9qAATLjXzzSwMAwjGg.png" /></figure><blockquote><strong>support:</strong> Indicates how often X and Y are seen together.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/436/1*s-j2vFa-gQOVy1r003yUTw.png" /></figure><blockquote><strong>confidence: </strong>The probability of X and Y appearing together.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*wsTMy0YqC67Uaf-kBML0Og.png" /></figure><blockquote><strong>lift:</strong> The coefficient of increase in the probability of purchasing product Y when product</blockquote><blockquote><strong>antecedents: </strong>Previous product.(X)</blockquote><blockquote><strong>consequences: </strong>Second product.(Y)</blockquote><blockquote><strong>antecedent support:</strong> Probability of observing X alone</blockquote><blockquote><strong>consequent support:</strong> The probability of Y being observed alone.</blockquote><blockquote><strong>Leverage:</strong> It can be called leverage effect. It is similar to Lift. There is a bias towards those with high support.</blockquote><blockquote><strong>conviction:</strong> It is the expected frequency of product X without product Y.</blockquote><blockquote><strong>zhangs_metric:</strong> It measures association and dissociation of X and Y.</blockquote><p>Before moving on to implementation, I would like to explain the working principle of the algorithm with an <strong>example</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d-VAQ3e_yZSzJlB6M12qIA.png" /></figure><p>We have a sample sales table where products <strong>A</strong>, <strong>B</strong>, <strong>C</strong>, <strong>D</strong> and <strong>E</strong> are sold together.</p><p><strong>Table1-(C1):</strong> The frequency of occurrence of each product in the sales table.</p><p><strong>L1:</strong> Filtering the total occurrence frequencies of the products according to the given <strong>SUPmin=2</strong> threshold value and creating them in a separate table.</p><p><strong>Table2-(C2):</strong> Matching the products with each other again and creating the resulting matches in a separate table according to their frequency of occurrence in the main table.</p><p><strong>L2:</strong> Creating the total co-occurrence frequencies of the matched products in a separate table by filtering them according to the given <strong>SUPmin = 2</strong> threshold value.</p><p><strong>Table3-(C3): </strong>Values ​​of the first row in the main table: <strong>{A,C,D}</strong>. The matching required to reach these values ​​is: <strong>{A,C}</strong>,<strong>{A,D}</strong>,<strong>{D,C}</strong>. But only <strong>{A,C}</strong> is included in our L2 table. That’s why we can’t include the first row from the main table into our new C3 table. Looking at row 2, the matching of <strong>{B,C,E}</strong> values ​​are <strong>{B,C}</strong>,<strong>{C,E}</strong> and <strong>{B,E}</strong>. These are all available in the L2 table. The frequency of these <strong>{B,C,E}</strong> values ​​appearing in the main table is 2. When we look at the other rows in the main table, they do not meet these criteria with the same logic.</p><p><strong>L3:</strong> <strong>{B,C,E}</strong> is filtered according to the total co-occurrence frequency of these 3 products and the <strong>SUPmin=2</strong> threshold value and created in a separate table.</p><h4>APRIORI ALGORITHM APPLICATING</h4><ul><li>For the Association Rules and Apriori application, we need to install the library named <strong><em>mlxtend</em></strong> first.</li></ul><pre>pip install mlxtend</pre><ul><li>Then we call our apriori functions named <strong><em>association_rules</em></strong> from <strong><em>frequent_patterns</em></strong> in the mlxtend library.</li></ul><pre>from mlxtend.frequent_patterns import apriori, association_rules</pre><ul><li>For the Apriori applicating, we will use the <strong><em>online_retail_II</em></strong> data set in Kaggle.</li></ul><pre>retail = pd.read_csv(&quot;/kaggle/input/online-retail-ii-uci/online_retail_II.csv&quot;)</pre><ul><li>After the Exploratory Data Analysis (EDA) process of our data set, we extract the 3 variables we need from our data set.</li></ul><pre># Reaching the product quantities in each invoice.<br>df.groupby([&quot;INVOICE&quot;,&quot;DESCRIPTION&quot;])[&quot;QUANTITY&quot;].sum()</pre><ul><li>Then we create a table with product names in the columns, invoices in the rows, and product quantities in each invoice as values.</li></ul><pre># Sorting descriptions by columns<br>df.groupby([&quot;INVOICE&quot;, &quot;DESCRIPTION&quot;]).\<br>                  agg({&quot;QUANTITY&quot;: &quot;sum&quot;}).unstack().iloc[0:5, 0:5]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*Z2M66SDd8ctWkztNkOUa8w.png" /></figure><ul><li>We fill our NaN values ​​with zero.</li></ul><pre># Filling nan values ​​with zero<br>df.groupby([&#39;INVOICE&#39;, &#39;DESCRIPTION&#39;]).\<br>                agg({&quot;QUANTITY&quot;: &quot;sum&quot;}).unstack().fillna(0).iloc[0:5, 0:5]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*IYn5QfculxAPNvof_KKWwQ.png" /></figure><ul><li>We assign a value to the transactions as 1 if the number of products in the invoice is greater than 0, and 0 if the number of products in the invoice is less than 0, according to the values ​​in our table.</li></ul><pre># 0,0 is converted to 0, if there is a value then it is 1<br>df.groupby([&#39;INVOICE&#39;, &#39;DESCRIPTION&#39;]).\<br>                agg({&quot;QUANTITY&quot;: &quot;sum&quot;}).unstack().fillna(0).\<br>                applymap(lambda x: 1 if x &gt; 0 else 0) .iloc[0:5, 0:5]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/838/1*6jWT9eLDwtcuk_ajcvY65w.png" /></figure><ul><li>We replace the product names in the columns with the stock code.</li></ul><pre># Changing product names with stock code<br>df.groupby([&#39;INVOICE&#39;, &#39;STOCKCODE&#39;]).\<br>                agg({&quot;QUANTITY&quot;: &quot;sum&quot;}).unstack().fillna(0).\<br>                applymap(lambda x: 1 if x &gt; 0 else 0) .iloc[0:5, 0:5]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*d5dqElEHvu8WEcvyFfKszQ.png" /></figure><ul><li>Our table has become as desired.</li></ul><pre>#It&#39;s ready!!<br>df_arl = df.groupby([&#39;INVOICE&#39;, &#39;STOCKCODE&#39;]).\<br>                        agg({&quot;QUANTITY&quot;: &quot;sum&quot;}).unstack().fillna(0).\<br>                        applymap(lambda x: 1 if x &gt; 0 else 0)</pre><ul><li>We prepare the frequency of occurrence of products according to a certain threshold value.</li></ul><pre>frequent_itemsets = apriori(df_arl,min_support=0.01,use_colnames=True)</pre><blockquote><strong>df_arl:</strong> Our edited dataset.<br><strong>min_support:</strong> Our minimum threshold value for support.<br><strong>use_colnames:</strong> We use these to convert integer values ​​to their corresponding element names.</blockquote><ul><li>Now we are creating the <strong>Association Rules.</strong></li></ul><pre>rules = association_rules(frequent_itemsets,metric=&quot;support&quot;,min_threshold=0.01)</pre><blockquote><strong>frequent_itemsets:</strong> Our itemset prepared with Apriori.<br><strong>metric:</strong> The metric to be used.<br><strong>min_threshold:</strong> Our minimum threshold value.</blockquote><ul><li>We can filter or sort this ready table according to the metric we want.</li></ul><pre># Filtering<br>rules[(rules[&quot;support&quot;]&gt;0.05) &amp; (rules[&quot;confidence&quot;]&gt;0.1) &amp; (rules[&quot;lift&quot;]&gt;5)]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CBTRPxZacVThOkvTX1bR8A.png" /></figure><pre># Filtering by confidence<br>rules[(rules[&quot;support&quot;]&gt;0.05) &amp; (rules[&quot;confidence&quot;]&gt;0.1) &amp; (rules[&quot;lift&quot;]&gt;5)].sort_values(&quot;confidence&quot;, ascending=False)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5BsAyPb0uRmVsoylh4EWoQ.png" /></figure><ul><li>Now, for any product purchased, we will ask it to recommend other products.</li><li>Let’s have a product whose stock code is <strong>85123A</strong> and its name is <strong>White Hanging Heart T-Light Holder</strong>. We run our code for this product with Stockcode.</li></ul><pre>def arl_recommender(rules_df, product_id, rec_count):<br> <br>sorted_rules = rules_df.sort_values(&quot;lift&quot;, ascending=False)<br>recommendation_list = []<br>recommendation_list_name = []<br> <br>for i, product in enumerate(sorted_rules[&quot;antecedents&quot;]):<br>for j in list(product):<br>if j[1] == product_id:<br>for k in list(sorted_rules.iloc[i][&quot;consequents&quot;]):<br>if k[1] not in recommendation_list:<br>recommendation_list.append(k[1])<br>added_product = prdct_name_finder(df,product_id)<br>print(f&quot;Added to Cart: {added_product[0]}\n\n&quot;)<br>print(f&quot;Members Who Bought This Also Bought:\n\n&quot;)<br>for i in range(0,rec_count):<br>recommendation_list_name.append(prdct_name_finder(df,recommendation_list[i]))<br>print(f&quot; {recommendation_list_name[i][0]}\n&quot;)<br><br><br>arl_recommender(rules, &quot;85123A&quot;, 3)</pre><p><strong>Results:</strong></p><p><strong>Added to Cart:<em> </em></strong>White Hanging Heart T-Light Holder</p><p><strong>Members Who Bought This Also Bought:</strong></p><p>Red Hanging Heart T-Light Holder</p><p>Zinc Metal Heart Decoration</p><p>Folkart Zinc Heart Christmas Dec</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xsPnEb4G9q21pIv1k0rPpg.png" /></figure><p>Our code worked very well and gave us 3 product recommendations.</p><p>For more and details about the code: <a href="https://www.kaggle.com/code/umutkocata/recommender-system-with-apriori"><strong>https://www.kaggle.com/code/umutkocata/recommender-system-with-apriori</strong></a></p><p><strong><em>Thank you for reading.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da15fc0f28ab" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ML-Model Selection/Validation/Optimization]]></title>
            <link>https://medium.com/@umut.kocatas41/ml-model-selection-validation-optimization-fa19a488f57a?source=rss-44d278718ad0------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa19a488f57a</guid>
            <category><![CDATA[model-optimization]]></category>
            <category><![CDATA[model-validation]]></category>
            <category><![CDATA[gridsearchcv]]></category>
            <category><![CDATA[model-selection]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Umut Kocatas]]></dc:creator>
            <pubDate>Fri, 23 Feb 2024 21:38:27 GMT</pubDate>
            <atom:updated>2024-02-23T21:38:27.280Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*--apP3hqT0J1zoAmpm6Uyw.jpeg" /></figure><p><strong><em>Hello everyone! In this article, I`m going to talk about model selection, validation and finally optimization.</em></strong></p><h4>MODEL SELECTION</h4><p>Machine learning is simply the process of a computer learning data using statistical and mathematical methods to extract patterns. After evaluating the candidate models used in this process, the actual model is determined. This is called the model selection process. So how do we choose the model in the first place?</p><h4>How should we choose the model?</h4><p>First, it is necessary to choose a model according to the type of problem. So first of all, is there a dependent variable or not? You can start with the question:</p><p><strong>Classification-Regression</strong></p><p>If there is a dependent variable, it is either a classification or regression problem, that is, supervised learning.</p><p>When we look at the <strong>classification</strong> algorithms, the target variable, that is, the dependent variable, consists of 2 states. This situation mathematically means that the values ​​between 0 and 1 take the value 0 or 1 according to a certain threshold value. For example, is a person sick or not?, will the customer churn or not? etc..</p><p>In <strong>regression</strong> algorithms, our target variable consists of a continuous variable. Continuous variables, by definition, are variables that can take values ​​within a certain range or infinitely. For example, predicting the price of a house, predicting the dollar exchange rate…</p><p><strong>Clustering</strong></p><p>If there is no dependent variable then the problem automatically enters into unsupervised learning. For example, if the problem is segmentation or clustering, then clustering algorithms can be used. For example; such as hierarchical clustering, k-means…</p><p><strong>Linear/Non-linear</strong></p><p>Model selection is made depending on whether the problem is linear or not. For example, Support Vector Machine (SVM) can be used for linear problems. If it is a prediction problem, Linear Regression is used. If the problem is non-linear then Decision Tree, Random Forest, Neural Network etc.. models can be used.</p><h4>MODEL VALIDATION</h4><p>Model validation is building the training and testing processes on more solid foundations in order to improve the accuracy and performance of the model.</p><h4>How do we evaluate the model?</h4><p>The K-Fold Cross Validation method, one of the most popular methods, is used to evaluate the model. This method is the most effective use of the data set to measure the performance and generalization ability of the model. This method is one of the methods that makes the solution more useful, especially when our training set is small.</p><p><strong>K-Fold Cross Validation</strong></p><p>The training set of a data set divided into training and test data is divided into K pieces. One of the divided parts is used to validate the training set each time. This situation is repeated K times. As a result, the performance of the model is evaluated by averaging the accuracy values ​​K times. The K value may vary depending on the size of the data set. General usage is 5 or 10.</p><h4>Using K-Fold Cross Validation !</h4><p>K-Fold Cross Validation method was used with the RandomForestClassifier model for the Diabetes data set, which is a classification problem.</p><pre>import pandas as pd<br>import numpy as np<br><br>df = pd.read_csv(&quot;diabetes.csv&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/749/1*aM7w5dC0OJ7zmIerixxTUQ.png" /></figure><pre>from sklearn.model_selection import train_test_split, cross_val_score<br>from sklearn.ensemble import RandomForestClassifier<br><br>rf = RandomForestClassifier()<br><br>X = df.drop(&quot;Outcome&quot;,axis=1)<br>y = df[[&quot;Outcome&quot;]]<br><br>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state=0)</pre><pre>cvs = cross_val_score(estimator = rf, X = X_train, y = y_train, cv = 5)<br><br>print(f&quot;RFC Accuracy: {round(cvs.mean(),5)}\nRFC Std:      \<br>{round(cvs.std(),5)}&quot;)</pre><blockquote><strong>estimator</strong>: Model<br><strong>X</strong>: Independent variable<br><strong>y</strong>: Dependent variable<br><strong>cv</strong>: How many iterations to perform</blockquote><pre>RFC Accuracy: 0.74136<br>RFC Std     : 0.04278</pre><p>Evaluation of the model can be measured with <strong>mean()</strong> and <strong>std()</strong> functions.</p><h4>MODEL OPTIMIZATION</h4><p>Model optimization is to ensure that the model works optimally with the data and gets the highest performance. For this, model hyperparameters are used and these vary for each model. So it takes a lot of time to set it manually every time. We will examine Grid Search, a popular optimization method that automatically optimizes this adjustment.</p><p><strong>GridSearch</strong></p><p>This method involves trying all possible combinations of hyperparameter values ​​within a predefined range. It is a simple and easy to understand method.</p><h4>Using GridSearch !</h4><p>For this method, we will optimize our previous model.</p><pre>from sklearn.model_selection import GridSearchCV</pre><p>We define the parameters we want to use in the dictionary.</p><p>The hyperparameters I chose for RandomForestClassifier:</p><blockquote><strong>n_estimators</strong>: <em>Number of trees in the random forest</em><br><strong>max_features</strong>: <em>Number of features in consideration at every split</em><br><strong>max_depth</strong>: <em>Maximum number of levels allowed in each decision tree</em><br><strong>min_samples_split</strong>: <em>Minimum sample number to split a node</em></blockquote><pre>rf_params = {&quot;n_estimators&quot;:[100,200,500],<br>             &quot;max_features&quot;:[4,8,&quot;auto&quot;],<br>             &quot;max_depth&quot;:[5,8],<br>             &quot;min_samples_split&quot;:[2,5,8]<br>             }</pre><p>We define our grid object, enter the necessary parameters and fit our model.</p><pre>gs = GridSearchCV(estimator = rf, <br>                  param_grid = rf_params, <br>                  scoring = &quot;accuracy&quot;, <br>                  cv = 5).fit(X_train, y_train)</pre><blockquote><strong>estimators</strong>: Model<br><strong>param_grid</strong>: Parameters<br><strong>scoring</strong>: Success metric</blockquote><pre>rf_best_result = gs.best_score_<br>rf_best_params = gs.best_params_<br><br>print(f&quot;RFC Before Accuracy: {round(cvs.mean(),5)}\nRFC After Accuracy:  \<br>{round(rf_best_result,5)}\nRFC Best Parameters: {rf_best_params}&quot;)</pre><blockquote><strong>best_score_</strong> : Accuracy value for the most optimally selected hyperparameter value.<br><strong>best_params_</strong>: Best parameters</blockquote><p><strong>Results.</strong></p><pre>RFC Before Accuracy: 0.74136<br>RFC After Accuracy : 0.75874<br><br>RFC Best Parameters: {&#39;max_depth&#39;: 5,<br>                      &#39;max_features&#39;: 4, <br>                      &#39;min_samples_split&#39;: 2, <br>                      &#39;n_estimators&#39;: 200}</pre><p>As a result, we found the optimal hyperparameters for our model and achieved some performance increase. It is also possible to adjust these parameters more precisely with RandomizedSearchCV, but it should not be forgotten that optimization is not always in the direction of increase.</p><p><strong><em>Thank you for reading.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa19a488f57a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>