<?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 Vishwas Kshirsagar on Medium]]></title>
        <description><![CDATA[Stories by Vishwas Kshirsagar on Medium]]></description>
        <link>https://medium.com/@k98vishwas?source=rss-2ee4f5bd7fcc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9wjp7p3G48QodrmlO3sgBQ.jpeg</url>
            <title>Stories by Vishwas Kshirsagar on Medium</title>
            <link>https://medium.com/@k98vishwas?source=rss-2ee4f5bd7fcc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 00:29:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@k98vishwas/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[Utilizing Machine Learning for the Detection of Fraud in Online Payments]]></title>
            <link>https://medium.com/@k98vishwas/utilizing-machine-learning-for-the-detection-of-fraud-in-online-payments-9285e800bd88?source=rss-2ee4f5bd7fcc------2</link>
            <guid isPermaLink="false">https://medium.com/p/9285e800bd88</guid>
            <category><![CDATA[fraud-detection]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[fraud]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[decision-tree]]></category>
            <dc:creator><![CDATA[Vishwas Kshirsagar]]></dc:creator>
            <pubDate>Mon, 24 Apr 2023 06:21:28 GMT</pubDate>
            <atom:updated>2023-04-24T08:05:39.813Z</atom:updated>
            <content:encoded><![CDATA[<p>Using Decision Tree Classifier Algorithm</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CrB8FHk0z_XMQrPO_s_iXA.jpeg" /></figure><p>The rise of online payment systems has certainly made payments easier, but it has also led to an increase in payment fraud. Anyone using any payment system, particularly when making payments with a credit card, can fall victim to online payment fraud. As a result, it is crucial for credit card companies to detect such fraud in order to ensure that their customers are not charged for products or services they did not purchase. If you are interested in learning about detecting online payment fraud using machine learning with Python, this article will guide you through the process.</p><p><strong>Detecting Online Payment Fraud Using Machine Learning: A Comprehensive Guide</strong></p><p>In order to employ machine learning for the purpose of detecting online payment fraud, it is imperative to develop a model capable of distinguishing between fraudulent and non-fraudulent payments. This requires a comprehensive dataset that encompasses information regarding online payment fraud. Through such a dataset, we can gain an understanding of the transactional patterns that commonly lead to fraud. To this end, I have procured a <a href="https://www.kaggle.com/datasets/ealaxi/paysim1?resource=download"><strong>dataset</strong></a> from Kaggle, which features historical data regarding fraudulent transactions.</p><p>By utilizing this dataset, we can effectively identify and prevent fraudulent online payments. The columns included in the dataset are as follows:</p><p>🔹<strong>Step</strong> refers to a time unit, where each step is equivalent to one hour.</p><p>🔹<strong>Type</strong> denotes the nature of an online transaction.</p><p>🔹<strong>Amount</strong> signifies the value of the transaction.</p><p>🔹<strong>NameOrig</strong> represents the name of the customer who initiated the transaction.</p><p>🔹<strong>OldbalanceOrg</strong> refers to the balance of the customer’s account before the transaction.</p><p>🔹<strong>NewbalanceOrig</strong> signifies the balance of the customer’s account after the transaction.</p><p>🔹<strong>NameDest</strong> denotes the name of the recipient of the transaction.</p><p>🔹<strong>OldbalanceDest</strong> signifies the initial balance of the recipient’s account before the transaction.</p><p>🔹<strong>NewbalanceDest</strong> signifies the balance of the recipient’s account after the transaction.</p><p>🔹<strong>IsFraud</strong> is a flag that indicates whether the transaction is fraudulent or not.</p><p>Having provided you with the information regarding the data utilized for online payment fraud detection task, I would now like to elaborate on the implementation of machine learning techniques in detecting such fraud using the programming language Python, which will be discussed in the following section.</p><p><strong>Detecting payment fraud in online transactions with Python</strong></p><p>To commence this task, I will import the required Python libraries along with the dataset essential for this purpose.</p><pre>import pandas as pd<br>import numpy as np<br><br>data = pd.read_csv(&quot;credit card.csv&quot;)</pre><pre>print(data.head())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/981/1*5rxVJ2p5XpOfhGhMoKmLtQ.png" /></figure><p>Let us examine whether the dataset contains any instances of missing or empty values.</p><pre>print(data.isnull().sum())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VlPADOPDkvn_wgtoj5Lzjw.png" /></figure><p>The dataset in question is devoid of any missing or incomplete information.</p><p>Before proceeding further, it is prudent to review the nature of the transactions outlined within the dataset.</p><pre>#Checking the transaction type<br>print(data.type.value_counts())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*imC51eLhTmjR-exGNbwvmw.png" /></figure><pre>type = data[&quot;type&quot;].value_counts()<br>transactions = type.index<br>quantity = type.values<br><br>import plotly.express as px<br>figure = px.pie(data, values=quantity,<br>               names=transactions, hole = 0.5,<br>               title=&quot;Distribution of Transaction Type&quot;)<br>figure.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NS_PbZwrTGfoJMRCVjz45A.png" /></figure><p>Let us examine the relationship between the characteristics or attributes of the dataset and the isFraud column.</p><pre>#checking correlation<br>correlation = data.corr()<br>print(correlation[&quot;isFraud&quot;].sort_values(ascending=False))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/1*qdlWAs66A0zo2bWjdOS-SA.png" /></figure><p>We will proceed with converting the categorical variables into numerical format.<br>Additionally, we will re-label the values of the “isFraud” column as “No Fraud” and “Fraud” to enhance the interpretability of the results.</p><pre>data[&quot;type&quot;] = data[&quot;type&quot;].map({&quot;CASH_OUT&quot;:1, &quot;PAYMENT&quot;: 2,<br>                                &quot;CASH_IN&quot;:3, &quot;TRANSFER&quot;: 4,<br>                                &quot;DEBIT&quot;:5})<br><br>data[&quot;isFraud&quot;] = data[&quot;isFraud&quot;].map({0: &quot;No Fraud&quot;, 1: &quot;Fraud&quot;})<br>print(data.head())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u1iLAFDxEuTXhRl0DTSnRw.png" /></figure><p><strong>Online Payments Fraud Detection Model</strong><br>We will proceed with training a classification model that can distinguish between fraudulent and legitimate transactions.</p><p>However, prior to training the model, it is necessary to partition the available data into two separate sets: one for training the model and the other for testing its effectiveness.</p><pre>#splitting the data<br>from sklearn.model_selection import train_test_split<br>x= np.array(data[[&quot;type&quot;, &quot;amount&quot;, &quot;oldbalanceOrg&quot;, &quot;newbalanceOrig&quot;]])<br>y= np.array(data[[&quot;isFraud&quot;]])</pre><p>Let us proceed with the training of the model for detecting fraud in online payments.</p><pre># training a machine learning model<br>from sklearn.tree import DecisionTreeClassifier<br>xtrain, xtest, ytrain, ytest = train_test_split(x,y, test_size = 0.10, random_state=512)<br>model = DecisionTreeClassifier()<br>model.fit(xtrain,ytrain)<br>print(model.score(xtest, ytest))</pre><pre>0.9997265277511466</pre><pre>#Let&#39;s predict<br><br># feautes = [type, amount, oldbalanceOrg, newbalanceOrig]<br><br>features = np.array([[4, 9560.60, 8563.56, 0.0]])<br>print(model.predict(features))</pre><pre>[&#39;No Fraud&#39;]</pre><p><strong>Summary</strong><br>Machine learning algorithms implemented in Python can be utilized to detect online payment fraud, which is an important application of data science in the finance industry. By analyzing and processing large amounts of data, machine learning models can identify patterns and anomalies that may indicate fraudulent behavior. The integration of machine learning techniques with online payment systems can enhance fraud detection and prevention measures, thereby improving the security and trustworthiness of financial transactions.</p><p><strong>GitHub Repository:</strong> <a href="https://github.com/k98vishwas/Online-Payments-Fraud-Detection-using-Decision-Tree">https://github.com/k98vishwas/Online-Payments-Fraud-Detection-using-Decision-Tree</a></p><h3>Conclusion</h3><p>If you find any difficulty in following the code part, mention them in the comment section.</p><p>Thank you for reading! Let me know in a comment or on <a href="https://www.linkedin.com/in/kvishwas98/">LinkedIn</a> if you felt like this did or didn’t help. I’ve got a few more articles that I’m writing and will be posting them every couple of weeks. They are mostly accounts from my project experience. If there are any other questions or anything else you’d like to hear about, please don’t hesitate to put in a request.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9285e800bd88" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maximizing Your Instagram Reach]]></title>
            <link>https://medium.com/@k98vishwas/maximizing-your-instagram-reach-aa1bd3e19bac?source=rss-2ee4f5bd7fcc------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa1bd3e19bac</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[regression]]></category>
            <category><![CDATA[instagram]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Vishwas Kshirsagar]]></dc:creator>
            <pubDate>Mon, 10 Apr 2023 11:50:13 GMT</pubDate>
            <atom:updated>2023-04-10T11:52:30.896Z</atom:updated>
            <content:encoded><![CDATA[<p>How Passive Aggressive Regressor Can Help You Grow!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tMyRDM0nWQRT99DLmsidNw.jpeg" /></figure><p>Instagram is currently among the most widely used social media applications, particularly by professionals for various purposes such as business promotion, content creation, portfolio building, and blogging. Due to its massive user base with diverse niches, Instagram continually adapts to improve its features for users and content creators. However, these changes can impact the visibility of content and influence its long-term success.</p><p>Therefore, content creators need to analyze their Instagram data to ensure sustainable growth. This is where the application of Data Science in social media becomes relevant. To that end, this article provides a comprehensive guide on Instagram Reach Analysis using Python, which can assist content creators in understanding and adapting to the evolving nature of Instagram.</p><h3><strong>Instagram A/C Analysis</strong></h3><p>As a researcher, I have spent a considerable amount of time studying Instagram reach and analyzing how the platform’s algorithm works. To do so, I collect data on the reach of my Instagram posts each week to gain insight into the platform’s algorithm. While there are some APIs available for analyzing Instagram reach, they are not always reliable, making it more effective to manually collect data.</p><p>For students interested in learning about Instagram reach analysis using Python, I have made the dataset I used in my research available for download. With this dataset, you can explore the task of Instagram reach analysis and prediction using machine learning in Python.</p><p>By analyzing the data collected, we can uncover trends and patterns that help us predict how our Instagram posts will perform. With machine learning techniques, we can build models that can accurately predict the reach of our posts.</p><p>In the following section, I will guide you through the process of performing Instagram reach analysis and prediction using machine learning techniques in Python, so you can gain a better understanding of how to analyze and optimize your Instagram account’s reach.</p><h3><strong>Instagram Analysis Using Python</strong></h3><p>To commence the analysis of the extent of my Instagram account’s reach, we will begin by importing the requisite Python libraries and datasets.</p><pre>!pip install wordcloud #The code installs the Python package &quot;wordcloud&quot; using pip, which allows for the creation of word clouds in Python.</pre><pre>import pandas as pd  #data cleaning and analysis<br>import numpy as np  #perform a wide variety of mathematical operations on arrays<br>import matplotlib.pyplot as plt  #creating static, animated, and interactive visualizations<br>import seaborn as sns # making statistical graphics <br>import plotly.express as px  #  to create beautiful interactive web-based visualizations<br>from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator # to show which words are the most frequent in the given text<br>from sklearn.model_selection import train_test_split<br>from sklearn.linear_model import PassiveAggressiveRegressor # Used to shuffle the training data, when shuffle is set to True</pre><pre>#The code reads the data from a CSV file named &quot;Instagram data.csv&quot; using pandas library<br>data = pd.read_csv(&quot;Instagram data.csv&quot;, encoding = &#39;latin1&#39;)<br><br><br>#prints the first 5 rows of the dataset.<br>print(data.head()) </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*caBtRJeg0F89ruGj4k1gvQ.png" /><figcaption>first 5 rows of the dataset</figcaption></figure><p><strong>Prior to beginning any task, it would be beneficial to check if there are any missing values in this dataset.</strong></p><pre>#The code calculates the sum of null values in each column of the &#39;data&#39; dataset.<br>data.isnull().sum() </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*wrSnZaqlhlh1BXLdjMBXFg.png" /><figcaption>NULL Values</figcaption></figure><p>So it has no null value so no need to perform ‘dropna’ here.</p><h4>To comprehend the data type of all the columns, we can examine the insights provided by each column.</h4><pre>#prints information about number of entries, columns, data types, and memory usage.<br>data.info()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/937/1*_rZ9nE456rvHNC_nenMx5g.png" /><figcaption>information about the dataset</figcaption></figure><h4>Examining the extent of Instagram’s impact</h4><p>Let’s begin by examining how far my Instagram posts have spread. Initially, I will assess the extent to which my posts have been viewed from my own location by analyzing the distribution of impressions.</p><pre>#creates a plot showing the distribution of impressionhome using the fivethirtyeight style.<br>plt.figure(figsize=(10,8))<br>plt.style.use(&#39;fivethirtyeight&#39;)<br>plt.title(&quot;Distribution of Impression from Home&quot;)<br>sns.distplot(data[&quot;From Home&quot;])<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AttHMTSmGUxUwKrNsvgtMg.png" /></figure><p>The data obtained from the home section on Instagram provides insights into the extent to which my posts are reaching my followers. Upon analyzing the impressions data from the home section, it appears challenging to reach all of my followers on a daily basis.</p><p><strong>Now, let’s examine the distribution of impressions that I have received from the utilization of hashtags.</strong></p><pre>#creates a histogram using Seaborn to visualize the distribution of the &quot;From Hashtags&quot; data column in a pandas dataframe called &quot;data&quot;.<br><br>plt.figure(figsize=(10,8))<br>plt.title(&quot;Distributio of Imprressions From Hashtags&quot;)<br>sns.distplot(data[&quot;From Hashtags&quot;])<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K2mGnbrLqIPSPt-WydHZ2Q.png" /></figure><p>Hashtags serve as a means to classify our Instagram posts, enabling us to expand our reach to a broader audience based on the type of content we produce. Analyzing the impact of hashtags on our posts indicates that not all posts can be discovered through hashtags alone, but they can potentially connect us with new users.</p><h4>To further illustrate this point, let us examine the distribution of impressions that I have garnered from the explore section of Instagram.</h4><pre>#generates a histogram of the distribution of impressions from Explore and displays it using the seaborn library.<br><br>plt.figure(figsize=(10,8))<br>plt.title(&quot;Distribution of Impressions From Explore&quot;)<br>sns.distplot(data[&quot;From Explore&quot;])<br>plt.show()</pre><p>The “explore” section of Instagram serves as the platform’s recommendation system, offering personalized post suggestions to users based on their interests and preferences. Analysis of the impressions obtained from this section indicates that our posts are not frequently recommended to users by Instagram’s algorithm. While some posts have gained traction through the “explore” section, the overall reach is notably lower compared to that achieved through the use of hashtags.</p><p><strong>Let us now examine the distribution of impression percentages across different sources on Instagram.</strong></p><pre><br>#sums up the values in different columns (&quot;From Home&quot;, &quot;From Hashtags&quot;, &quot;From Explore&quot;, and &quot;From Other&quot;) in a pandas DataFrame called &quot;data&quot; and assigns the results to respective variables named &quot;home&quot;, &quot;hashtags&quot;, &quot;explore&quot;, and &quot;other&quot;.<br>home = data[&quot;From Home&quot;].sum()<br>hashtags = data[&quot;From Hashtags&quot;].sum()<br>explore = data[&quot;From Explore&quot;].sum()<br>other = data[&quot;From Other&quot;].sum()<br><br>#creates two lists, &quot;labels&quot; and &quot;values&quot;, with four elements each, where the elements in &quot;labels&quot; are strings and the elements in &quot;values&quot; are variables that hold some numerical data.<br>labels = [&quot;From Home&quot;, &quot;From Hashtags&quot;, &quot;From Eplore&quot;, &quot;Other&quot;]<br>values = [home, hashtags, explore, other]<br><br><br>fig = px.pie(data, values=values, names= labels,<br>            title = &quot;Impressions on Instagram Posts From Various Sources&quot;, hole = 0.5)<br>fig.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nq_ZyPQRH-J5pQjI4ivNng.png" /><figcaption>Impressions on Instagram Posts</figcaption></figure><p>The preceding circular chart indicates that 44.1% of the audience engagement originates from individuals who follow the content creator, while 33.6% results from the usage of hashtags. Moreover, 19.2% of the engagement arises from the content being featured in the “explore” section, with the remaining 3.05% originating from diverse sources.</p><h4><strong>Analyzing Content</strong></h4><p>Let us proceed with examining the content of my Instagram posts by analyzing the dataset containing two distinct columns, namely ‘caption’ and ‘hashtags.’ This analysis will provide us with valuable insights into the type of content I share on Instagram.</p><p>Our next step will involve creating a wordcloud based on the caption column, which will allow us to visualize the most frequently used words in my Instagram post captions.</p><pre><br>#generates a word cloud visualization of the text in the &quot;Caption&quot; column of a dataset, with stopwords removed and a white background color.<br><br>text = &quot; &quot;.join(i for i in data.Caption)<br>stopwords = set(STOPWORDS)<br>wordcloud = WordCloud(stopwords = stopwords, background_color=&quot;white&quot;).generate(text)<br>plt.style.use(&quot;classic&quot;)<br>plt.figure(figsize=(12,10))<br>plt.imshow(wordcloud, interpolation=&quot;bilinear&quot;)<br>plt.axis(&quot;off&quot;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x1ohskcdqyuKNXfBgzVoow.png" /><figcaption>Instagram posts by analyzing</figcaption></figure><p>Let us generate a visual representation of the hashtags column in the form of a wordcloud to analyze the prevalent hashtags used in my Instagram posts.</p><pre>#generates a word cloud visualization from a collection of hashtags in a given dataset.<br><br>text = &quot; &quot;.join(i for i in data.Hashtags)<br>stopwords = set(STOPWORDS)<br>wordcloud = WordCloud(stopwords=stopwords, background_color=&quot;white&quot;).generate(text)<br>plt.figure(figsize=(12,10))<br>plt.imshow(wordcloud, interpolation=&quot;bilinear&quot;)<br>plt.axis(&quot;off&quot;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n3AuQwMCgrmJkE-5yflzvg.png" /><figcaption>prevalent hashtags</figcaption></figure><h4>Analyzing Relationships</h4><p>Let us examine the connections between variables to identify the key determinants of our Instagram reach, which will enhance our comprehension of the underlying mechanisms governing the Instagram algorithm.</p><p>Let us examine the correlation between the quantity of likes and impressions of my Instagram content.</p><pre>figure = px.scatter(data_frame = data, x=&quot;Impressions&quot;, <br>                   y = &quot;Likes&quot;, size = &quot;Likes&quot;, trendline = &quot;ols&quot;,<br>                   title = &quot;Relationship Between Likes and Impressions&quot;)<br>figure.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xcYmCeM2iz4xYQkP54sfkA.png" /><figcaption>Relation B/W Likes &amp; Impressions</figcaption></figure><p>A correlation exists between the quantity of likes received and the extent of the audience reached through Instagram.</p><p>We will now examine the connection between the quantity of comments and the volume of views obtained on my Instagram posts.</p><pre>figure = px.scatter(data_frame = data, x = &quot;Impressions&quot;, y = &quot;Comments&quot;, size = &quot;Comments&quot;,<br>                   trendline = &quot;ols&quot;, title = &quot;Relationship Between Comments and Total Impressions&quot;)<br>figure.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b1hXZvJ2VHHGS1rlRN5Rmg.png" /><figcaption>Relation B/W Comments &amp; Impressions</figcaption></figure><p>Based on our observations, it appears that the quantity of comments a post receives does not have a noticeable impact on the extent of its reach.</p><p>Moving forward, we will analyze the correlation between the quantity of shares and the number of impressions.`</p><pre>figure = px.scatter(data_frame = data, x = &quot;Impressions&quot;, y = &quot;Shares&quot;, size = &quot;Shares&quot;, trendline = &quot;ols&quot;,<br>                   title = &quot;Relationship Between Shares and Total Impressions&quot;)<br>figure.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cqVqGgCjGOYvQ5HlI7Eheg.png" /><figcaption>Relation B/W Shares &amp; Impressions</figcaption></figure><p>An increased quantity of shares may lead to a greater level of exposure, however, it’s important to note that shares have less impact on post reach compared to likes.</p><p>Now, let’s examine how the number of saves relates to the number of impressions</p><pre>figure = px.scatter(data_frame = data, x = &quot;Impressions&quot;,<br>                   y = &quot;Saves&quot;, size = &quot;Saves&quot;, trendline = &quot;ols&quot;,<br>                   title = &quot;Relationship Between Post Saves and Total Impressions&quot;)<br>figure.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LG-TRMwGA6-8zVit-tREQg.png" /><figcaption>Relation B/W Post Saves and Total Impressions</figcaption></figure><p>There exists a correlation between the frequency at which my Instagram post is saved and the extent of its reach, implying a linear relationship.</p><p>Let us now examine the correlation between all the columns and the Impressions column.</p><pre>correlation = data.corr()<br>print(correlation[&quot;Impressions&quot;].sort_values(ascending=False))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HkenTyE-ffkKuDyN-FZdQA.png" /><figcaption>Correlation between Impressions and Other Parameters</figcaption></figure><p>It can be asserted that on Instagram, a greater number of likes and saves on a post can contribute to increased reach. Additionally, an increased number of shares may also have a positive impact on reach. However, a low number of shares is not likely to significantly affect reach.</p><h4>Analyzing Conversion Rate</h4><p>The term “conversion rate” in Instagram refers to the proportion of followers gained in relation to the number of profile visits received from a particular post. To determine the conversion rate, a formula can be employed, which involves dividing the number of follows by the number of profile visits and multiplying the result by 100. To illustrate, let’s examine the conversion rate of my personal Instagram account.</p><pre>conversion_rate = (data[&quot;Follows&quot;].sum() / data[&quot;Profile Visits&quot;].sum()) * 100<br>print(conversion_rate)</pre><pre>41.00265604249668</pre><p>The conversion rate of my Instagram account is 41%, indicating a favorable outcome.</p><p>Now, let’s explore the correlation between the overall profile visits and the number of followers acquired from all profile visits.</p><pre>figure = px.scatter(data_frame = data, x = &quot;Profile Visits&quot;, y = &quot;Follows&quot;,<br>                   size = &quot;Follows&quot;, trendline = &quot;ols&quot;,<br>                   title = &quot;Relationship Between Profile Visits and Followers Gained&quot;)<br>figure.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s229CiPXFBrdo_J3PFFXbg.png" /><figcaption>Relation B/W Profile Visits &amp; Followers</figcaption></figure><p>There is a direct correlation between the number of profile visits and the increase in the number of followers. This correlation can be described as a linear relationship.</p><h4>Instagram Reach Prediction Model</h4><p>In the following section, I will be demonstrating the process of training a machine learning model that will be utilized to forecast the reach of an Instagram post. Prior to training the model, the data will be separated into distinct training and testing sets.</p><pre>x = np.array(data[[&#39;Likes&#39;, &#39;Saves&#39;, &#39;Comments&#39;, &#39;Shares&#39;, <br>                   &#39;Profile Visits&#39;, &#39;Follows&#39;]])<br>y = np.array(data[&quot;Impressions&quot;])<br>xtrain, xtest, ytrain, ytest = train_test_split(x, y, <br>                                                test_size=0.2, <br>                                                random_state=521)</pre><p>Here’s a methodology to train a machine learning model using Python for the purpose of predicting the reach of an Instagram post.</p><pre>model = PassiveAggressiveRegressor()<br>model.fit(xtrain, ytrain)<br>model.score(xtest, ytest)</pre><pre>0.933839144607709</pre><p>Next, we will utilize a machine learning model by providing input parameters to make a forecast on the potential reach of an Instagram post.</p><pre>features = np.array([[282.0, 233.0, 4.0, 9.0, 165.0, 54.0]])<br>model.predict(features)</pre><pre>array([11514.08805704])</pre><h3>Summary</h3><p>The utilization of machine learning techniques in Python can enable an individual to evaluate and forecast the potential reach of Instagram posts. For a content creator to attain consistent success on Instagram, a thorough examination of their reach data is necessary. Therefore, the incorporation of Data Science in social media is essential. We hope this article on Instagram Reach Analysis using Python has been informative. Please feel free to pose any relevant queries in the comments section below.</p><p><strong>GitHub Repository:</strong> <a href="https://github.com/k98vishwas/Instagram-Account-Analysis-">https://github.com/k98vishwas/Instagram-Account-Analysis-</a></p><h3><strong>Conclusion</strong></h3><p>If you find any difficulty in following the code part, mention them in the comment section.</p><p>Thank you for reading! Let me know in a comment or on <a href="https://www.linkedin.com/in/kvishwas98/">LinkedIn</a> if you felt like this did or didn’t help. I’ve got a few more articles that I’m writing and will be posting them every couple of weeks. They are mostly accounts from my project experience. If there are any other questions or anything else you’d like to hear about, please don’t hesitate to put in a request.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa1bd3e19bac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementation of SVM 2/2]]></title>
            <link>https://medium.com/@k98vishwas/implementation-of-svm-2-2-779b8765d824?source=rss-2ee4f5bd7fcc------2</link>
            <guid isPermaLink="false">https://medium.com/p/779b8765d824</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[svm]]></category>
            <dc:creator><![CDATA[Vishwas Kshirsagar]]></dc:creator>
            <pubDate>Mon, 09 Jan 2023 17:48:48 GMT</pubDate>
            <atom:updated>2023-04-10T11:46:18.548Z</atom:updated>
            <content:encoded><![CDATA[<p>This blog focuses on the code part of the SVM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XVkukQ6b72zK_RjxFdtp7A.jpeg" /><figcaption>SVM</figcaption></figure><p>Let’s recap what we have learned in the <a href="http://Intuition of SVM — Support Vector Machines 1/2"><strong>Intuition of SVM — Support Vector Machines 1/2</strong></a></p><p>Imagine that you have a dataset with two classes, and you want to train an SVM to classify the data points into one of the two classes. The SVM will look for the “line of best fit” that maximally separates the two classes. But in higher dimensions, the line of best fit becomes a “hyperplane,” which is a subspace of one dimension less than the original space.</p><p>The SVM algorithm searches for the hyperplane that has the largest distance (called the “margin”) between the hyperplane and any data points from either class. This is because a larger margin leads to a lower generalization error, meaning that the model is less likely to make mistakes on unseen data.</p><p>Once the SVM has found the hyperplane with the maximum margin, it can use this hyperplane to classify new data points by assigning them to the class on the appropriate side of the hyperplane.</p><p>Without further ado, Let’s chop-chop <strong>👇🏻</strong></p><pre># importing all the neccessary libraries<br>import pandas as pd<br>import numpy as np<br>import matplotlib.pyplot as plt</pre><pre># importing the dataset<br>datasets = pd.read_csv(&quot;/content/sample_data/Social_Network_Ads.csv&quot;)<br>print(&quot;Data Successfully Imported&quot;)</pre><p>Data Successfully Imported</p><pre>datasets.head() #print first 5 records</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qa2ItteVASoIusH3qNK2Xw.png" /><figcaption>first 5 Records</figcaption></figure><pre># separate all the independent variables into &#39;X&#39; and dependant into &#39;Y&#39;<br>X = datasets.iloc[:, [2,3]].values<br>Y = datasets.iloc[:, 4].values</pre><pre># Splitting the dataset into the Training set and Test set<br><br>from sklearn.model_selection import train_test_split<br>X_Train, X_Test, Y_Train, Y_Test = train_test_split(X, Y, test_size = 0.25, random_state = 0)</pre><pre># Feature Scaling<br><br>from sklearn.preprocessing import StandardScaler<br>sc_X = StandardScaler()<br>X_Train = sc_X.fit_transform(X_Train)<br>X_Test = sc_X.transform(X_Test)</pre><pre># Fitting the classifier into the Training set<br><br>from sklearn.svm import SVC<br>classifier = SVC(kernel = &#39;linear&#39;, random_state = 0)<br>classifier.fit(X_Train, Y_Train)</pre><p>SVC(kernel=’linear’, random_state=0)</p><pre># Predicting the test set results<br><br>Y_Pred = classifier.predict(X_Test)</pre><pre># Making the Confusion Matrix <br><br>from sklearn.metrics import confusion_matrix<br>cm = confusion_matrix(Y_Test, Y_Pred)<br>print(cm</pre><p>[[66 2] [ 8 24]]</p><pre># Visualising the Training set results<br><br>from matplotlib.colors import ListedColormap<br>X_Set, Y_Set = X_Train, Y_Train<br>X1, X2 = np.meshgrid(np.arange(start = X_Set[:, 0].min() - 1, stop = X_Set[:, 0].max() + 1, step = 0.01),<br>                     np.arange(start = X_Set[:, 1].min() - 1, stop = X_Set[:, 1].max() + 1, step = 0.01))<br>plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),<br>             alpha = 0.75, cmap = ListedColormap((&#39;red&#39;, &#39;green&#39;)))<br>plt.xlim(X1.min(), X1.max())<br>plt.ylim(X2.min(), X2.max())<br>for i, j in enumerate(np.unique(Y_Set)):<br>    plt.scatter(X_Set[Y_Set == j, 0], X_Set[Y_Set == j, 1],<br>                c = ListedColormap((&#39;red&#39;, &#39;green&#39;))(i), label = j)<br>plt.title(&#39;Support Vector Machine (Training set)&#39;)<br>plt.xlabel(&#39;Age&#39;)<br>plt.ylabel(&#39;Estimated Salary&#39;)<br>plt.legend()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*CDrf8eh0ovGFI72iTGld3g.png" /><figcaption>SVM Training Set Graph</figcaption></figure><pre># Visualising the Test set results<br><br>from matplotlib.colors import ListedColormap<br>X_Set, Y_Set = X_Test, Y_Test<br>X1, X2 = np.meshgrid(np.arange(start = X_Set[:, 0].min() - 1, stop = X_Set[:, 0].max() + 1, step = 0.01),<br>                     np.arange(start = X_Set[:, 1].min() - 1, stop = X_Set[:, 1].max() + 1, step = 0.01))<br>plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),<br>             alpha = 0.75, cmap = ListedColormap((&#39;red&#39;, &#39;green&#39;)))<br>plt.xlim(X1.min(), X1.max())<br>plt.ylim(X2.min(), X2.max())<br>for i, j in enumerate(np.unique(Y_Set)):<br>    plt.scatter(X_Set[Y_Set == j, 0], X_Set[Y_Set == j, 1],<br>                c = ListedColormap((&#39;red&#39;, &#39;green&#39;))(i), label = j)<br>plt.title(&#39;Support Vector Machine (Test set)&#39;)<br>plt.xlabel(&#39;Age&#39;)<br>plt.ylabel(&#39;Estimated Salary&#39;)<br>plt.legend()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*RqwJ-1jTnWI8YaiGV_9UaA.png" /><figcaption>SVM Test Set Graph</figcaption></figure><p>A simple implementation of a (linear) Support Vector Machine model in python. A classifier is an object of the SVC class imported from sklearn.svm library.</p><p>the linear kernel type was chosen since this was a linear SVM classifier model.</p><p>If you find any difficulty in following the code part, mention them in the comment section.</p><p><em>Thank you for reading! Let me know in a comment or on </em><a href="https://www.linkedin.com/in/kvishwas98"><em>LinkedIn</em></a><em> if you felt like this did or didn’t help. I’ve got a few more articles that I’m writing and will be posting them every couple of weeks. They are mostly accounts from my project experience. If there are any other questions or anything else you’d like to hear about, please don’t hesitate to put in a request.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=779b8765d824" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Intuition of SVM — Support Vector Machines 1/2]]></title>
            <link>https://medium.com/@k98vishwas/intuition-of-svm-support-vector-machines-1-2-40148bccdb0?source=rss-2ee4f5bd7fcc------2</link>
            <guid isPermaLink="false">https://medium.com/p/40148bccdb0</guid>
            <category><![CDATA[supervised-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Vishwas Kshirsagar]]></dc:creator>
            <pubDate>Thu, 05 Jan 2023 07:40:22 GMT</pubDate>
            <atom:updated>2023-01-05T08:28:24.041Z</atom:updated>
            <content:encoded><![CDATA[<h3>Intuition of SVM — Support Vector Machines 1/2</h3><p>This blog focuses on the theory part of the SVM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*iScKPttzoT6Wv516Jb6sGQ.jpeg" /><figcaption>SVM</figcaption></figure><p>Support Vector Machines (SVMs) are a type of supervised machine learning algorithm that can be used for classification or regression tasks. Given a set of training examples, each labeled as belonging to one of two classes, an SVM training algorithm builds a model that assigns new examples to one class or the other, making it a non-probabilistic binary linear classifier. An SVM model represents the examples as points in space, mapping them into a higher-dimensional feature space via a set of basis functions, and finding a linear decision surface that maximally separates the two classes. The support vectors are the points in the training data that lie closest to the decision surface and influence the position and orientation of the decision surface.</p><p>SVMs are particularly well-suited for problems where the number of dimensions is large, or where the number of training examples is small relative to the number of sizes. They are also effective in situations where the classes could be more balanced, or where there is a lot of noise in the data.</p><p><strong>To give you an idea, Here is the </strong>👇🏻</p><h3><strong>Example</strong></h3><p>Imagine that you are working for a marketing company and you want to classify customers as either “likely to buy” or “unlikely to buy” based on their age, income, and education level. You have a dataset of customer information, including their age, income, education level, and whether or not they made a purchase.</p><p>You can use an SVM classifier to train a model on this data. The classifier will take the age, income, and education level of each customer as input and predict whether they are likely to make a purchase.</p><p>To train the classifier, you provide it with a set of labeled examples of customers who did and did not make a purchase, along with their age, income, and education level. The classifier uses this data to learn the characteristics that are typical of customers who are likely to make a purchase.</p><p>Once the classifier has been trained, you can use it to predict whether new customers are likely to make a purchase. For example, if you give it to a customer with the age of 25, an income of $50,000, and a college degree, it might predict that they are likely to make a purchase. On the other hand, if you give a customer with an age of 65, an income of $30,000, and a high school degree, it might predict that they are unlikely to make a purchase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*_8dno_8LgQXtJJfo7hDvYA.png" /><figcaption>Support Vector Machine</figcaption></figure><h3><strong>Assumptions</strong></h3><p>The SVM algorithm makes the following assumptions:</p><ol><li>The data is linearly separable: This means that there exists a linear decision boundary that can accurately separate the classes.</li><li>The classes are fully determined by a hyperplane: This means that there is a clear margin between the classes and that points on either side of the margin are clearly associated with one class or the other.</li><li>The optimization problem has a unique solution: This means that there is only one solution that maximally separates the classes and that this solution is unique.</li><li>The problem is convex: This means that the optimization problem has a single global minimum, rather than multiple local minima.</li><li>The training data is free of noise: This means that the data is clean and free of errors or inconsistencies, which could lead to incorrect classifications.</li><li>The training data is representative of the overall population: This means that the training data accurately reflects the distribution of the overall population and can be used to make predictions about new examples.</li></ol><p><strong>Let’s jot down the upper hands of SVM </strong>👇🏻</p><h3><strong>Advantages</strong></h3><p>Support Vector Machines (SVMs) have several advantages that make them a popular choice for many machine-learning tasks:</p><ol><li>SVMs are effective in high-dimensional spaces: SVMs are particularly well-suited for problems where the number of dimensions is large, or where the number of training examples is small relative to the number of dimensions. This is because SVMs try to find the hyperplane that maximally separates the classes, which is easier to do when there are fewer dimensions.</li><li>SVMs are memory efficient: Because the SVM model only stores a subset of the training examples (called support vectors), it is more memory efficient than other models that store the entire training set.</li><li>SVMs are versatile: SVMs can be used for classification or regression tasks and can be effective with a variety of different kernel functions. This makes them a good choice for a wide range of problems.</li><li>SVMs are resistant to overfitting: Because the SVM model tries to find the hyperplane that maximally separates the classes, it is less prone to overfitting than other models that try to fit the data as closely as possible.</li><li>SVMs are widely used and well-understood: SVMs have been around for a long time and are well-established machine learning algorithms. As a result, there is a wealth of knowledge and resources available for those who want to learn more about them.</li></ol><p><strong>Let’s pinpoint the downside of SVM </strong>👇🏻</p><h3><strong>Drawbacks</strong></h3><ol><li>SVMs can be sensitive to the choice of kernel and hyperparameters: The performance of an SVM model can depend heavily on the choice of kernel function and the values of the hyperparameters, which can be difficult to determine.</li><li>SVMs can be computationally intensive: Training an SVM model can be computationally intensive, especially for large data sets with many examples.</li><li>SVMs may not perform well on highly imbalanced data sets: If one class is much more prevalent than the other, the classifier may be biased toward the more common class, leading to poor performance in the minority class.</li><li>SVMs are sensitive to the scale of the input features: If the input features are on different scales, this can affect the performance of the SVM model. It is often necessary to scale the features to the same range before training an SVM.</li><li>SVMs may not be well-suited for data sets with many noise points or outliers: Because the SVM model tries to find the maximum margin hyperplane, it may be sensitive to noise or outliers in the data, which could lead to poor performance.</li><li>SVMs are not well-suited for data sets with many missing values: If the data has many missing values, it may be difficult to train an SVM model effectively.</li></ol><h3>Conclusion</h3><p>If you find any difficulty in following the theory part, mention them in the comment section.</p><p><em>Thank you for reading! Let me know in a comment or on </em><a href="https://www.linkedin.com/in/kvishwas98"><em>LinkedIn</em></a><em> if you felt like this did or didn’t help. I’ve got a few more articles that I’m writing and will be posting them every couple of weeks. They are mostly accounts from my project experience. If there are any other questions or anything else you’d like to hear about, please don’t hesitate to put in a request.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40148bccdb0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Decision Trees 2/2]]></title>
            <link>https://medium.com/@k98vishwas/decision-trees-2-2-8b77ecb3ca90?source=rss-2ee4f5bd7fcc------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b77ecb3ca90</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[supervised-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[decision-tree]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Vishwas Kshirsagar]]></dc:creator>
            <pubDate>Tue, 03 Jan 2023 04:24:17 GMT</pubDate>
            <atom:updated>2023-01-03T04:24:17.215Z</atom:updated>
            <content:encoded><![CDATA[<p>2/2 contains the code part of the Decision Trees Classifier Algorithm.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*hNtX5bMtEYo30QMCJOqUKQ.jpeg" /><figcaption>Decision Tree Classifier Algorithm</figcaption></figure><p><strong>A quick recap of what we have learned in the </strong><a href="https://medium.com/@k98vishwas/decision-trees-1-2-583c1df0db09">Decision Trees 1/2</a>.</p><p>A decision tree is a decision support tool that uses a tree-like model of decisions and their possible consequences, along with probability estimates and costs, to predict the outcome of a decision. Decision tree classifiers are widely used in many areas, such as credit risk assessment, medical diagnosis, and image classification.</p><p>The decision tree classifier works by splitting the training data into smaller and smaller subsets based on the values of the features. The tree is built by making decisions based on the features of the data, and each internal node in the tree corresponds to a decision about the value of a particular feature. The leaves of the tree represent the final classification of the data.</p><p>To classify a new sample, the decision tree classifier starts at the root node and follows the path down the tree based on the values of the features in the sample. When it reaches a leaf node, it returns the class label associated with that leaf.</p><p>One advantage of decision tree classifiers is that they are easy to interpret and explain, as the decision-making process is explicitly represented in the tree structure. However, they can also be prone to overfitting, especially if the tree is allowed to grow too deep.</p><h3>About Dataset</h3><p>The Iris dataset was used in R.A. Fisher’s classic 1936 paper, <a href="http://rcs.chemometrics.ru/Tutorials/classification/Fisher.pdf">The Use of Multiple Measurements in Taxonomic Problems</a>, and can also be found in the <a href="http://archive.ics.uci.edu/ml/">UCI Machine Learning Repository</a>.</p><p>It includes three iris species with 50 samples each as well as some properties of each flower. One flower species is linearly separable from the other two, but the other two are not linearly separable from each other.</p><p>The columns in this dataset are:</p><ul><li>SepalLengthCm</li><li>SepalWidthCm</li><li>PetalLengthCm</li><li>PetalWidthCm</li><li>Target</li></ul><p>What’s the point if we don’t show the proof, Enough Said.</p><p><strong>Let’s Rock and Roll 👇🏻</strong></p><pre># Importing all the neccessary libraries<br><br>import pandas as pd  # to analyze data<br>import numpy as np  # to perform a wide variety of mathematical operations on arrays<br>import seaborn as sns # to visualize random distributions.<br>import matplotlib.pyplot as plt # to perform data visualization and graphical plotting<br>%matplotlib inline<br>from sklearn.datasets import load_iris<br>from sklearn.tree import DecisionTreeClassifier, plot_tree</pre><h3>Load and Read Dataset</h3><pre># Load and read an inbuild &#39;iris&#39; dataset<br><br>iris = load_iris()<br>data_f = pd.DataFrame(data = np.c_[iris[&quot;data&quot;], iris[&quot;target&quot;]],columns = iris.feature_names+[&quot;target&quot;])</pre><pre># print first 5 records<br><br>data_f.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9v6M6_-J-MI1BaKy2on0TA.png" /><figcaption>Head()</figcaption></figure><pre># print last 5 records<br><br>data_f.tail()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q6I_nhGRa2cEqbsocTvK_A.png" /><figcaption>Tail()</figcaption></figure><pre># let&#39;s assign the value for each target  0:setosa, 1:versicolor, 2:virginica<br><br>print(np.unique(iris.target))<br>iris.target_names</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jrHDc75lwitOJRENIKK9gQ.png" /><figcaption>Target Variables</figcaption></figure><pre># let&#39;s define and return output with the right set<br><br>def names(a):<br>    if(a==0): return &quot;Iris-setosa&quot;<br>    elif(a==1): return &quot;Iris-Versicolor&quot;<br>    else: return &quot;Iris-Virginica&quot;<br>    <br>data_f[&quot;target_name&quot;] = data_f[&quot;target&quot;].apply(names)<br><br>data_f.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6DWoH80onM1uu1WALWdBlg.png" /><figcaption>Head()</figcaption></figure><pre># let&#39;s check the nulls if there are any<br><br>data_f.isnull().sum()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ioWzdDRlRKhqdXsu1G50pw.png" /><figcaption>Check the NULL values</figcaption></figure><pre># print the shape of the dataset<br><br>data_f.shape   # 150 rows and 6 columns</pre><pre>(150, 6)</pre><pre># let&#39;s separate independant variables and dependant variable<br><br>data = data_f.values<br><br>x = data[:,0:4]<br>y = data[:,4]</pre><pre>y_data = np.array([np.average(x[:,i][y==j]) for i in range(x.shape[1]) for j in (np.unique(y))])<br>y_data = y_data.reshape(4,3)<br>y_data = np.swapaxes(y_data, 0,1)<br><br>x_axis = np.arange(len(data_f.columns)-2)<br>width = 0.2</pre><h3>Visual Represantation</h3><pre># let&#39;s visualize the length and width features<br><br>plt.figure(figsize =(12,8))<br>plt.bar(x_axis, y_data[0], width, label = &quot;Setosa&quot;)<br>plt.bar(x_axis+width, y_data[1], width, label = &quot;Versicolour&quot;)<br>plt.bar(x_axis+width*2, y_data[2], width, label = &quot;Verginica&quot;)<br><br>plt.xticks(x_axis, [&quot;Sepal length&quot;, &quot;Sepal width&quot;, &quot;Petal length&quot;, &quot;Petal width&quot;])<br>plt.xlabel(&quot;Features&quot;)<br>plt.ylabel(&quot;Value(cm).&quot;)<br>plt.legend(bbox_to_anchor=(1,1))<br><br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_KAnXPt038Hfp2QVj1bsPQ.png" /><figcaption>Graphical Representation of Features.</figcaption></figure><h3>Train Test Split</h3><pre># let&#39;s split the data into train and test<br><br>x = data_f.drop([&quot;target&quot;, &quot;target_name&quot;], axis = &quot;columns&quot;)<br>y = data_f[&quot;target&quot;]<br><br>from sklearn.model_selection import train_test_split<br><br>x_train, x_test, y_train , y_test = train_test_split(x, y, test_size = .2, random_state = 420)</pre><h3>Decision Tree Classifier Model</h3><pre>model_dt = DecisionTreeClassifier()<br><br><br># let&#39;s fit the model<br>model_dt.fit(x_train, y_train)</pre><pre>DecisionTreeClassifier()</pre><pre># let&#39;s set the prediction model<br><br>model_dt.predict(x_test)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-cKUJpkhtMFdUlcrVEaBGw.png" /><figcaption>Predictive Results</figcaption></figure><pre># let&#39;s check the model score<br><br>model_dt.score(x_test, y_test)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NVJ9DZtirGN8kw53Her7yQ.png" /><figcaption>Accuracy of Predictive Class</figcaption></figure><h3>Cross Validation Score Function</h3><pre>from sklearn.model_selection import cross_val_score, ShuffleSplit<br><br>cv = ShuffleSplit(n_splits=6, test_size = .2)<br><br>arr = cross_val_score(DecisionTreeClassifier(), x, y, cv= cv)<br><br>print(list(arr))<br>print(&quot;Average Score:&quot;, np.mean(arr))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nAC3s9lbA5Pe7KJUpuY6GA.png" /><figcaption>Average Accuracy Score</figcaption></figure><h3>Decision Tree Visualization</h3><pre>fig = plt.figure(figsize=(25,20))<br>fig = plot_tree(model_dt, feature_names = iris.feature_names, class_names = iris.target_names,<br>               rounded = True,<br>               filled = True)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N-sZh8m1ieYtj5k_LZb8Qg.png" /><figcaption>Graphical Representation of Decision Tree Classifier</figcaption></figure><h3><strong>Interpretation</strong></h3><p>Less the Gini Score better the model, however the Decision Tree Classifier Graph representation we can interpret that the ‘Petal Width’ has more variance than the other variables hence trees getting splits with Petal Width.</p><p>GitHub Repository: <a href="https://github.com/KVishwas98/TSF-Task6-Prediciton-using-Decision-Tree-Algorithm">https://github.com/KVishwas98/TSF-Task6-Prediciton-using-Decision-Tree-Algorithm</a></p><h3>Conclusion</h3><p>If you find any difficulty in following the code part, mention them in the comment section.</p><p><em>Thank you for reading! Let me know in a comment or on </em><a href="https://www.linkedin.com/in/kvishwas98"><em>LinkedIn</em></a><em> if you felt like this did or didn’t help. I’ve got a few more articles that I’m writing and will be posting them every couple of weeks. They are mostly accounts from my project experience. If there are any other questions or anything else you’d like to hear about, please don’t hesitate to put in a request.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b77ecb3ca90" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Decision Trees 1/2]]></title>
            <link>https://towardsdev.com/decision-trees-1-2-583c1df0db09?source=rss-2ee4f5bd7fcc------2</link>
            <guid isPermaLink="false">https://medium.com/p/583c1df0db09</guid>
            <category><![CDATA[decision-tree]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[supervised-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Vishwas Kshirsagar]]></dc:creator>
            <pubDate>Wed, 28 Dec 2022 13:07:50 GMT</pubDate>
            <atom:updated>2023-01-04T20:35:46.661Z</atom:updated>
            <content:encoded><![CDATA[<p>1/2 contains the theory part of Decision Trees</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5XSTZeMXta50pCoB2qYpuQ.jpeg" /><figcaption>Decision Tree Algorithm</figcaption></figure><p>Decision tree algorithms are a type of machine learning algorithm that is used for classification and regression tasks. They work by constructing a tree-like model of decisions based on the features of the data and using this model to make predictions.</p><p>The decision tree algorithm works by constructing a tree-like model of decisions based on the features of the data. The tree is constructed by making a series of decisions based on the features of the data, and each decision splits the data into two or more branches based on the values of the features. The process continues until the data is split into groups that are as pure as possible, with each group containing data points that are similar to each other.</p><p>To make a prediction using a decision tree, the algorithm follows the branches of the tree based on the values of the features of the data point being predicted. The final branch that is reached is used to make the prediction.</p><p>Decision tree algorithms are popular because they are easy to understand and interpret, and they can handle both numerical and categorical data. They are also relatively fast to train and make predictions with. However, they can be prone to overfitting, and they may not perform as well as some other algorithms on certain types of data.</p><h3><strong>Example</strong></h3><p>Here is a simple example of a decision tree algorithm being used to classify data points based on their features:</p><p>Suppose we have a dataset of animals, and we want to build a decision tree to classify each animal as either a “mammal” or a “bird”. The dataset contains two features: “has feathers” and “gives milk”.</p><p>The decision tree algorithm starts by examining the feature that best splits the data into two groups. In this case, the “has feathers” feature is the best choice, as it can be used to split the data into two groups: animals that have feathers and animals that do not.</p><p>The decision tree algorithm then creates two branches, one for animals that have feathers and one for animals that do not. For the animals that have feathers, the decision tree algorithm checks the “gives milk” feature. If the animal gives milk, it is classified as a mammal. If it does not give milk, it is classified as a bird. For the animals that do not have feathers, the decision tree algorithm classifies them as mammals.</p><p>Here is a visual representation of the decision tree:</p><pre>      has feathers<br>     /            \<br>mammal            bird<br>   |                |<br>gives milk      does not give milk</pre><p>Using this decision tree, we can classify new animals by following the branches of the tree based on the values of their features. For example, if we encounter a new animal that has feathers and gives milk, the decision tree would classify it as a mammal.</p><h3><strong>Assumptions</strong></h3><p>Decision tree algorithms make a number of assumptions about the data and the problem being solved. These assumptions can affect the performance and interpretability of the model, and it is important to consider them when using decision tree algorithms.</p><p>Some of the assumptions of decision tree algorithms are:</p><ol><li>Decision trees work best when the features of the data are independent of each other. If the features are correlated, the decision tree may make decisions based on correlated features rather than on the underlying structure of the data.</li><li>Decision trees assume that the data is stationary, meaning that the statistical properties of the data do not change over time. If the data is non-stationary, the decision tree may not be able to accurately model the data.</li><li>Decision trees assume that the data can be split into groups that are as pure as possible. If the data is not well-separated, the decision tree may not be able to accurately model the data.</li><li>Decision trees assume that the data is homogeneous, meaning that all the data points belong to the same class or follow the same distribution. If the data is heterogeneous, the decision tree may not be able to accurately model the data.</li><li>Decision trees assume that the data is complete and accurate. If the data is missing or contains errors, the decision tree may not be able to accurately model the data.</li></ol><p>It is important to consider these assumptions when using decision tree algorithms and to adjust the model or the data as necessary to ensure that the assumptions are satisfied.</p><h3><strong>Drawbacks</strong></h3><p>However, like all machine learning algorithms, decision trees have some drawbacks that should be considered when deciding whether to use them for a particular problem.</p><p>Some of the drawbacks of decision trees are:</p><ol><li><strong>Overfitting</strong>: Decision trees can be prone to overfitting, especially when the tree is very deep and has many branches. Overfitting occurs when the model fits the training data too closely and does not generalize well to new data. This can lead to poor performance on test data or real-world data.</li><li><strong>Sensitivity to data</strong>: Decision trees can be sensitive to the specific data used to train them. If the data changes slightly, the tree structure and the resulting predictions can change significantly. This can make decision trees less stable and harder to interpret than some other algorithms.</li><li><strong>Limited ability to handle continuous data</strong>: Decision trees are better suited for handling categorical data than continuous data. They may not be as effective at modeling relationships between continuous variables, or at making predictions with continuous data.</li><li><strong>Limited ability to handle large datasets</strong>: Decision trees can become very large and complex when trained on large datasets, which can make them slower to train and use than some other algorithms.</li><li><strong>Limited ability to handle multi-class problems</strong>: Decision trees are generally better suited for binary classification problems than multi-class problems. They may not be as effective at predicting the class of a data point when there are more than two classes.</li></ol><p><strong>Stay tuned for the 2/2. Will perform hands-on with the Decision Trees driven dataset.</strong></p><h3>Conclusion</h3><p>If you find any difficulty in following the theory part, mention them in the comment section.</p><p><em>Thank you for reading! Let me know in a comment or on </em><a href="https://www.linkedin.com/in/kvishwas98"><em>linkedin</em></a><em> if you felt like this did or didn’t help. I’ve got a few more articles that I’m writing and will be posting them every couple of weeks. They are mostly accounts from my project experience. If there are any other questions or anything else you’d like to hear about, please don’t hesitate to put in a request.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=583c1df0db09" width="1" height="1" alt=""><hr><p><a href="https://towardsdev.com/decision-trees-1-2-583c1df0db09">Decision Trees 1/2</a> was originally published in <a href="https://towardsdev.com/">Towards Dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[K — Nearest Neighbor KNN 2/2]]></title>
            <link>https://towardsdev.com/k-nearest-neighbor-knn-2-2-2278f3baef43?source=rss-2ee4f5bd7fcc------2</link>
            <guid isPermaLink="false">https://medium.com/p/2278f3baef43</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[knn-algorithm]]></category>
            <category><![CDATA[supervised-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Vishwas Kshirsagar]]></dc:creator>
            <pubDate>Fri, 09 Dec 2022 12:09:22 GMT</pubDate>
            <atom:updated>2023-01-03T04:10:16.172Z</atom:updated>
            <content:encoded><![CDATA[<h3>K — Nearest Neighbor KNN 2/2</h3><p>2/2 contains the code part of the K — Nearest Neighbor Classifier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ngkO0bSZzArzRBzpF1LXXQ.jpeg" /><figcaption>citation: <a href="https://hackerbits.com/">https://hackerbits.com/</a></figcaption></figure><p><strong>A quick recap of what we have learned in the </strong><a href="https://medium.com/towardsdev/k-nearest-neighbor-knn-1-2-acda977a9dce">K — Nearest Neighbor KNN 1/2</a></p><p>The k-nearest neighbors algorithm, also known as KNN or k-NN, is a non-parametric, supervised learning classifier, which uses proximity to make classifications or predictions about the grouping of an individual data point.</p><p>While it can be used for either regression or classification problems, it is typically used as a classification algorithm, working off the assumption that similar points can be found near one another.</p><h3><strong>About Dataset</strong></h3><p>The Iris dataset was used in R.A. Fisher’s classic 1936 paper, <a href="http://rcs.chemometrics.ru/Tutorials/classification/Fisher.pdf">The Use of Multiple Measurements in Taxonomic Problems</a>, and can also be found in the <a href="http://archive.ics.uci.edu/ml/">UCI Machine Learning Repository</a>.</p><p>It includes three iris species with 50 samples each as well as some properties of each flower. One flower species is linearly separable from the other two, but the other two are not linearly separable from each other.</p><p>The columns in this dataset are:</p><ul><li>Id</li><li>SepalLengthCm</li><li>SepalWidthCm</li><li>PetalLengthCm</li><li>PetalWidthCm</li><li>Target</li><li>flower_name</li></ul><p>What’s the point if we don’t show the proof, Enough Said.</p><p><strong>Let’s Rock and Roll 👇🏻</strong></p><pre># import all the necessary libraries<br><br>import pandas as pd<br><br>#importing the inbuilt dataset<br><br>from sklearn.datasets import load_iris<br><br>#define and load the dataset<br><br>iris = load_iris()</pre><pre># check the features name<br><br>iris.feature_names</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*Rg-kRvhkNVJyqB7dzhg5Kw.png" /></figure><pre># check the target names<br><br>iris.target_names</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*Gu2iDnBtk0WndDaFwpS_6A.png" /></figure><pre># converting dataset into the dataframe format<br><br>df = pd.DataFrame(iris.data, columns = iris.feature_names)<br><br>#print the first 5 records<br><br>df.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7d7oo7dWmm_cB4XpNiJDFQ.png" /></figure><pre># print the total number of columns and rows<br><br>df.shape     # 150 rows and 4 columns</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*fTrxa5be0MhMN5dOqxUEkw.png" /></figure><pre># insert a new column &#39;target&#39;<br><br>df[&#39;target&#39;] = iris.target   # setosa flower<br><br># print the first 5 records<br><br>df.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w0TXt1OK8Iodpwl2BsF3eA.png" /></figure><pre>df[df.target == 1].head()  # versicolor flower</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sN1Ca9ZrSPPshEVWjwmfgQ.png" /></figure><pre>df[df.target == 2].head()  # virginica flower</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*Ulm7ztuvMFed83_18ku0Ew.png" /></figure><pre># Assigning flower name as per the target assigned<br><br>df[&#39;flower_name&#39;] = df.target.apply(lambda x: iris.target_names[x]) <br>df.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xZFyHoQu3SLu5wvkdmCF2A.png" /></figure><pre>df[45:55]  # check the two classes.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hXJL6pDBjVjPRArP7eakyA.png" /></figure><pre># split the data in three variables as per the flower name<br><br>df0 = df[:50]      # setosa flower<br>df1 = df[50:100]   # Versicolor flower<br>df2 = df[100:]     # virginica flower</pre><pre># plot the graph <br><br>import matplotlib.pyplot as plt<br>%matplotlib inline</pre><h3>Sepal Length vs Sepal Width (Setosa vs Versicolor)</h3><pre>plt.xlabel(&#39;Sepal Length&#39;)<br>plt.ylabel(&#39;Sepal Width&#39;)<br>plt.scatter(df0[&#39;sepal length (cm)&#39;], df0[&#39;sepal width (cm)&#39;], color = &quot;green&quot;, marker=&#39;+&#39;)<br>plt.scatter(df1[&#39;sepal length (cm)&#39;], df1[&#39;sepal width (cm)&#39;], color= &quot;red&quot;, marker = &#39;+&#39; )</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*QOsCWA5BQO1bgI1O6eQ63g.png" /></figure><h3>Petal Length vs Petal Width (Setosa vs Versicolor)</h3><pre>plt.xlabel(&quot;Petal Length&quot;)<br>plt.ylabel(&quot;Petal Width&quot;)<br>plt.scatter(df0[&#39;petal length (cm)&#39;], df0[&#39;petal width (cm)&#39;], color= &quot;green&quot;, marker= &quot;+&quot;)<br>plt.scatter(df1[&#39;petal length (cm)&#39;], df1[&#39;petal width (cm)&#39;], color= &#39;red&#39;, marker= &quot;+&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TE6W-PGQp8yx4zN_tBAZ4w.png" /></figure><h3>Split into Train and Test</h3><pre>from sklearn.model_selection import train_test_split</pre><pre>X = df.drop([&quot;target&quot;, &quot;flower_name&quot;], axis=&quot;columns&quot;)<br>y = df.target</pre><pre>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.2, random_state= 420)</pre><pre># check the legnth of the &#39;X_train&#39;<br><br>len(X_train)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*i0KOO58H2isiOJ-Uwvwz7g.png" /></figure><pre># check the length of the &#39;X_test&#39;<br>len(X_test)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*_0T4kjTr9C0rmI8GC4SD-A.png" /></figure><h3>KNN (K — Nearest Neighbour Classifier)</h3><pre>from sklearn.neighbors import KNeighborsClassifier<br><br># define and stored the knn library in &#39;knn&#39; <br><br>knn = KNeighborsClassifier(n_neighbors=3)<br><br># fit the algorithm<br><br>knn.fit(X_train, y_train)</pre><pre>knn.score(X_test, y_test)   # check the score of test dataset</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/1*dgWioTqbh20HMACQZEng_A.png" /></figure><pre># to see the matrix plot <br><br>from sklearn.metrics import confusion_matrix<br><br>y_pred = knn.predict(X_test)<br><br>cm = confusion_matrix(y_test, y_pred)<br><br>cm</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J4BafQjMwEd5cUcVhoXAmw.png" /></figure><pre># plot the confusion matrix graph with the heatmap<br><br>%matplotlib inline<br>import matplotlib.pyplot as plt  # visualise the result<br>import seaborn as sns  # seaborn is for plotting syntific numeric graph<br><br>plt.figure(figsize=(7,5))<br>sns.heatmap(cm, annot=True)<br>plt.xlabel(&#39;Predicted&#39;)<br>plt.ylabel(&quot;Actual&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5hJZtcsof4Hw5tIfgjgCXA.png" /></figure><pre># print the precision, recall, f1-scores<br><br>from sklearn.metrics import classification_report<br><br>cl_report = classification_report(y_test, y_pred)<br><br>print(cl_report)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/944/1*fHNRcUT4nnSX1-etUo6Xkw.png" /></figure><p>GitHub Repository: <a href="https://github.com/KVishwas98/KNN-K---Nearest-Neighbour-Classifier-">https://github.com/KVishwas98/KNN-K---Nearest-Neighbour-Classifier-</a></p><h3>Conclusion</h3><p>If you find any difficulty in following the code part, mention them in the comment section.</p><p><em>Thank you for reading! Let me know in a comment or on </em><a href="https://www.linkedin.com/in/kvishwas98"><em>linkedin</em></a><em> if you felt like this did or didn’t help. I’ve got a few more articles that I’m writing and will be posting them every couple of weeks. They are mostly accounts from my project experience. If there are any other questions or anything else you’d like to hear about, please don’t hesitate to put in a request.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2278f3baef43" width="1" height="1" alt=""><hr><p><a href="https://towardsdev.com/k-nearest-neighbor-knn-2-2-2278f3baef43">K — Nearest Neighbor KNN 2/2</a> was originally published in <a href="https://towardsdev.com/">Towards Dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[K — Nearest Neighbor KNN 1/2]]></title>
            <link>https://towardsdev.com/k-nearest-neighbor-knn-1-2-acda977a9dce?source=rss-2ee4f5bd7fcc------2</link>
            <guid isPermaLink="false">https://medium.com/p/acda977a9dce</guid>
            <category><![CDATA[data-analytics]]></category>
            <category><![CDATA[knn-algorithm]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Vishwas Kshirsagar]]></dc:creator>
            <pubDate>Wed, 07 Dec 2022 06:22:47 GMT</pubDate>
            <atom:updated>2022-12-07T12:26:48.568Z</atom:updated>
            <content:encoded><![CDATA[<h3>K — Nearest Neighbor KNN 1/2</h3><p>1/2 contains the theory part of K — Nearest Neighbor.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*wUAOzIF2z2WNvzwjdtE2qA.png" /></figure><p>In a machine learning task, we usually have two kinds of problems that are to be solved either can be ‘Classification’ or it can be ‘Regression’ problem.</p><p>Classification is a process of sorting a given set of data into each different classes. Classification can be implemented on both kinds of data structured as well as unstructured. Classes are often referred to as labels or targets which hold different classes. For example, classifying different fruits.</p><p>Regression is a problem in which our target holds continuous values or real values. Like prediction of the salary or age of a person. K-Nearest Neighbour is one of the simplest Machine Learning algorithms based on the Supervised Learning technique.</p><p>KNN is a simple, supervised machine learning (ML) algorithm that can be used for classification or regression tasks — and is also frequently used in missing value imputation.</p><p>It is based on the idea that the observations closest to a given data point are the most “similar” observations in a data set, and we can therefore classify unforeseen points based on the values of the closest existing points. By choosing K, the user can select the number of nearby observations to use in the algorithm.</p><p>It is also called a lazy learner algorithm because it does not learn from the training set immediately instead it stores the dataset and at the time of classification, it performs an action on the dataset.</p><p>KNN algorithm at the training phase just stores the dataset and when it gets new data, then it classifies that data into a category that is much similar to the new data.</p><p>Example: Suppose, we have an image of an animal that looks similar to a cat and a dog, but we want to know whether it is a cat or a dog. So, for this identification, we can use the KNN algorithm, as it works on a similarity measure. Our KNN model will find the similar features of the new data set to the cats and dogs images and based on the most similar features it will put it in either the cat or dog category.</p><p><strong>How does K-NN work?</strong></p><p>The K-NN working can be explained on the basis of the below algorithm:</p><ul><li>Step 1: Select the number K of the neighbors</li><li>Step 2: Calculate the Euclidean distance of K number of neighbors</li><li>Step 3: Take the K nearest neighbors as per the calculated Euclidean distance.</li><li>Step 4: Among these k neighbors, count the number of the data points in each category.</li><li>Step 5: Assign the new data points to that category for which the number of neighbors is maximum.</li><li>Step 6: Our model is ready.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/0*JTUU4d-HP_mT1JW8.png" /></figure><p>KNN (Graph Representation)</p><p><strong>How to select the value of K in the K-NN Algorithm?</strong></p><ul><li>There is no particular way to determine the best value for “K”, so we need to try some values to find the best out of them. The most preferred value for K is 5.</li><li>A very low value for K such as K=1 or K=2, can be noisy and lead to the effects of outliers in the model.</li><li>Large values for K are good, but it may find some difficulties.</li></ul><p><strong>Advantages of the KNN Algorithm:</strong></p><ul><li>A simple algorithm that is easy to understand.</li><li>Used for nonlinear data.</li><li>The versatile algorithm is used for both classifications as well as regression.</li><li>Gives high accuracy but there are more good algorithms in supervised models.</li><li>The algorithm doesn’t demand building a model, tuning several model parameters, or making additional assumptions.</li></ul><p><strong>Disadvantages of the KNN Algorithm:</strong></p><ul><li>The requirement of high storage.</li><li>The prediction rate is slow.</li><li>Stores all the training data.</li><li>The algorithm gets slower when the number of examples, predictors, or independent variables increases.</li></ul><p><strong>Stay tuned for the 2/2. Will perform hands-on with the KNN dataset.</strong></p><h3>Conclusion</h3><p>If you find any difficulty in following the theory part, mention them in the comment section.</p><p><em>Thank you for reading! Let me know in a comment or on </em><a href="https://www.linkedin.com/in/kvishwas98"><em>linkedin</em></a><em> if you felt like this did or didn’t help. I’ve got a few more articles that I’m writing and will be posting them every couple of weeks. They are mostly accounts from my project experience. If there are any other questions or anything else you’d like to hear about, please don’t hesitate to put in a request.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=acda977a9dce" width="1" height="1" alt=""><hr><p><a href="https://towardsdev.com/k-nearest-neighbor-knn-1-2-acda977a9dce">K — Nearest Neighbor KNN 1/2</a> was originally published in <a href="https://towardsdev.com/">Towards Dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Logistic Regression 2/2]]></title>
            <link>https://towardsdev.com/logistic-regression-2-2-ae468a190ea3?source=rss-2ee4f5bd7fcc------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae468a190ea3</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-analytics]]></category>
            <category><![CDATA[supervised-learning]]></category>
            <category><![CDATA[logistic-regression]]></category>
            <dc:creator><![CDATA[Vishwas Kshirsagar]]></dc:creator>
            <pubDate>Mon, 28 Nov 2022 04:08:38 GMT</pubDate>
            <atom:updated>2022-12-09T11:55:17.087Z</atom:updated>
            <content:encoded><![CDATA[<p>The previous blog taught us “What logistic regression actually is?”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*jBmoK4u2m4sb6lvFfuDeBw.jpeg" /><figcaption>ref: Logistic Regression</figcaption></figure><p><strong>Let’s recap what we have learned in the </strong><a href="https://medium.com/towardsdev/logistic-regression-b7daceb6c84e"><strong>Logistic Regression 1/2.</strong></a></p><p>Logistic regression is one of the most popular Machine Learning algorithms, which comes under the Supervised Learning technique. It is used for predicting the categorical dependent variable using a given set of independent variables.</p><p>Logistic regression predicts the output of a categorical dependent variable. Therefore the outcome must be a categorical or discrete value. It can be either Yes or No, 0 or 1, true or False, etc. but instead of giving the exact value as 0 and 1,<strong> </strong>it gives the probabilistic values which lie between 0 and 1.</p><p>Logistic Regression is much similar to Linear Regression except that how they are used. Linear Regression is used for solving Regression problems, whereas<strong> </strong>Logistic regression is used for solving the classification problems.</p><p>Enough Said, <strong>Let’s Rock and Roll👇🏻</strong></p><p>To understand Logistic Regression better way, we will consider <strong>Attrition Dataset</strong>.</p><h3><strong>Understanding of the dataset</strong></h3><p>Employee attrition is the gradual reduction in employee numbers. <br>Employee attrition happens when the size of your workforce diminishes over time.</p><p>This means that employees are leaving faster than they are hired. <br>Employee attrition happens when employees retire, resign, or simply aren’t replaced.</p><p>This is a critical condition for the company, so to avoid unforeseen circumstances.<br>this model will help them create a future-based plan to save their company from getting early notification of employee leaving conditions. <br>however, they could either retain their employees or start hiring new ones.</p><pre># importing all the necessary libraries<br><br>import numpy as np  # numeric Manipulation and numepric operation<br>import pandas as pd  # data manipulation and EDA part<br>import matplotlib.pyplot as plt  # visualize the result<br>import seaborn as sns  # scintific numeric manipulation and visualization</pre><pre>import os # to get the operating system&#39;s path</pre><pre>os.chdir(&quot;C:/Users/vishw/OneDrive/Documents/Imarticus/Python/Machine Learning&quot;)</pre><pre>df = pd.read_csv(&quot;Attrition.csv&quot;)</pre><pre>df   # print the dataset</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UUziY4-cuNUFsHOJ0PpCZw.png" /></figure><pre>df.columns  # print the total columns</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9O0Wno_I2Fsz4uKtNgYN2A.png" /></figure><pre>df.drop([&quot;EmployeeNumber&quot;], axis=1, inplace =True)   # drop the &#39;EmployeeNumber&#39; since this will not contribute to the prediction</pre><pre>df.drop([&quot;EmployeeCount&quot;], axis=1, inplace=True)  # drop the &#39;EmployeeCount&#39; since this will not contribute to the prediction</pre><pre>from sklearn.preprocessing import LabelEncoder  #  converting the labels into a numeric form so as to convert them into the machine-readable form.<br>le = LabelEncoder()</pre><pre># compute the mean and std operation and perform scaling on the object columns.<br><br>df[df.select_dtypes(include=[&quot;object&quot;]).columns] = df[df.select_dtypes(include=[&quot;object&quot;]).columns].apply(le.fit_transform)</pre><pre>df.head()  # print the first 5 records</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HgkFQLn1317aTsBRmi1oRw.png" /></figure><pre>df.Gender.value_counts()  # total number of dependant rows counts per 0 and 1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jfz1_Ey4s3c_wMq8atWavA.png" /></figure><pre>from sklearn.model_selection import train_test_split  # importing the train_test_split</pre><pre>df_train, df_test = train_test_split(df, test_size = .2)  # splitting  the data into train, test</pre><pre># to increase the FPR we are concatenating more samples in the data<br>#df1 =  df_train[df_train.Attrition  == 1]<br>#df_train = pd.concat([df_train  , df1, df1, df1, df1, df1])</pre><pre>df.Attrition.value_counts()   # dummy data added for class 0</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ikF0goUTjctDiRrcTVApuA.png" /></figure><pre># splitting data into train_x and Train_y<br>df_train_x = df_train.iloc[:, 0: -1]   #  basically all the independant variables will be in train_x<br>df_train_y = df_train.iloc[:, -1]   # and dependant will be in train_y</pre><pre># splitting data into test_x and Test_y<br><br>df_test_x = df_test.iloc[:, 0: -1] #  basically all the independant variables will be in test_x<br>df_test_y = df_test.iloc[:, -1]  # # and dependant will be in test_y</pre><pre>from sklearn.linear_model import LogisticRegression  # importing the logistic regression from sklearn library<br>logreg = LogisticRegression()</pre><pre>logreg.fit(df_train_x, df_train_y)   # fit the train model in the logistic regression algorithm</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*XOMIAU7UFqLSJeMR5RYVJg.png" /></figure><pre>pred = logreg.predict(df_test_x)  # to train the model prediction set to the test_x</pre><pre>from sklearn.metrics import confusion_matrix   # print the confusion matrix purpose</pre><pre># from left to right actual and top to bottom predicted values<br><br>conf = confusion_matrix(df_test_y, pred)<br>conf</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gb6ql6Cmmr9yrqtvEBD1zQ.png" /></figure><pre>from sklearn.metrics import accuracy_score  # print the accuracy <br>accuracy_score(df_test_y, pred)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*ilyjnp42cckkhNcwcTN0eQ.png" /></figure><pre>from sklearn.metrics import recall_score   # Print the recall score consist on (TP/TP+FN)<br>recall_score(df_test_y, pred)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4QejFNF61s6w4vooec5flA.png" /></figure><pre>from sklearn.metrics import precision_score # print the precision score (TP/TP+FP)<br>precision_score(df_test_y, pred)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/944/1*BE1mU7Nt1bnPCepr4VVbZw.png" /></figure><pre>from sklearn.metrics import f1_score  # harmonic mean f1 score ((2*precision*recall) / precision + recall)<br>f1_score(df_test_y, pred)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1006/1*pAfrFgYQh1mH8LHt454LAg.png" /></figure><pre>pred = logreg.predict(df_test_x)   #print the prediction<br><br>pred</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ztP7-D3Bt-UM6rbk73zNDw.png" /></figure><pre>pred_prob = logreg.predict_proba(df_test_x) # print the prediction in probability <br><br>pred_prob</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Bnf58o0RMa5zNtCfrLuLg.png" /></figure><pre>len(pred_prob)  # print the length of the pred_prob</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/861/1*gq_bLtCM36rM2VyUYAj2bQ.png" /></figure><pre>df_test_x.shape  # print the shape of the number of columns and rows</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WdcKhWJ3n-3l_b6rkFggOA.png" /></figure><pre>from sklearn.metrics import roc_auc_score<br><br>from sklearn.metrics import roc_curve</pre><pre>roc_auc_score(df_test_y, pred)</pre><pre>pred_prob = logreg.predict_proba(df_test_x)<br><br>    pred_prob[:, 1]    #print the class 1 probability</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HLwwHl-DLdgCtnizjJCBxw.png" /></figure><pre># plot the curve<br>fpr, tpr, thresh = roc_curve(df_test_y, pred_prob[:, 1])</pre><pre>import matplotlib.pyplot as plt</pre><pre>area = roc_auc_score(df_test_y, pred)<br>area = round(area, 3)<br>area</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vdk6RohvJkxSwcpBz5vxOA.png" /></figure><pre>plt.plot(fpr, tpr)<br>plt.xlabel(&quot;fpr&quot;)<br>plt.ylabel(&quot;tpr&quot;)<br>plt.title(&quot;AUROC plot on Attrition Data&quot;)<br>plt.grid()<br>plt.text(x = .5, y =.5, s = &quot;Area under curve&quot;)<br>plt.text(x = .5, y =.4, s = area, size =15)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eP0wIGscCJ_bYsoQEXO0gw.png" /></figure><p>In general, an AUC of 0.5 suggests no discrimination (i.e., the ability to diagnose with and without the or condition based on the test).</p><p>0.7 to 0.8 is considered acceptable, and 0.8 to 0.9 is considered excellent. and more than 0.9 is considered outstanding.</p><pre>pred_prob_full = logreg.predict_proba(df.iloc[: , 0: -1]) # considering the records with the indexing</pre><pre>pred_prob_full = pd.DataFrame(pred_prob_full)  # converting into the dataframe</pre><pre># remaning the columns names to be able to read and understand better.<br><br>pred_prob_full = pred_prob_full.rename(columns = {pred_prob_full.columns[0] : &quot;PredProbe0&quot;,<br>                                                 pred_prob_full.columns[1] : &quot;PredProb1&quot;})</pre><pre># sorting value in sceding to show highest probability at the first<br><br>pred_prob_full = pred_prob_full.sort_values(&quot;PredProb1&quot;, ascending = False)</pre><pre>pred_prob_full = pred_prob_full.iloc[: ,[0,1]]  # printing by the indexing</pre><pre>pred_prob_full    # printing the results.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lih_sAyaladW62eacj8Zpg.png" /></figure><p>GitHub Repository: <a href="https://github.com/KVishwas98/Logistic-Regression---Attrition-Dataset">https://github.com/KVishwas98/Logistic-Regression---Attrition-Dataset</a></p><h3>Conclusion</h3><p>If you find any difficulty in following the code part, mention them in the comment section.</p><p><em>Thank you for reading! Let me know in a comment or on </em><a href="https://www.linkedin.com/in/kvishwas98"><em>linkedin</em></a><em> if you felt like this did or didn’t help. I’ve got a few more articles that I’m writing and will be posting them every couple of weeks. They are mostly accounts from my project experience. If there are any other questions or anything else you’d like to hear about, please don’t hesitate to put in a request.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae468a190ea3" width="1" height="1" alt=""><hr><p><a href="https://towardsdev.com/logistic-regression-2-2-ae468a190ea3">Logistic Regression 2/2</a> was originally published in <a href="https://towardsdev.com/">Towards Dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Logistic Regression 1/2]]></title>
            <link>https://towardsdev.com/logistic-regression-b7daceb6c84e?source=rss-2ee4f5bd7fcc------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7daceb6c84e</guid>
            <category><![CDATA[data-scientist]]></category>
            <category><![CDATA[theory]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[logistic-regression]]></category>
            <dc:creator><![CDATA[Vishwas Kshirsagar]]></dc:creator>
            <pubDate>Mon, 21 Nov 2022 10:13:43 GMT</pubDate>
            <atom:updated>2022-11-21T14:10:09.462Z</atom:updated>
            <content:encoded><![CDATA[<p>1/2 contains the theory part of logistic regression</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OIIeLS0j2l-GR7cHQLfywQ.jpeg" /><figcaption>ref: Logistic Regression</figcaption></figure><p>Logistic regression is one of the most popular Machine Learning algorithms, which comes under the Supervised Learning technique. It is used for predicting the categorical dependent variable using a given set of independent variables.</p><p>Logistic regression predicts the output of a categorical dependent variable. It can be either Yes or No, 0 or 1.</p><p>If you see closely logistic regression is as same as linear regression. The only difference is linear regression is used for solving the regressional problem. and logistic regression is used for solving a classification problem.</p><p>If the dependent variable is not in binary format. then we simply convert it into 0 or 1. If the actual value is 0 and 1, then the predicted value will also be 0 and 1. Class 0 is taken as a negative class and class 1 is always taken as a positive class.</p><p>In logistic regression, To judge the performance of the model. We use a confusion matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TT08ZvxMJjBhHRIbPe8Itg.png" /><figcaption>Confusion Matrix</figcaption></figure><p>from the top of the table, we get the predicted value.</p><p>from the left to right, we get the actual value.</p><p>Class 0 is taken as a negative class, class 1 is taken as a positive class.</p><p>N is the other name for class 0 and Y is the name for class 1.</p><p>T — True prediction</p><p>F — False Prediction</p><p>TN — True Negative Class</p><p>TP — True Positive Class</p><p>The final decision is not taken on the actual value, but on the predicted value.</p><h3><strong>Accuracy Score:</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kkChBcysIeAJfyd_gHqIzw.png" /></figure><p>Accuracy is not the only parameter to judge the model or decide the performance model.</p><p>A model can be useless even if overall accuracy is very high. The model should not be judged only based on accuracy but the model should be evaluated on model performance. It should be mapped to the problem statement requirement.</p><h3><strong>True Positive Ratio (TPR)</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7gK_LkR0igXrR42c4zoZCQ.png" /><figcaption>True Positive Ratio</figcaption></figure><p>The true Positive Ratio is to measure the performance of class 1. The higher the TPR better is the model. The range of TPR is always between 0–1.</p><h3><strong>False Positive Ratio (FPR)</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OYXNKiVnLBBf9LlxhaaZJA.png" /><figcaption>False Positive Ratio</figcaption></figure><p>The false Positive Ratio is to measure the performance of class 0. The lower the FPR better is the model. The range of FPR is always between 0–1.</p><h3><strong>Precision:</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f9ZpDGey_LPac53DM-FFlQ.png" /><figcaption>Precision Score</figcaption></figure><p>Precision is the measure of positive class in the given model. The higher the precision better the model. When you want to target your customer, want to run a marketing or sales campaign. Give more preference to precision. The range of Precision is always between 0–1.</p><h3><strong>Area Under Curve Receiving operating Curve (AUC— ROC):</strong></h3><p>AUC — ROC curve is a performance measurement for classification problems at various threshold settings. ROC is a probability curve and AUC represents the degree or measure of separability. It tells how much the model is capable of distinguishing between classes. Higher the AUC, the better the model is at predicting 0 classes as 0 and 1 classes as 1. By analogy, the Higher the AUC, the better the model is at distinguishing between patients with the disease and no disease.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KaNonjKlLiyYVI_xfUfQdw.png" /><figcaption>AUC-ROC Curve</figcaption></figure><h3><strong>F1 Ratio:</strong></h3><p>The <strong>F1-score</strong> combines the precision and recall of a classifier into a single metric by taking their harmonic mean. It is primarily used to compare the performance of two classifiers. Suppose that classifier A has a higher recall, and classifier B has higher precision. In this case, the F1-scores for both classifiers can be used to determine which one produces better results.</p><p>The F1 ratio is also known as the harmonic mean. Between precision &amp; Recall (TPR).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fBnw3GZ0UcZGxqJMf9QcFg.png" /><figcaption>F1 Score (Harmonic Mean Score)</figcaption></figure><h3><strong>Class Imbalance:</strong></h3><p>Class imbalance is a problem where the representation of one class is more than another class.</p><p>When your model/ data has a class imbalance, the accuracy of the model may be very high, But the model will fail for under-represented classes.</p><p>We desire a model which works balanced for the classes.</p><h3><strong>Solution:</strong></h3><p><strong>Oversampling: </strong>We increase the records of the class or the category which was under-represented has to be done after sampling and only on train data. In oversampling, we can create duplicates, and add them to the data.</p><p>Over-sampling should not be performed on test data. Because it will give the same output over again. and there is no advantage to it.</p><p><strong>Undersampling: </strong>We increase the records of the class or the category which was over-represented has to be done after sampling and only on train data.</p><p>Remove the records which have more data. Under-sampling should be performed on train data.</p><p><strong>Logistic Regression Formula:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jf15oLNUE6AwThW8-n-dHg.png" /><figcaption>Logistic Regression Formula’s</figcaption></figure><p>When the value is more than 0.5 considered it in class 1 and when the dependent value is less than 0.5 considered it in class 0. Considering the log value of the fraction number helps in finding the class of the output.</p><h3><strong>Assumptions:</strong></h3><ol><li>The dependant variable is binary.</li><li>The observations are independent.</li><li>There is no multicollinearity among exploratory varibles.</li><li>There are no extreme outliers.</li><li>The sample size should be sufficiently large.</li></ol><h3><strong>Drawbacks:</strong></h3><ol><li>If the number of observations is lesser than the number of features, Logistic Regression should not be used.</li><li>It can only be used to predict discrete functions.</li><li>Non-linear problems can’t be solved with logistic regression because it has a linear decision surface.</li><li>It is tough to obtain complex relationships using logistic regression.</li><li>Logistic Regression needs that independent variables are linearly related to the log odds (log(p/(1-p)).</li></ol><h3>Conclusion</h3><p>If you find any difficulty in following the theory part, mention them in the comment section.</p><p><em>Thank you for reading! Let me know in a comment or on </em><a href="https://www.linkedin.com/in/kvishwas98"><em>linkedin</em></a><em> if you felt like this did or didn’t help. I’ve got a few more articles that I’m writing and will be posting them every couple of weeks. They are mostly accounts from my project experience. If there are any other questions or anything else you’d like to hear about, please don’t hesitate to put in a request.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7daceb6c84e" width="1" height="1" alt=""><hr><p><a href="https://towardsdev.com/logistic-regression-b7daceb6c84e">Logistic Regression 1/2</a> was originally published in <a href="https://towardsdev.com/">Towards Dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>