<?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 NigarAli on Medium]]></title>
        <description><![CDATA[Stories by NigarAli on Medium]]></description>
        <link>https://medium.com/@nigarali?source=rss-b35a71d07196------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7rUzKujMXGRf5rbvZGrifg.jpeg</url>
            <title>Stories by NigarAli on Medium</title>
            <link>https://medium.com/@nigarali?source=rss-b35a71d07196------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 20 May 2026 13:47:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nigarali/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[Encoding Features]]></title>
            <link>https://medium.com/@nigarali/encoding-features-b4c28978c5b7?source=rss-b35a71d07196------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4c28978c5b7</guid>
            <category><![CDATA[encoding]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[NigarAli]]></dc:creator>
            <pubDate>Mon, 08 Apr 2024 17:17:32 GMT</pubDate>
            <atom:updated>2024-04-08T17:17:32.418Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*RVL2No0SCKgmbMLJa3iBvQ.jpeg" /></figure><p>Encoding categorical variables is a vital step in preparing data for machine learning tasks. When dealing with categorical data, characterized by non-numeric values such as text or categories, it becomes necessary to transform them into a numerical format for compatibility with machine learning algorithms. Various widely-used categorical encoding techniques are available, each presenting its unique set of advantages and drawbacks</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*03NEVMHcq_Px51h30bxaWA.jpeg" /><figcaption>Danny Butvinik</figcaption></figure><h3>Feature Encoding Tips</h3><p>Tip 1: Prevent Data Leakage When converting categorical variables like “cat”, “dog”, and “horse” into numerical ones such as 0, 1, 2, etc., ensure that the encoder is fitted solely on the training data. This prevents data leakage, where information from the test data inadvertently influences the training process. After fitting on the training data, use the encoder to transform the validation/test data. If there are missing or new categories in the validation/test data, handle them by either removing unseen categories or encoding them as -1 or other arbitrary values.</p><p>Tip 2: Save Your Encoders After fitting on the training data, save encoders for later use in transforming validation/test data. This allows for the retrieval of categories or transforming encoded values back to their original categories if necessary, using methods like .inverse_transform.</p><h4><strong>1.Label Encoding</strong></h4><p>You can use LabelEncoder when you have categorical variables that are ordinal, meaning they have an inherent order or ranking. LabelEncoder assigns a unique numerical value to each category, effectively encoding the categories into integers.</p><p>For example, if you have a categorical variable “education_level” with categories like “High School”, “Bachelor’s Degree”, “Master’s Degree”, and “PhD”, where there is a clear order from lowest to highest level of education, you can use LabelEncoder to encode these categories as 0, 1, 2, and 3 respectively.</p><h4>2.Ordinal encoding</h4><p>Ordinal encoding is similar to label encoding but allows you to explicitly define the mapping between categories and integer labels. This is especially useful when there is a clear and predefined ordinal relationship. You manually specify the order of categories and map them to integers accordingly.</p><h4>3.One hot/Dummy encoder</h4><p>One-hot encoding can be done with OneHotEncoder from the sklearn package or using the pandas get_dummies method.For a categorical feature having many categories or levels, one-hot encoding is not a great choice from a machine learning perspective, most apparently due to a large number of dimensionality it adds up to.An increase in the dimensionality of the dataset causes curses of dimensionality, which leads to the problem of parallelism and multicollinearity.</p><p>Limit to x-most frequent features:</p><p>One-hot encoding the entire nominal categorical variable with many levels causes to increase the dimensionality. A better choice would be to take top x most frequent categories and create a dummy encoding or one-hot encoding.The less frequent categories are considered less influential and are thus left out or grouped into a single category, depending on the specific encoding strategy used. This helps simplify the dataset while retaining the most relevant information.</p><h4>3.Frequency/Count Encoding</h4><p>Frequency encoding is an encoding technique to transform an original categorical variable to a numerical variable by considering the frequency distribution of the data. It can be useful for nominal features.Frequency encoding is a technique or hacks used heavily in Kaggle competitions.Count encoding or frequency encoding, replaces each category with the count of how many times it appears in the dataset. This encoding technique can be useful when there’s a correlation between the frequency of a category and the target variable.</p><p>Instead of assigning arbitrary numerical values or creating binary columns like in one-hot encoding, frequency encoding utilizes the actual frequency or count of each category.</p><p>Here’s how frequency encoding works:</p><ol><li>Calculate Frequencies: For each category in the categorical variable, count how many times it appears in the dataset.</li><li>Replace Categories: Replace each category with its frequency count. So, instead of having the original category names, you have numerical values representing how often each category appears.</li></ol><p>Frequency encoding is particularly useful in scenarios where the frequency of categories holds valuable information for the prediction task. It can be beneficial when:</p><p>Handling High Cardinality: When dealing with categorical variables that have a large number of unique categories (high cardinality), one-hot encoding may result in too many columns, leading to the curse of dimensionality. Frequency encoding can help reduce dimensionality by replacing categories with their frequencies.</p><p>Preserving Information: Frequency encoding preserves the information about the frequency distribution of categories, which can be relevant for certain machine learning algorithms. For example, if the frequency of occurrence of a category correlates with the target variable, frequency encoding can capture this relationship more directly compared to other encoding methods.</p><p>It reduces dimensionality compared to one-hot encoding. Count encoding retains the original information about the frequency of each category in the dataset.</p><p>Drawbacks:</p><p>Loss of Category Label Information: Frequency encoding replaces category labels with numerical counts, which means the original category labels are lost. This may not be suitable if the category labels themselves hold important semantic meaning.</p><p>Sensitive to Outliers: Frequency encoding can be sensitive to outliers or rare categories with extremely high frequencies. These outliers may disproportionately influence the encoded numerical values, potentially affecting model performance.</p><p>While count encoding preserves frequency information, it discards any other meaningful information or relationships that may exist between categories. Count encoding can be sensitive to data imbalances.</p><p>When to use<strong>: </strong>This encoding technique can be useful when there’s a correlation between the frequency of a category and the target variable. Also applicable for categorical features with a lot of categories. Also, the count_encoder should be fit only on the train dataset. The fitted object should be used to transform test and out of time (OOT) datasets.</p><p><strong>4.Target Encoding</strong></p><p>Target encoding, also known as mean encoding, involves replacing each category with the mean (or some other statistic) of the target variable for that category. Here’s how target encoding works:</p><ul><li>Calculate the mean of the target variable for each category.</li><li>Replace the category with its corresponding mean value.</li></ul><p>There are two ways to implement target encoding</p><p>Mean Encoding: The encoded values are the mean of the target values with smoothing applied</p><p>Leave-One-Out Encoding: The encoded values are the mean of the target values except for the data point that we want to predict</p><p>Pros:</p><p><strong>Supports High Cardinality</strong>: Target Encoder can be used in cases where there are many different categories, and it is better if there are multiple data samples for each category</p><p>Cons:</p><p><strong>Target Leakage</strong>: Even with smoothing, this may result in target leakage and overfitting. Leave-One-Out Encoding and introducing Gaussian noise in the target variable can be used to address the overfitting problem</p><h4>5.Rare Encoding</h4><p>When applying the Rare Encoding technique, a threshold value is determined (which can be in the form of a ratio or frequency), and if values with frequencies lower than this threshold value are not desired, instead of individually representing these values, it may be preferred to group them together. This results in a decrease in the number of classes, combining previously numerous different classes that are likely not strong in terms of representation and have a low probability of being observed in the data. These variables, which are less likely to be observed, are all grouped together, and all of these classes are referred to as “Rare”. This process is called Rare Encoding.</p><p>There are various encoding techniques used for nominal and ordinal variables.</p><p>Reference:</p><p><a href="https://towardsdatascience.com/feature-encoding-techniques-in-machine-learning-with-python-implementation-dbf933e64aa">Feature Encoding Techniques in Machine Learning with Python Implementation</a></p><p><a href="https://machinelearningmastery.com/one-hot-encoding-for-categorical-data/?source=post_page-----616e87bf8c74--------------------------------">https://machinelearningmastery.com/one-hot-encoding-for-categorical-data/?source=post_page-----616e87bf8c74--------------------------------</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4c28978c5b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to handle outliers?]]></title>
            <link>https://medium.com/@nigarali/how-to-handle-outliers-307f4348e414?source=rss-b35a71d07196------2</link>
            <guid isPermaLink="false">https://medium.com/p/307f4348e414</guid>
            <category><![CDATA[outliers]]></category>
            <dc:creator><![CDATA[NigarAli]]></dc:creator>
            <pubDate>Mon, 08 Apr 2024 13:23:40 GMT</pubDate>
            <atom:updated>2024-04-08T13:23:40.928Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*klOA36VsT1eKLpW2G4a17Q.png" /></figure><p>Outliers are data points that deviate significantly from the rest of data.They can affect the accuracy of predictions and should be treated appropriately.This involves either removing them or transforming them using a suitable technique.</p><h4>There are several reasons to handle outliers in data analysis:</h4><ol><li>For linear models: Outliers can significantly affect the parameter estimation process in linear regression models. Therefore, it’s often advisable to handle outliers before fitting linear models.</li><li>When outliers are due to errors: If outliers are likely to be due to data entry errors or measurement errors, it’s generally a good idea to handle them to prevent these errors from unduly influencing the analysis.</li><li>When outliers affect distribution assumptions: If the underlying assumptions of your statistical test or model are violated due to the presence of outliers, it’s important to address them. For example, if you’re using parametric statistical tests that assume normality, outliers can affect the validity of your results.</li></ol><h4>In certain situations, there are cases that we shouldn’t remove outliers:</h4><ol><li>When they represent genuine data: Outliers may represent genuine variation in the data. In such cases, removing them could lead to biased results. It’s essential to understand the domain and context of the data to determine whether outliers are valid or not.</li><li>For robust models: Some models are inherently robust to outliers. For instance, decision trees, random forests, and gradient boosting models are less sensitive to outliers because they partition the feature space and make decisions based on regions rather than the entire dataset.</li><li>When data is scarce: In situations where data is limited, removing outliers may lead to loss of valuable information. In such cases, it might be better to use robust statistical techniques that are less influenced by outliers.</li><li>When outliers are of interest: Sometimes, outliers themselves are the subject of the analysis. In these cases, removing them would defeat the purpose of the analysis.For example,In fraud detection and anomaly detection tasks, outliers play a critical role in identifying unusual or suspicious behavior. Therefore, removing outliers in fraud detection scenarios is generally not advisable.</li><li>In cases of exploratory data analysis (EDA): During EDA, it’s often useful to identify outliers and understand their impact on the data distribution and relationships. Removing outliers prematurely can obscure important insights.</li></ol><h4>Techniques to detect outliers</h4><p>There are several techniques that can be used to detect and treat outliers.Single feature methods focus on analyzing one feature at a time, while multivariate methods consider interactions between multiple features simultaneously.</p><h4><strong>Univariate</strong> outliers: outliers of objects that contains only one dimension</h4><ol><li><strong>Visual Inspection</strong>:Visual inspection involves plotting the data and identifying any data points that are far away from the rest of the data.Box plots,scatter plots and histograms can be useful for identifying outliers visually</li><li><strong>Z-score Method:</strong> The score method involves calculating the standard score for each data point.Data points with a standard score greater than a threshold value are considered outliers.The threshold value is typically set to 3,meaning that any data point with a Z-score than 3 is considered an outlier.</li></ol><pre>from scipy import stats<br>z_scores=stats.zscore(df)<br>abs_z_scores=np.abs(z_scores)<br>outliers=df[abs_z_scores&gt;threshold]</pre><p>3.<strong>Modified Z-Score Method:</strong>Similar to the z-score method, but uses the median and median absolute deviation (MAD) instead of the mean and standard deviation.Robust to outliers and suitable for datasets with non-normal distributions or containing extreme values.</p><p>4.<strong>Interquartile Range(IQR) method:</strong></p><pre>def outlier_thresholds(dataframe,col_name,q1=0.25,q3=0.75)<br>    quartile1=dataframe[col_name].quantile(q1)<br>    quartile3=dataframe[col_name].quantile(q3)<br>    interquartile_range=quartile3-quartile1<br>    up_limit=quartile3+1.5*interquartile_range<br>    low_limit=quartile1-1.5*interquartile_range<br>    return low_limit,up_limit</pre><p>5.<strong>Grubbs’ Test:</strong></p><p>Grubbs’ Test is a statistical test to detect outliers in univariate datasets based on the maximum deviation from the mean.It provides a formal hypothesis test for identifying outliers, suitable for scenarios where statistical significance is important.</p><p>6.<strong>Tukey’s Fences:</strong>Similar to the IQR method but uses a different multiplier to determine the fence for identifying outliers.Suitable for datasets with skewed distributions or those containing extreme values.</p><h4>Multivariate Methods:</h4><ol><li><strong>Density-Based Methods (e.g., DBSCAN)</strong></li></ol><p>DBSCAN is a clustering algorithm that can be used for outlier detection.Data points that do not belong to any cluster are considered outliers.</p><pre>from sklearn.cluster import DBSCAN<br>dbscan=DBSCAN(eps=eps,min_samples=min_samples<br>dbscan.fit(data)<br>outliers=data[dbscan.labels_ == -1)</pre><p>2.<strong>Local Outlier Factor (LOF)</strong></p><p>LOF measures the local density deviation of a data point with respect to its neighbors. Outliers have significantly lower densities compared to their neighbors.</p><pre>clf=LocalFactorialOutlier(neighbors=20)<br>clf.predict(df)<br>df.scores=clf.negative_outlier_factor_<br>np.sort(df_scores)[0:5]</pre><p>3.<strong>Isolation Forest:</strong>This method isolates outliers by randomly partitioning the dataset into subsets and identifying anomalies as instances that require fewer partitions to isolate.</p><pre>def random_cut_forest(data, n_estimators=100, contamination=0.1):<br>    <br>    model = IsolationForest(n_estimators=n_estimators, contamination=contamination, behaviour=&quot;new&quot;)<br>    model.fit(data)<br>    # The anomaly score of each sample is calculated as the mean anomaly score of the trees in the forest<br>    anomaly_scores = model.decision_function(data)<br>    return anomaly_scores</pre><p>4.<strong>Mahalanobis Distance</strong></p><p>Applies to multivariate datasets, measuring the distance of each data point from the centroid of the data in multiple dimensions, taking into account the covariance structure.</p><p>5.<strong>Robust Random Cut Forest</strong></p><p>Suitable for datasets with high dimensionality and containing outliers.It’s robust to outliers and efficient for processing large-scale datasets with high-dimensional features.</p><p>6.<strong>Cluster-Based Methods:</strong> Utilize multivariate data to partition the dataset into clusters, identifying outliers as data points that do not belong to any cluster or form small clusters.</p><h4>Techniques to handle outliers</h4><ol><li><strong>Removing outliers:</strong> Removing outliers involves deleting the data points that are identified as outliers.However,this can result in a loss of data and can also affect the accuracy of the analysis</li><li><strong>Winsorization: </strong>Winsorization is a technique for handling outliers by replacing extreme values with the nearest values that are not considered outliers.It is a form of trimming that can be useful for handling outliers</li></ol><pre>from scipy.stats.mstats import winsorize<br>import numpy as np<br><br># Example dataset<br>data = np.array([10, 20, 30, 40, 500])<br><br># Winsorization: replace outliers with values from the nearest non-outlying values<br>winsorized_data = winsorize(data, limits=[0.05, 0.05])  # Two-sided winsorization, trimming 5% from each tail<br><br>print(&quot;Original data:&quot;, data)<br>print(&quot;Winsorized data:&quot;, winsorized_data)</pre><p>3.<strong>Data transformation</strong>:Transformation involves transforming the data,such as using a logarithmic or square root transformation,to reduce the impact of outliers on the analysis.Log transformation is particularly useful for highly skewed data.</p><p>4.<strong>Robust statistical methods</strong>:Robust statistical methods are particularly useful when dealing with datasets containing outliers, heavy-tailed distributions, or other forms of non-normality. They provide more reliable estimates and inferences in such situations and help mitigate the influence of extreme values on statistical analyses and modeling. However, it’s important to note that robust methods may have lower efficiency (greater variance) when the data conform well to the assumptions of traditional statistical methods.</p><blockquote>Robust Measures of Central Tendency</blockquote><blockquote>Robust Measures of Dispersion</blockquote><blockquote>Percentile Bootsrap</blockquote><blockquote>Trimmed Mean</blockquote><blockquote>M-estimators</blockquote><blockquote>RANSAC(Random Sample Consensus)</blockquote><blockquote>Huber Loss</blockquote><blockquote>Winsorizing</blockquote><blockquote>MAD(Median Absolute Deviation)</blockquote><blockquote>Tukey’s Fences</blockquote><blockquote>Biweight Midvariance</blockquote><p>Thanks for reading.You can follow me on <a href="https://www.linkedin.com/in/nigaralizada/">linkedin</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=307f4348e414" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My Experience with PwC Switzerland’s Job Simulation Program on Forage Platform]]></title>
            <link>https://medium.com/@nigarali/my-experience-with-pwc-switzerlands-job-simulation-program-on-forage-platform-bb55d15259a5?source=rss-b35a71d07196------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb55d15259a5</guid>
            <dc:creator><![CDATA[NigarAli]]></dc:creator>
            <pubDate>Sun, 07 Apr 2024 18:06:45 GMT</pubDate>
            <atom:updated>2024-04-07T18:06:45.245Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*ZBki3gau19h4QBqBzhcnrA.jpeg" /></figure><p>I recently completed the PwC Switzerland job simulation program on Forage, an exceptional platform for honing skills and gaining practical experience. Forage offers Virtual Work Experience Programs endorsed by top companies, providing opportunities for everyone, including international students, regardless of visa or work status. These programs comprise tasks and resources crafted to replicate real-world career scenarios.</p><p>In essence, Forage’s Virtual Work Experience Programs are primarily aimed at assisting students. By participating in a Forage Virtual Internship, you can gain insights into diverse career paths and develop the skills and confidence necessary for success in transitioning from academia to the professional world.</p><p>My journey involved immersing myself in hands-on experience in data analytics and data visualizations using Power BI. Now, I’m excited to share the steps of the program and my insights gained along the way.</p><h4>First Task: Call Center</h4><p>The initial task involved developing dashboards tailored for the call center manager, who lacked visibility into current trends. The objective was to create a dashboard enabling the manager to comprehend today’s trends effectively. Several key performance indicators (KPIs) and metrics needed analysis:</p><p>Overall customer satisfaction</p><p>Overall calls answered/abandoned</p><p>Calls by time</p><p>Average speed of answer</p><p>Agent’s performance quadrant -&gt; average handle time (talk duration) vs calls answered</p><h4>My Dashboard:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*I0znJCY443IgOtHZoVPcvg.png" /></figure><p><strong>Key Insights:</strong></p><p>In the analyzed dataset of call center activities, a total of 5000 calls were recorded, out of which 4054 were answered, leaving 900 unanswered calls. This resulted in 3640 resolved issues and 1354 unresolved issues. It was observed that the distribution of topics among resolved and unresolved calls remained roughly consistent.</p><ul><li>Topic Distribution: The topic distribution across resolved and unresolved calls appeared to be similar. However, it was noted that the least unresolved calls were related to admin support, followed by streaming.</li><li>Agent Performance:</li><li>Highest Rated Agent: Dan received the highest rating among agents, while Diana received the lowest rating.</li><li>Most Answered Calls: Jim handled the highest number of answered calls, while Joe and Stewart handled the lowest.</li><li>Highest Resolved Calls: Jim and Dan achieved the highest number of resolved calls, whereas Stewart and Joe had the lowest resolved calls.</li><li>Active Days: Monday through Saturday emerged as the highest active days of the week, indicating increased call volume and engagement during these days.</li></ul><p><strong>Recommendations:</strong></p><ol><li>Agent Training and Support: Provide additional training and support to agents with lower performance ratings to improve their effectiveness in handling calls.</li><li>Topic-specific Analysis: Conduct a deeper analysis of calls related to admin support and streaming to identify potential areas for improvement or optimization.</li><li>Workforce Management: Allocate resources efficiently based on the observed trends in call volume on different days of the week to ensure adequate coverage during peak periods.</li><li>Performance Recognition: Implement a recognition program to acknowledge and incentivize agents who consistently achieve high ratings and resolve a significant number of calls.</li></ol><h4>The Second Task: Customer Retention</h4><p>The subsequent task entailed designing a dashboard specifically focused on customer retention for the call center manager. This dashboard aimed to provide insights and recommendations concerning customer retention strategies.<br>The next step involved the following:</p><ol><li>Identifying appropriate Key Performance Indicators (KPIs) relevant to customer retention.</li><li>Developing a comprehensive dashboard tailored for the retention manager, highlighting the selected KPIs.</li><li>Composing a concise email to the engagement partner, outlining the findings from the dashboard analysis and offering recommendations for necessary changes.</li></ol><h4>My Dashboard:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/795/1*GoBin5Syy8Fe2nm7fHPsow.png" /></figure><blockquote>Key Insights</blockquote><p>The influence of streaming services, such as Streamin TV and streaming movies, on customer churn rates appears to be minimal. However, there is a significant proportion of customers subscribing to our phone services who are churning. Therefore, it is imperative to investigate our phone service offerings to enhance customer retention. A considerable number of customers have opted for month-to-month contracts with us. Analysis suggests that factors such as marital status and customer dependency have limited impact on customer retention.</p><h4>Third Task:Diversity and Inclusion</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*yYFlkYYjzXvJAqkAjAlYDw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb55d15259a5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handling Missing Data]]></title>
            <link>https://medium.com/@nigarali/handling-missing-data-a7b637aa200d?source=rss-b35a71d07196------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7b637aa200d</guid>
            <category><![CDATA[handling-missing-values]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[eda]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[NigarAli]]></dc:creator>
            <pubDate>Sun, 07 Apr 2024 15:15:45 GMT</pubDate>
            <atom:updated>2024-04-07T15:15:45.124Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*82aVEKva0CWIGhE-Ads25A.png" /></figure><p>Handling missing data is a crucial step in preparing data for machine learning models.</p><h4>Types of Missing Data</h4><p>When evaluating how missing data might affect registry findings, it’s crucial to understand why the data is missing. Missing data can generally be categorized into three groups:</p><blockquote><strong>Missing completely at random(MCAR)-</strong></blockquote><blockquote>Randomly scattered in the dataset. Has much fewer null values than other types of missingness. Has no correlation with other variables. Can be due to technical or human error during data entry.</blockquote><blockquote><strong>Missing at random(MAR)</strong></blockquote><blockquote>Broader than MCAR. Randomness occurs only to specific groups of the data. For example, certain students missing their classes more during winter, the elderly leaving the Mobile OS field blank because they don’t know how to use a phone, etc. A clear distinction with MCAR is that MAR will always have some relationship with observed values.</blockquote><blockquote><strong>Missing Not At Random (MNAR)</strong></blockquote><blockquote>Final and most difficult case of missingness. It is randomly scattered in the dataset but the huge amount of missing values suggest some unobserved factor affecting the missingness. However deep you search, you will not find a relationship with existing features. This type will always have some systematic relationship with unobserved factors like leaving the IQ field blank because of embarrassment, leaving satisfaction score blank because customers could not fit their satisfaction into the given scores, etc.</blockquote><p>One possible way to handle this problem is to get rid of the observations that have missing data.However,you will risk losing valuable information.A better strategy would be to impute the missing values.In other words,we need to infer those missing values fromthe exiting part of the data.</p><p>Assumptions:</p><ol><li>Data is Missing at Random(MAR)</li><li>Easy to implement</li><li>No data manipulation required</li></ol><p>Limitations:</p><ol><li>Deleted data can be informative</li><li>Can lead to the deletion of large part of the data</li><li>Can create a bias in the dataset,if a large amount</li></ol><p>When to use:</p><ol><li>Data is MAR(Missing at Random)</li><li>Good for Mixed,Numerical and Categorical data</li><li>Missing datais not more than 5%-6% of the dataset</li><li>Data does not contain much information and will not bias in the dataset</li></ol><h4>Imputation Methods</h4><p>It may not be always be logical to apply the same operations to columns with a small number of missing data and columns with a larger number of missing values.For example, a more accurate data set can be obtained by presenting different solutions to detect the data proportionally</p><pre>missing_columns_info=missing_percent(df)<br>missing_columns_info</pre><p>Columns with a missing values of more than 70% are dropped.</p><p><strong>Imputing missing values:</strong></p><p>There are many imputation methods for replacing the missing values. You can use different python libraries such as Pandas, and Sci-kit Learn to do this. Let’s go through some of the ways of replacing the missing values.</p><p><strong>Replacing with an arbitrary value:</strong></p><p>If you can make an educated guess about the missing value, then you can replace it with some arbitrary value.</p><p>Assumptions:</p><ol><li>Data is not Missing At Random</li><li>The missing data is imputed with an arbitrary value that is not part of the dataset or Mean/Median/Mode of data</li></ol><p>Advantages:</p><ol><li>Easy to implement</li></ol><p>Disadvantages:</p><ol><li>Can distort original variable distribution</li><li>Arbitrary values can create outliers</li><li>Extra caution required in selecting the Arbitrary value</li></ol><p>When to use:</p><p>1.When data is not MAR(Missing at Random)</p><p>2.Suitable for All</p><p><strong>Mean,Median Imputation:</strong></p><p>Numeric features like ‘Age’ can be imputed with their mean,median using fillna method.Therefore, the choice between mean and median for imputation depends on the distributional characteristics of the data in the column.If the distribution of a column follows a normal pattern, we can use the mean to fill in missing values. However, if the distribution is not normal, we should opt for the median instead.</p><p><strong>Replacing with the mode:</strong></p><p>Mode is the most frequently occurring value. It is used in the case of categorical features. You can use the ‘fillna’ method for imputing the categorical columns ‘Gender,’ ‘Married,’ and ‘Self_Employed.’</p><p><strong>Backward Fill(or Backfill) and Forward Fill (or Ffill) Imputation:</strong></p><p>These methods are often used in time-series data or datasets where observations are ordered based on some sequence.Therefore, careful consideration should be given before applying backward or forward fill imputation, especially in cases where the sequential relationship between observations is not clear or when missing values occur in clusters.</p><p><strong>Interpolation:</strong></p><p>Estimate missing values based on the values of other data points using interpolation techniques like linear or polynomial interpolation.andas’ interpolate method can be used to replace the missing values with different interpolation methods like ‘polynomial,’ ‘linear,’ and ‘quadratic.’ More sophisticated than simple imputation but may be sensitive to outliers.</p><p>Most of the imputation technique can cause bias.Simple imputation can result in an understimation of standard errors.As the number of missing data increases,simple imputation methods should be avoided.</p><p><strong>Univariate approach vs Multivariate approach</strong></p><p>We can impute missing values using the sci-kit library by creating a model to predict the observed value of a variable based on another variable which is known as regression imputation.</p><p>In a Univariate approach, only a single feature is taken into consideration. You can use the class SimpleImputer and replace the missing values with mean, mode, median, or some constant value.</p><p>In a multivariate approach, more than one feature is taken into consideration. There are two ways to impute missing values considering the multivariate approach. Using KNNImputer or IterativeImputer classes.</p><p><strong>K-Nearest Neighbour(KNN) Imputation</strong></p><p>One commonly adopted strategy for addressing missing data is to employ a predictive model to estimate the absent values.This technique entails developing a separate model for each input variable containing missing entries.</p><p>The default value of K is set to 5.Although there is no definitive method for determining the ideal value of K,a commonly used heuristic suggests that the optimal K is often the square root of the total number of samples.To identify the most suitable ,an error plot or accuracy plot is commonly used.</p><p>With this imputer, the problem is choosing the correct value for <em>k</em>. As you cannot use GridSearch to tune it, we can take a visual approach for comparison:</p><pre>n_neighbors = [2, 3, 5, 7]<br><br>fig, ax = plt.subplots(figsize=(16, 8))<br># Plot the original distribution<br>sns.kdeplot(diabetes.SkinThickness, label=&quot;Original Distribution&quot;)<br>for k in n_neighbors:<br>    knn_imp = KNNImputer(n_neighbors=k)<br>    diabetes_knn_imputed.loc[:, :] = knn_imp.fit_transform(diabetes)<br>    sns.kdeplot(diabetes_knn_imputed.SkinThickness, label=f&quot;Imputed Dist with k={k}&quot;)<br><br>plt.legend();<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*VV8SaWx8OshMoiv3IUkNWg.png" /></figure><p>Knn uses a variety of distance metric for the algorithm to function effectively.For example,Chebychev,Cosine Similarity,Euclidean,Minkowski,Manhattan,Hamming etc</p><p><strong>MICE Imputation-’Multiple Imputation by Chained Equation’</strong></p><p>MICE is the advanced missing data imputation technique that uses multiple iterations of Machine Learning model training to predict the missing values using known values from other features in the data as predictors</p><p>How does MICE algorithm work?</p><ol><li>You basically take the variable that contains missing values as a response ‘Y’ and other variables as predictors ‘X’</li><li>Build a model with rows where Y is not missing</li><li>Then predict the missing observations</li><li>Do this multiple times by doing random draws of the data and taking the mean of the predictions</li></ol><p>Thanks for reading: You can follow me on <a href="https://www.linkedin.com/feed/">Linkedin</a></p><p><strong>Referance:</strong></p><ul><li><a href="https://medium.com/towards-datascience">towardsdatascience</a></li><li><a href="https://www.analyticsvidhya.com/blog/2021/10/handling-missing-value/">Effective Strategies for Handling Missing Values in Data Analysis (Updated 2023)</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7b637aa200d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to merge dataframes in Pandas]]></title>
            <link>https://medium.com/@nigarali/how-to-merge-dataframes-in-pandas-a8689c77de8f?source=rss-b35a71d07196------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8689c77de8f</guid>
            <category><![CDATA[data-analytics]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[power-bi]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[data-visualization]]></category>
            <dc:creator><![CDATA[NigarAli]]></dc:creator>
            <pubDate>Tue, 27 Feb 2024 22:38:02 GMT</pubDate>
            <atom:updated>2024-02-27T22:38:02.135Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CZn98J8EvOFhdUl0TMEaGA.png" /></figure><p>As a data enthusiast and avid Pandas user, I often find myself grappling with the challenge of merging datasets effectively. In this article, I’ll share insights into various techniques, such as merge, join, and concat, which have become integral parts of my data manipulation toolkit.</p><h3>Merge</h3><p>Consider a scenario where you have two tables with common columns that you want to combine. The merge function in Pandas is a powerful tool that implements common SQL-style joining operations. There are different types of merges based on the dataset structure:</p><blockquote>One-to-one: Joining two DataFrame objects on their indexes, which must contain unique values.</blockquote><blockquote>Many-to-one: Joining a unique index to one or more columns in a different DataFrame.</blockquote><blockquote>Many-to-many: Joining columns on columns.</blockquote><p>The how argument in the merge() function specifies which keys are included in the resulting table. The options for how and their SQL equivalents are as follows:</p><blockquote>left: LEFT OUTER JOIN (Use keys from the left frame only)</blockquote><blockquote>right: RIGHT OUTER JOIN (Use keys from the right frame only)</blockquote><blockquote>outer: FULL OUTER JOIN (Use the union of keys from both frames)</blockquote><blockquote>inner: INNER JOIN (Use the intersection of keys from both frames)</blockquote><blockquote>cross: CROSS JOIN (Create the Cartesian product of rows of both frames)</blockquote><p>Consider two tables, each sharing a common identifier, employeeid</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/1*xAxAc_Pee9I9FAHBymWt6w.png" /></figure><pre>result=pd.merge(df1,df2, on=[&#39;EmployeeID&#39;])<br>result</pre><figure><img alt="Output of the code" src="https://cdn-images-1.medium.com/max/681/1*qvt2gmGiSY-D3mWK2nVNtQ.png" /><figcaption>Output of the code</figcaption></figure><p>As we observe, the default option corresponds to an inner join.</p><p>Now,I’m about to combine data using the left join method.</p><pre>result2=pd.merge(df1,df2,how=&#39;right&#39; ,on=[&#39;EmployeeID&#39;])<br>result2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*Cnsf5jyhLXQcUxgs7xit1Q.png" /><figcaption>Output of the code</figcaption></figure><p>When utilizing a left join, the resulting dataset provides a comprehensive view of the values from the left table. In cases where there is no corresponding match in the right table, these unmatched values will be denoted as ‘NaN’ in the merged dataset. This feature ensures that even non-matching records from the left table are retained in the final output, offering valuable insights into the data.</p><pre>result2=pd.merge(df1,df2,how=&#39;right&#39; ,on=[&#39;EmployeeID&#39;])<br>result2<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*hd6Z5D2mZzPmz8yx7mJ9Gg.png" /><figcaption>output of the code</figcaption></figure><p>When employing a right join, the merged dataset showcases the values from the right table. Any values without a corresponding match in the left table will be indicated as ‘NaN’ in the final output, allowing for a clear representation of the right table’s information in the merged result</p><pre>result3=pd.merge(df1,df2,how=&#39;outer&#39;, on=[&#39;EmployeeID&#39;])<br>result3</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/783/1*8ekVMU2S0TdWEQ1jAMkCSw.png" /><figcaption>output of the code</figcaption></figure><p>When opting for an outer join, the merged dataset incorporates values from both the left and right tables. In instances where there is no match in either table, the missing values are filled with ‘NaN,’ offering a comprehensive view that includes information from both datasets.</p><pre>result4=pd.merge(df1,df2, how=&#39;cross&#39;)<br>result4</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*3ZGNvXOmF2-wkglSsavzXQ.png" /><figcaption>output of the code</figcaption></figure><p>In the case of a cross join, the resulting dataset represents the Cartesian product of both the left and right tables. This means every row from the left table is paired with every row from the right table, creating an exhaustive combination of records. Unlike other join types, there is no consideration of matching criteria, and the merged dataset encompasses all possible combinations.</p><h4>JOINS</h4><p>In the realm of Pandas, join operations typically rely on indexes to align and merge datasets. However, in situations where the dataset lacks a predefined index structure, a practical approach is to employ the set_index method. This method allows for the establishment of a suitable index, paving the way for a smooth integration of datasets through the versatile join functionality. By explicitly defining the join based on the set index, this process ensures a coherent and accurate combination of data from multiple sources.</p><pre>df3 = df1.set_index(&#39;EmployeeID&#39;).join(df2.set_index(&#39;EmployeeID&#39;), lsuffix=&#39;_Left&#39;, rsuffix=&#39;_Right&#39;)<br><br># Reset index to bring &#39;EmployeeID&#39; back as a regular column<br>df3 = df3.reset_index()<br>df3</pre><p>The aim of this code snippet is to merge two DataFrames, df1 and df2, using the &#39;EmployeeID&#39; column. The set_index method is utilized to establish the &#39;EmployeeID&#39; as the index for both DataFrames, facilitating the subsequent join operation. The lsuffix=&#39;_Left&#39; and rsuffix=&#39;_Right&#39; parameters distinguish columns with identical names. Finally, the reset_index function is applied to bring &#39;EmployeeID&#39; back as a regular column in the resulting DataFrame df3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*p5icCb518Mz8N7xlrQlOyg.png" /><figcaption>Output of the code</figcaption></figure><p>Similar to the merge method, the join method in Pandas allows for the use of different join types. This feature provides users with the freedom to choose the type of join that best fits their needs, be it a left join, right join, inner join, or outer join. This flexibility ensures a natural and intuitive approach to merging datasets in Pandas, accommodating various scenarios and enhancing the overall adaptability of the data manipulation process.</p><pre>df3 = df1.set_index(&#39;EmployeeID&#39;).join(df2.set_index(&#39;EmployeeID&#39;), how=&#39;inner&#39;, lsuffix=&#39;_Left&#39;, rsuffix=&#39;_Right&#39;)<br>df3</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*1J3WiiYtewm2K_rmxY1-fw.png" /><figcaption>Output of the code</figcaption></figure><h4>Concat method</h4><p>Combine Pandas objects along a specified axis with the concatenate method. This function also provides the option for set logic along other axes, offering flexibility in handling overlapping labels on the concatenation axis. Additionally, the method allows the addition of a layer of hierarchical indexing, enhancing its utility for managing data with similar or overlapping labels.</p><pre>result5=pd.concat([df1,df2])<br>result5</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/773/1*TFjmw6AR1DA4qvjxq4UNrg.png" /><figcaption>Output of the code</figcaption></figure><p><em>In this article, we explored the powerful world of data manipulation using Pandas, uncovering techniques such as merging, joining, and concatenating DataFrames.Happy Data Analyzing!</em></p><p><em>Hi, I’m Nigar, a data enthusiast passionate about simplifying complex data processes. Connect with me on </em><a href="https://www.linkedin.com/in/nigaralizada/"><em>Linkedin </em></a><em>for more data insights and updates.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8689c77de8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>