<?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 Injemamul Irshad on Medium]]></title>
        <description><![CDATA[Stories by Injemamul Irshad on Medium]]></description>
        <link>https://medium.com/@injemam?source=rss-7f11375f784e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*rw-xUlpkUpDQHfjUK_Juyw.jpeg</url>
            <title>Stories by Injemamul Irshad on Medium</title>
            <link>https://medium.com/@injemam?source=rss-7f11375f784e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 16:19:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@injemam/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[Introduction to Chi-Squared Test for Machine Learning]]></title>
            <link>https://injemam.medium.com/introduction-to-chi-squared-test-for-machine-learning-2875d661d606?source=rss-7f11375f784e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2875d661d606</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[chi-techniques]]></category>
            <dc:creator><![CDATA[Injemamul Irshad]]></dc:creator>
            <pubDate>Sat, 30 Jan 2021 18:22:31 GMT</pubDate>
            <atom:updated>2021-01-30T18:22:31.695Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PiCxYP2S8wv5qC6E" /><figcaption>Photo by <a href="https://unsplash.com/@claybanks?utm_source=medium&amp;utm_medium=referral">Clay Banks</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>A general problem in applied machine learning is discovering whether input features are important to the outcome to be predicted.<br><strong>This is the problem of feature selection.</strong></p><p>In classification problems where input variables also are categorical, we will use statistical tests to work out whether the output variable depends or independent of the input variables. If independent, then the input variable is a candidate for a feature that may be inappropriate to the problem and removed from the dataset.</p><p>Pearson’s chi-squared statistical hypothesis is an example of a test for independence between categorical variables.</p><h4>Contingency Table</h4><p>A <strong>contingency table</strong>, sometimes called a two-way frequency <strong>table</strong>, is a tabular mechanism with at least two rows and two columns used in <strong>statistics</strong> to present categorical data in terms of frequency counts.</p><p>For example, the Sex=rows and Interest=columns table with contrived counts might look as follows:</p><pre>      Science, Math, Art<br>Male     20,    30,  15<br>Female   20,    15,  30</pre><p>The degrees of freedom for the chi-squared distribution is calculated based on the size of the contingency table as:</p><p><strong>degrees of freedom:</strong> (rows — 1) * (cols — 1)</p><p>In terms of a p-value and a chosen significance level (alpha), the test can be interpreted as follows:</p><p>If <strong>p-value</strong> <strong>&lt;= </strong>alpha: significant result, reject null hypothesis (H0), dependent.</p><p>If <strong>p-value</strong> <strong>&gt;</strong> alpha: not significant result, fail to reject the null hypothesis (H0), independent.</p><p>The Pearson’s chi-squared test for independence can be calculated in Python using the chi2_contingency() SciPy function.</p><p>The function takes an array as input representing the contingency table for the two categorical variables. It returns the calculated statistic and <strong>p-value</strong> for interpretation as well as the calculated <strong>degrees of freedom</strong> and table of expected frequencies.</p><pre>stat, p, dof, expected = chi2_contingency(table)</pre><p>We can interpret the statistic by retrieving the critical value from the chi-squared distribution for the probability and number of degrees of freedom.</p><p><strong>For example, </strong><br>a probability of 95% can be used, suggesting that the finding of the test is quite likely given the assumption of the test that the variable is independent. If the statistic is less than or equal to the critical value, we can fail to reject this assumption, otherwise, it can be rejected.</p><pre># interpret test-statistic<br>prob = 0.95<br>critical = chi2.ppf(prob, dof)<br>if abs(stat) &gt;= critical:<br>print(&#39;Dependent (reject H0)&#39;)<br>else:<br>print(&#39;Independent (fail to reject H0)&#39;)</pre><p>We can also interpret the p-value by comparing it to a chosen significance level, which would be 5%, calculated by inverting the 95% probability used in the critical value interpretation.</p><pre># interpret p-value<br>alpha = 1.0 - prob<br>if p &lt;= alpha:<br>print(&#39;Dependent (reject H0)&#39;)<br>else:<br>print(&#39;Independent (fail to reject H0)&#39;)</pre><p>We can tie all of this together and demonstrate the chi-squared significance test using a contrived contingency table.<br>A contingency table is defined below that has a different number of observations for each population (row), but a similar proportion across each group (column). Given the similar proportions, we would expect the test to find that the groups are similar and that the variables are independent (fail to reject the null hypothesis or H0).</p><pre>table = [ [10, 20, 30],<br>           [6, 9, 17]]</pre><p><strong>The complete example is listed below.</strong></p><pre># chi-squared test with similar proportions<br>from scipy.stats import chi2_contingency<br>from scipy.stats import chi2<br># contingency table<br>table = [ [10, 20, 30],<br>[6, 9, 17]]<br>print(table)<br>stat, p, dof, expected = chi2_contingency(table)<br>print(&#39;dof=%d&#39; % dof)<br>print(expected)</pre><pre># interpret test-statistic<br>prob = 0.95<br>critical = chi2.ppf(prob, dof)<br>print(&#39;probability=%.3f, critical=%.3f, stat=%.3f&#39; % (prob, critical, stat))<br>if abs(stat) &gt;= critical:<br>print(&#39;Dependent (reject H0)&#39;)<br>else:<br>print(&#39;Independent (fail to reject H0)&#39;)</pre><pre># interpret p-value<br>alpha = 1.0 - prob<br>print(&#39;significance=%.3f, p=%.3f&#39; % (alpha, p))<br>if p &lt;= alpha:<br>print(&#39;Dependent (reject H0)&#39;)<br>else:<br>print(&#39;Independent (fail to reject H0)&#39;)</pre><p>Running the example first prints the contingency table. The test is calculated and the degrees of freedom (dof) is reported as 2,<br>Next, the calculated expected frequency table is printed and we can see that indeed the observed contingency table does appear to match via an eyeball check of the numbers.<br>The critical value is calculated and interpreted, finding that indeed the variables are independent (fail to reject H0). The interpretation of the p-value makes the same finding.</p><pre>[[10, 20, 30], [6, 9, 17]]<br>dof=2<br>[[10.43478261 18.91304348 30.65217391]<br>[ 5.56521739 10.08695652 16.34782609]]<br>probability=0.950, critical=5.991, stat=0.272<br>Independent (fail to reject H0)<br>significance=0.050, p=0.873<br>Independent (fail to reject H0)</pre><p>Thankyou <a href="https://www.regexsoftware.com/">Regex software</a> for providing winter intership.</p><p>Hope you liked this post make sure to clap on this post.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2875d661d606" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Big Mart Sales Prediction Project Tutorial]]></title>
            <link>https://ai.plainenglish.io/big-mart-sales-prediction-project-tutorial-2de2414cdf2e?source=rss-7f11375f784e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2de2414cdf2e</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning-python]]></category>
            <category><![CDATA[machine-learning-projects]]></category>
            <category><![CDATA[data-visualization]]></category>
            <dc:creator><![CDATA[Injemamul Irshad]]></dc:creator>
            <pubDate>Sat, 23 Jan 2021 18:06:24 GMT</pubDate>
            <atom:updated>2022-03-28T17:04:49.652Z</atom:updated>
            <content:encoded><![CDATA[<h3>BigMart Sales Prediction Project</h3><blockquote>This post is the <strong>day 2</strong> post of my ‘<strong>10 days of machine learning projects’</strong> tutorial series. If you are new to my blog then you can check out Day 1 post <a href="https://medium.com/ai-in-plain-english/iris-flower-classification-step-by-step-tutorial-c8728300dc9e"><strong>here</strong></a>.<br>Without wasting any time let’s dive into our today’s tutorial.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hlExdQjk0oJAGoOe" /><figcaption>Photo by <a href="https://unsplash.com/@frenchieeye?utm_source=medium&amp;utm_medium=referral">Jon Cellier</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4><strong>DataSet</strong></h4><p>Download the dataset from <a href="https://www.kaggle.com/brijbhushannanda1979/bigmart-sales-data"><strong>here</strong></a> : <a href="https://www.kaggle.com/brijbhushannanda1979/bigmart-sales-data">https://www.kaggle.com/brijbhushannanda1979/bigmart-sales-data</a></p><h4><strong>Hypotheses Generation</strong></h4><p>It is always a good idea to generate some hypotheses before proceeding to any data science project.</p><p>We can separate this process into four levels: <strong>Product level, Store level, Customer level, and Macro level.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*GjavGQk-LHR0LNZYnKVEfw.png" /><figcaption>Source: Analytics Vidhya</figcaption></figure><p><strong>Product level hypotheses</strong>:</p><ol><li><strong>Brand</strong>: Branded products have more trust of the customers so they should have high sales.</li><li><strong>Visibility in Store</strong>: The location of the product placement also depends on the sales.</li><li><strong>Display Area</strong>: Products that are placed at an attention-catching place should have more sales.</li><li><strong>Utility</strong>: Daily use products have a higher tendency to sell compared to other products.</li><li><strong>Packaging</strong>: Quality packaging can attract customers and sell more.</li></ol><p><strong>Store Level Hypotheses:</strong></p><ol><li><strong>City type</strong>: Stores located in urban cities should have higher sales.</li><li><strong>Store Capacity</strong>: One-stop shops are big in size so their sell should be high.</li><li><strong>Population density</strong>: Densely populated areas have high demands so the store located in these areas should have higher sales.</li><li><strong>Marketing</strong>: Stores having a good marketing division can attract customers through the right offers.</li></ol><p><em>You can generate more hypotheses, these are some hypotheses that I think should be.</em></p><h4>Take a look at the Data Structure</h4><pre>import pandas as pd<br>import numpy as np<br>import matplotlib.pyplot as plt<br>import seaborn as sns<br>%matplotlib inline<br>import warnings # Ignores any warning<br>warnings.filterwarnings(&quot;ignore&quot;)<br><br>train = pd.read_csv(&quot;/content/bigmart-sales-data/Train.csv&quot;) <br>test = pd.read_csv(&quot;/content/bigmart-sales-data/Test.csv&quot;)<br><br>train.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*jB_H3ICvzyFwh3Z63DvJjQ.png" /></figure><pre>train.<strong>info</strong>()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/358/1*5NxfxaFnn9BmnCdNRUtDrA.png" /></figure><pre>train.<strong>describe</strong>()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*rEMrooXK1OutaOyzpELAwQ.png" /></figure><h4><strong>Overview of what we are going to cover:</strong></h4><ol><li><strong>Exploratory data analysis (EDA)</strong></li><li><strong>Data Pre-processing</strong></li><li><strong>Feature engineering</strong></li><li>Feature Transformation</li><li>Modeling</li><li>Hyperparameter tuning</li></ol><h3>BigMart Sales Prediction — Part 1</h3><h3>Exploratory data analysis (EDA)</h3><p>We have made our hypotheses and now we are ready to do some data exploration and come up with some inference.</p><p>The goal for the EDA is to get some insight and if any irregularities are found we will correct that in the next section, <strong>Data Pre-Processing.</strong></p><pre># Check for duplicates<br>idsTotal = train.shape[0]<br>idsDupli = train[train[&#39;Item_Identifier&#39;].duplicated()]</pre><pre>print(f&#39;There are {len(idsDupli)} duplicate IDs for {idsTotal} total entries&#39;)</pre><p>Output:</p><pre>There are 6964 duplicate IDs for 8523 total entries</pre><p>This shows that our Item_Identifier has some duplicate values. since a product can exist in more than one store it is expected for this repetition.</p><h4>1.1. Univariate Analysis</h4><p>In Univariate analysis we will explore each variable in a dataset.</p><p><strong>1.1.1. Distribution of the target variable: </strong><strong>Item_Outlet_Sales</strong></p><pre>plt.figure(figsize=(12,7))<br>sns.distplot(train.Item_Outlet_Sales, bins = 25)<br>plt.xlabel(&quot;Item_Outlet_Sales&quot;)<br>plt.ylabel(&quot;Number of Sales&quot;)<br>plt.title(&quot;Item_Outlet_Sales Distribution&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*CF8F0U1qeBmbU50_dIx-hg.png" /></figure><pre>print (&quot;Skew is:&quot;, train.Item_Outlet_Sales.skew())<br>print(&quot;Kurtosis: %f&quot; % train.Item_Outlet_Sales.kurt())</pre><p>Output:</p><pre>Skew is: 1.1775306028542798<br>Kurtosis: 1.615877</pre><p>We can see that our target variable is skewed towards the right. Therefore, we have to normalize it.</p><p><strong>1.1.2. Numerical Predictors</strong></p><p>Now we will consider our dependent variables. First of all, we will check for the numerical variables in our dataset:</p><pre>num_features = train.select_dtypes(include=[np.number])<br>num_features.dtypes</pre><p>Output:</p><pre>Item_Weight                  float64<br>Item_Visibility              float64<br>Item_MRP                     float64<br>Outlet_Establishment_Year      int64<br>Item_Outlet_Sales            float64<br>dtype: object</pre><p>We can see that out of 12 we have only 5 numeric variables.</p><p><strong>1.1.2.1. Correlation between Numerical Predictors and Target variable</strong></p><p>Now let’s check the correlation between our dependent variables and target variable:</p><pre>corr=num_features.corr()<br>corr</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/963/1*i7ndjab2Y1SuqqcIBoDYdA.png" /></figure><pre>corr[&#39;Item_Outlet_Sales&#39;].sort_values(ascending=False)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*vpHyObErjHp1qIFRnU1b9Q.png" /></figure><p>From the above result, we can see that Item_MRP have the most positive correlation and the Item_Visibility have the lowest correlation with our target variable. It is totally different from our initial hypotheses, this variables was expected to have high impact in the sales increase. Nevertheless, since this is not an expected behaviour and we should investigate.</p><p><strong>1.1.3. Categorical Predictors</strong></p><p>Now lets do some analysis on categorical variable and look at the variables that contain some insight on the hypotheses that we previously made.</p><p><strong>1.1.3.1. Distribution of the variable </strong><strong>Item_Fat_Content</strong></p><pre>sns.<strong>countplot</strong>(train.Item_Fat_Content)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*DT63goGIS16DpY3NREwN8Q.png" /></figure><p>For Item_Fat_Content there are two possible type “Low Fat” or “Regular”. However, in our data it is written in different manner. We will Correct this.</p><p><strong>1.1.3.2. Distribution of the variable </strong><strong>Item_Type</strong></p><pre>sns.countplot(train.Item_Type)<br>plt.xticks(rotation=90)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*pULn5S1ir4f3kzVOgbi9qw.png" /></figure><p>forItem_Type we have 16 different types of unique values and it is high number for categorical variable. Therefore we must try to reduce it.</p><p><strong>1.1.3.3. Distribution of the variable </strong><strong>Outlet_Size</strong></p><pre>sns.countplot(train.Outlet_Size)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*I18RM1AJ8_ua507_CnvPIg.png" /></figure><p>There seems to be less number of stores with size equals to “High”. It will be very interesting to see how this variable relates to our target.</p><p><strong>1.1.3.4. Distribution of the variable </strong><strong>Outlet_Location_Type</strong></p><pre>sns.countplot(train.Outlet_Location_Type)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/1*CIoNCD_vPM3zRgle6Uz04w.png" /></figure><p>From the above graph we can see that Bigmart is a brand of medium and small size city compare to densely populated area.</p><p><strong>1.1.3.5. Distribution of the variable </strong><strong>Outlet_Type</strong></p><pre>sns.countplot(train.Outlet_Type)<br>plt.xticks(rotation=90)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*45LWMYZhuhkneUhWFia2vQ.png" /></figure><p>There seems like Supermarket Type2 , Grocery Store and Supermarket Type3 all have low numbers of stores, we can create a single category with all of three, but before doing this we must see their impact on target variable.</p><h4>1.2. Bivariate Analysis</h4><p>Now it time to see the relationship between our target variable and predictors.</p><p><strong>1.2.1. Numerical Variables</strong></p><p><strong>1.2.1.1. </strong><strong>Item_Weight and </strong><strong>Item_Outlet_Sales analysis</strong></p><pre>plt.figure(figsize=(12,7))<br>plt.xlabel(&quot;Item_Weight&quot;)<br>plt.ylabel(&quot;Item_Outlet_Sales&quot;)<br>plt.title(&quot;Item_Weight and Item_Outlet_Sales Analysis&quot;)<br>plt.plot(train.Item_Weight, train[&quot;Item_Outlet_Sales&quot;],&#39;.&#39;, alpha = 0.3)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*dM5dOT0LrjPZkS_flMtNsQ.png" /></figure><p>We saw previously that Item_Weight had a low correlation with our target variable. This plot shows there relation.</p><p><strong>1.2.1.2. </strong><strong>Item_Visibility and </strong><strong>Item_Outlet_Sales analysis</strong></p><pre>plt.figure(figsize=(12,7))<br>plt.xlabel(&quot;Item_Visibility&quot;)<br>plt.ylabel(&quot;Item_Outlet_Sales&quot;)<br>plt.title(&quot;Item_Visibility and Item_Outlet_Sales Analysis&quot;)<br>plt.plot(train.Item_Visibility, train[&quot;Item_Outlet_Sales&quot;],&#39;.&#39;, alpha = 0.3)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*S1WkGRwc1FBotPyLQT6Ftg.png" /></figure><p><strong>1.2.1.3. </strong><strong>Outlet_Establishment_Year and </strong><strong>Item_Outlet_Sales analysis</strong></p><pre>Outlet_Establishment_Year_pivot = train.pivot_table(index=&#39;Outlet_Establishment_Year&#39;, values=&quot;Item_Outlet_Sales&quot;, aggfunc=np.median)<br>Outlet_Establishment_Year_pivot.plot(kind=&#39;bar&#39;, color=&#39;blue&#39;,figsize=(12,7))<br><br>plt.xlabel(&quot;Outlet_Establishment_Year&quot;)<br>plt.ylabel(&quot;Sqrt Item_Outlet_Sales&quot;)<br>plt.title(&quot;Impact of Outlet_Establishment_Year on Item_Outlet_Sales&quot;)<br>plt.xticks(rotation=0)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/847/1*mYfsYBr4HbeseXdYH6BFeQ.png" /></figure><p>There seems to be no appreciable meaning between the year of store establishment and the sales for the items.</p><p><strong>3.2.2. Categorical Variables</strong></p><p><strong>1.2.2.1. Impact of </strong><strong>Item_Fat_Content on</strong><strong>Item_Outlet_Sales</strong></p><pre>Item_Fat_Content_pivot = train.pivot_table(index=&#39;Item_Fat_Content&#39;, values=&quot;Item_Outlet_Sales&quot;, aggfunc=np.median)<br>Item_Fat_Content_pivot.plot(kind=&#39;bar&#39;, color=&#39;blue&#39;,figsize=(12,7))<br><br>plt.xlabel(&quot;Item_Fat_Content&quot;)<br>plt.ylabel(&quot;Item_Outlet_Sales&quot;)<br>plt.title(&quot;Impact of Item_Fat_Content on Item_Outlet_Sales&quot;)<br>plt.xticks(rotation=0)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*N6EwGHM7MMqWbqbmkdsQgw.png" /></figure><p>Low Fat products seem to higher sales than the Regular products</p><p><strong>1.2.2.2. Impact of Outlet_Identifier on Item_Outlet_Sales</strong></p><pre>Outlet_Identifier_pivot = train.pivot_table(index=&#39;Outlet_Identifier&#39;, values=&quot;Item_Outlet_Sales&quot;, aggfunc=np.median)<br>Outlet_Identifier_pivot.plot(kind=&#39;bar&#39;, color=&#39;blue&#39;,figsize=(12,7))<br><br>plt.xlabel(&quot;Outlet_Identifier&quot;)<br>plt.ylabel(&quot;Item_Outlet_Sales&quot;)<br>plt.title(&quot;Impact of Outlet_Identifier on Item_Outlet_Sales&quot;)<br>plt.xticks(rotation=0)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/829/1*G6yMheLbzAJIeFYBEqQcVg.png" /></figure><p>Out of 10- There are 2 Groceries strore, 6 Supermarket Type1, 1Supermarket Type2, and 1 Supermarket Type3. You can see from the below pivot table.</p><pre>train.pivot_table(values=&#39;Outlet_Type&#39;,<br>                  columns=&#39;Outlet_Identifier&#39;,<br>                  aggfunc=lambda x:x.mode())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uQ846USIktRigwV7FT_z6Q.png" /></figure><pre>train.pivot_table(values=&#39;Outlet_Type&#39;,<br>                  columns=&#39;Outlet_Size&#39;,<br>                  aggfunc=lambda x:x.mode())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*Ev6jxAu1ftNJALjK9OcLbA.png" /></figure><p>Most of the stores are of Supermarket Type1 of size High and they do not have best results. whereas Supermarket Type3 (OUT027) is a Medium size store and have best results.</p><p><strong>1.2.2.3. Impact of </strong><strong>Outlet_Size on </strong><strong>Item_Outlet_Sales</strong></p><pre>Outlet_Size_pivot = train.pivot_table(index=&#39;Outlet_Size&#39;, values=&quot;Item_Outlet_Sales&quot;, aggfunc=np.median)<br>Outlet_Size_pivot.plot(kind=&#39;bar&#39;, color=&#39;blue&#39;,figsize=(12,7))<br><br>plt.xlabel(&quot;Outlet_Size&quot;)<br>plt.ylabel(&quot;Item_Outlet_Sales&quot;)<br>plt.title(&quot;Impact of Outlet_Size on Item_Outlet_Sales&quot;)<br>plt.xticks(rotation=0)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*Rv4OMBkeF1SO1w-ydTxTCQ.png" /></figure><p><strong>1.2.2.4. Impact of </strong><strong>Outlet_Type on </strong><strong>Item_Outlet_Sales</strong></p><pre>Outlet_Type_pivot = train.pivot_table(index=&#39;Outlet_Type&#39;, values=&quot;Item_Outlet_Sales&quot;, aggfunc=np.median)<br>Outlet_Type_pivot.plot(kind=&#39;bar&#39;, color=&#39;blue&#39;,figsize=(12,7))<br><br>plt.xlabel(&quot;Outlet_Type&quot;)<br>plt.ylabel(&quot;Item_Outlet_Sales&quot;)<br>plt.title(&quot;Impact of Outlet_Size on Item_Outlet_Sales&quot;)<br>plt.xticks(rotation=0)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*4mjRlAs6Hdo7vCasLE80wA.png" /></figure><p>It could be a good idea to create a new feature that shows the sales ratio according to the store size.</p><p><strong>1.2.2.5. Impact of </strong><strong>Outlet_Location_Type on </strong><strong>Item_Outlet_Sales</strong></p><pre>Outlet_Location_Type_pivot = train.pivot_table(index=&#39;Outlet_Location_Type&#39;, values=&quot;Item_Outlet_Sales&quot;, aggfunc=np.median)<br>Outlet_Location_Type_pivot.plot(kind=&#39;bar&#39;, color=&#39;blue&#39;,figsize=(12,7))<br><br>plt.xlabel(&quot;Outlet_Location_Type&quot;)<br>plt.ylabel(&quot;Item_Outlet_Sales&quot;)<br>plt.title(&quot;Impact of Outlet_Location_Type on Item_Outlet_Sales&quot;)<br>plt.xticks(rotation=0)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*UkB_hDyVYe37JdrKt9ofjw.png" /></figure><p>This shows that our hypotheses was totaly different from the result that we got from the above plot. Tier 2 cities have the higher sales than the Tier 1 and Tier 2.</p><pre>train.pivot_table(values=&#39;Outlet_Location_Type&#39;,<br>                  columns=&#39;Outlet_Type&#39;,<br>                  aggfunc=lambda x:x.mode())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*TI4kh_s5bODx7qPve1rxhg.png" /></figure><h3>2. Data Pre-Processing</h3><p>During our EDA we were able to take some Insights regarding our first hypotheses and the available data.</p><p><strong>2. 1. Looking for missing values</strong></p><p>We have two datasets the first one train.csv and the second is test.csv. Let’s combine them into a dataframe data with a source column specifying where each observation belongs, so that we don’t have to do pre-processing separately.</p><pre># Join Train and Test Dataset<br><br>#Create source column to later separate the data easily<br>train[&#39;source&#39;]=&#39;train&#39;<br>test[&#39;source&#39;]=&#39;test&#39;<br>data = pd.concat([train,test], ignore_index = True)<br>print(train.shape, test.shape, data.shape)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/521/1*mBBwtEZbx5mUNrrzwz-eeg.png" /></figure><p>Now checking for the percentage of missing values present in our data:</p><pre>data.isnull().sum()/data.shape[0]*100 <br>#show values in percentage</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*0_60c2dSurg0RIbfAY05xw.png" /></figure><p><strong>Note that</strong> <em>Item_Outlet_Sales</em> is the target variable and contains missing values because our test data does not have the <em>Item_Outlet_Sales</em> column.<br>Nevertheless, we’ll impute the missing values in <em>Item_Weight </em>and <em>Outlet_Size</em>.</p><p><strong>2.2. Imputing Missing Values</strong>In our EDA section, we have seen that the Item_Weight and the Outlet_Size had missing values.</p><p>In our EDA section, we have seen that the <em>Item_Weight</em> and the <em>Outlet_Size</em> had missing values.</p><p><strong>2.2.1. Imputing the </strong><strong>mean for </strong><strong>Item_Weight missing values</strong></p><pre>item_avg_weight = data.pivot_table(values=&#39;Item_Weight&#39;, index=&#39;Item_Identifier&#39;)<br>print(item_avg_weight)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/369/1*-dBUuXi7V_pnxSAiDr-ybg.png" /></figure><pre>def impute_weight(cols):<br>  Weight = cols[0]<br>  Identifier = cols[1]<br><br>  if pd.isnull(Weight):<br>    return item_avg_weight[&#39;Item_Weight&#39;][item_avg_weight.index == Identifier]<br><br>  else:<br>    return Weight<br><br>print(&#39;Original #missing: %d&#39;%sum(data[&#39;Item_Weight&#39;].isnull()))<br><br>data[&#39;Item_Weight&#39;] = data[[&#39;Item_Weight&#39;, &#39;Item_Identifier&#39;]].apply(impute_weight,axis=1).astype(float)<br><br>print(&#39;Final #missing: %d&#39;%sum(data[&#39;Item_Weight&#39;].isnull()))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*KKaTXnxRfRG7UbsE_QSm6A.png" /></figure><p><strong>2.2.2. Imputing </strong><strong>Outlet_Size missing values with the </strong><strong>mode</strong></p><pre>#Import mode function:<br>from scipy.stats import mode </pre><pre>#Determing the mode for each<br>outlet_size_mode = data.pivot_table(values=&#39;Outlet_Size&#39;, columns=&#39;Outlet_Type&#39;,aggfunc=lambda x:x.mode())<br><br>outlet_size_mode</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/805/1*sabZt0JI7P709kR7A_n9gw.png" /></figure><pre>def impute_size_mode(cols):<br>    Size = cols[0]<br>    Type = cols[1]<br><br>    if pd.isnull(Size):<br>        return outlet_size_mode.loc[&#39;Outlet_Size&#39;]      [outlet_size_mode.columns == Type][0]</pre><pre>    else:<br>        return Size<br><br>print (&#39;Orignal #missing: %d&#39;%sum(data[&#39;Outlet_Size&#39;].isnull()))<br><br>data[&#39;Outlet_Size&#39;] = data[[&#39;Outlet_Size&#39;,&#39;Outlet_Type&#39;]].apply(impute_size_mode,axis=1)<br><br>print (&#39;Final #missing: %d&#39;%sum(data[&#39;Outlet_Size&#39;].isnull()))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*XLCaYf_7HxW7eqnl7S08iA.png" /></figure><h3>3. Feature Engineering</h3><p><strong>3.1. Should we combine </strong><strong>Outlet_Type?</strong></p><pre>#Creates pivot table with Outlet_Type and the mean of <br>#Item_Outlet_Sales. Agg function is by default mean()<br><br>data.pivot_table(values=&#39;Item_Outlet_Sales&#39;, columns=&#39;Outlet_Type&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*dlHmyzNwhQEk6EVPswUjEg.png" /></figure><p>we are not going to combine because the average product sale are different.</p><p><strong>3.2. </strong><strong>Item_Visibility minimum value is 0</strong></p><p>In our EDA we observe that <strong>Item_Visibility</strong> had minimum value 0. so this make no sense, lets consider it as missing value and impute with its mean.</p><pre>#Determine average visibility of a product</pre><pre>visibility_avg = data.pivot_table(values=&#39;Item_Visibility&#39;, index=&#39;Item_Identifier&#39;)<br><br>#Impute 0 values with mean visibility of that product</pre><pre>missing_values = (data[&#39;Item_Visibility&#39;] == 0)<br><br>print (&#39;Number of 0 values initially: %d&#39;%sum(missing_values))<br><br>data.loc[missing_values,&#39;Item_Visibility&#39;] = data.loc[missing_values,&#39;Item_Identifier&#39;].apply(lambda x: visibility_avg.at[x, &#39;Item_Visibility&#39;])<br><br>print (&#39;Number of 0 values after modification: %d&#39;%sum(data[&#39;Item_Visibility&#39;] == 0))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*z6D1H00s5AvenRNhXUABjA.png" /></figure><p><strong>3.3. Determine the years of operation of a store</strong></p><p>Data we have is from 2013, so we will create a new feature calculating the age of outlets.</p><pre>#Remember the data is from 2013<br>data[&#39;Outlet_Years&#39;] = 2013 - data[&#39;Outlet_Establishment_Year&#39;]<br>data[&#39;Outlet_Years&#39;].describe()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*jSS9yG-kRHezYI0xRtTrUA.png" /></figure><p><strong>3.4. Create a broad category of </strong><strong>Item_Type</strong></p><p>Item_Type is having 16 unique categories which might to be very useful in our analysis. So it’s a good idea to combine them. Take a close look at Item_identifier each item starts with FD” (Food), “DR” (Drinks) or “NC” (Non-Consumables). We can group the items within these 3 categories.</p><pre>#Get the first two characters of ID:</pre><pre>data[&#39;Item_Type_Combined&#39;] = data[&#39;Item_Identifier&#39;].apply(lambda x: x[0:2])<br><br>#Rename them to more intuitive categories:</pre><pre>data[&#39;Item_Type_Combined&#39;] = data[&#39;Item_Type_Combined&#39;].map({&#39;FD&#39;:&#39;Food&#39;,&#39;NC&#39;:&#39;Non-Consumable&#39;,                                                            <br>              &#39;DR&#39;:&#39;Drinks&#39;})</pre><pre>data[&#39;Item_Type_Combined&#39;].value_counts()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*UqmhYRil9TwF5wF37uQA0w.png" /></figure><p><strong>3.5. Modify categories of </strong><strong>Item_Fat_Content</strong></p><p>Here we are correcting the Typos in categories of Item_Fat_Content variable.</p><pre>#Change the category of low fat</pre><pre>print(&#39;Original Categories:&#39;)<br>print(data[&#39;Item_Fat_Content&#39;].value_counts())<br><br>print(&#39;\nModified categories&#39;)<br>data[&#39;Item_Fat_Content&#39;] = data[&#39;Item_Fat_Content&#39;].replace({&#39;LF&#39;:&#39;Low Fat&#39;,<br>                                  &#39;low fat&#39;:&#39;Low Fat&#39;,<br>                                  &#39;reg&#39;:&#39;Regular&#39;})<br><br>print(data[&#39;Item_Fat_Content&#39;].value_counts())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/373/1*8C_lmQNRJB2ktfGdH-TFgQ.png" /></figure><p>Wait.. we have seen some non-consumables previously:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/287/1*QdBY3rJp_-E77WoqMuIwww.png" /></figure><p>and a fat-content should not be specified for them. we will create a separate category for such kind observations.</p><pre>#Mark non-consumables as separate category in low_fat:<br><br>data.loc[data[&#39;Item_Type_Combined&#39;] == <br>&quot;Non-Consumable&quot;, &quot;Item_Fat_Content&quot;] = &quot;Non-Edible&quot;</pre><pre>data[&#39;Item_Fat_Content&#39;].value_counts()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*MaL7V_3qEVlhJx3PYx6yIg.png" /></figure><p>BigMart Sales Prediction step by step tutorial Part 1 ends here…. <br>It already became a very long post. So, I will not waste your time writing any useless thing. And don’t forget to check Part 2 of this post.</p><p><strong><em>Don’t forget, if you like it, please give it an applause!</em></strong></p><p><a href="https://www.buymeacoffee.com/Inje">https://www.buymeacoffee.com/Inje</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/78/1*BqnV7zgz5AJWoI_S7L3oOw.gif" /></figure><h3>BigMart Sales Prediction part 2</h3><p><strong>Tommorow We Will cover:<br></strong>4. Feature Transformation<br>5. Modeling<br>6. Hyperparameter tuning</p><p>Connect with me on LinkedIn: <a href="https://www.linkedin.com/in/injemamul/"><strong><em>Md Injemamul Irshad</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2de2414cdf2e" width="1" height="1" alt=""><hr><p><a href="https://ai.plainenglish.io/big-mart-sales-prediction-project-tutorial-2de2414cdf2e">Big Mart Sales Prediction Project Tutorial</a> was originally published in <a href="https://ai.plainenglish.io/">Artificial Intelligence in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Iris Flower Classification Step-by-Step Tutorial]]></title>
            <link>https://ai.plainenglish.io/iris-flower-classification-step-by-step-tutorial-c8728300dc9e?source=rss-7f11375f784e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8728300dc9e</guid>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[machine-learning-tutorial]]></category>
            <category><![CDATA[machine-learning-python]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Injemamul Irshad]]></dc:creator>
            <pubDate>Thu, 21 Jan 2021 15:47:50 GMT</pubDate>
            <atom:updated>2021-01-23T10:24:25.851Z</atom:updated>
            <content:encoded><![CDATA[<h4>Do you want to learn machine learning, but having trouble getting started?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ybj7FQ2tXSwT8FHJ" /><figcaption>Photo by <a href="https://unsplash.com/@markuswinkler?utm_source=medium&amp;utm_medium=referral">Markus Winkler</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This is my first post and this post is for an absolute beginner. If you are stuck somewhere in this tutorial then don’t worry about that. This post is just for you to make you familiar with the machine learning process, In the upcoming series of posts, we will discuss in-depth about the concepts.</p><p>In this post, you will make your first machine learning project (<strong>step-by-step</strong>) in python.</p><p><strong><em>Overview of what we are going to cover:<br> </em></strong>1. Setting up the Environment.<br> 2. Loading the dataset.<br> 3. Summarizing the data.<br> 4. Data visualization.<br> 5. Model Building- part 1.<br> 6. Model Building- part 2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1XBBBoZZ-0LKMYgh" /><figcaption>Photo by <a href="https://unsplash.com/@askkell?utm_source=medium&amp;utm_medium=referral">Andy Kelly</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>This post is</em><strong><em> 1 day </em></strong><em>of the “</em><strong><em> 10 days of machine learning project </em></strong><em>” post series.</em><strong><em><br>1–3 days</em></strong><em> — Beginners project tutorials<br></em><strong><em>4–6 days</em></strong><em> — Intermediate project tutorials<br></em><strong><em>7–10 days</em></strong><em> — Advanced project tutorials</em></p><h3>Machine Learning in Python: Step-By-Step Tutorial<br> (start here)</h3><p>It is called a hello world program of machine learning and it’s a classification problem where we will predict the flower class based on its petal length, petal width, sepal length, and sepal width.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*nfK3vGZkTa4GrO7yWpcS-Q.png" /></figure><h4>1. Setting up the Environment:</h4><p>In this tutorial we are going to use Google Colab, hope you guys are familiar with Google Colab. <br>Google Colab provides a jupyter notebook to run your code without installing any software or libraries in your local machine.</p><p><strong>1.1 </strong>Search ‘<strong>google colab</strong>’ in your browser or <a href="https://colab.research.google.com/notebooks/intro.ipynb#recent=true"><em>CLICK HERE</em></a> to go to the colab website:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*symagIGRVO9a7zgelAbBtg.png" /><figcaption>Create a new Notebook written in blue color</figcaption></figure><p><strong>1.2.</strong> Click on <strong><em>New Notebook</em></strong> , to create a new notebook in <a href="https://colab.research.google.com/notebooks/intro.ipynb#recent=true"><em>google colab</em></a> where <br>we will write our code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JJeuPPhVq3OKBhvKvWR6LQ.png" /><figcaption>Press Shift+Enter to run the cell in notebook</figcaption></figure><h4>2. Loading the dataset:</h4><p>First of all we will import some <strong>libraries </strong>for analysis and model building:</p><pre># IMPORTING LIBRARIES</pre><pre>import numpy as np<br>import pandas as pd<br>import seaborn as sns<br>sns.set_palette(&#39;husl&#39;)<br>import matplotlib.pyplot as plt<br>%matplotlib inline</pre><pre>from sklearn.model_selection import train_test_split<br>from sklearn.model_selection import cross_val_score<br>from sklearn.model_selection import StratifiedKFold<br>from sklearn.metrics import classification_report<br>from sklearn.metrics import accuracy_score<br>from sklearn.linear_model import LogisticRegression<br>from sklearn.tree import DecisionTreeClassifier<br>from sklearn.neighbors import KNeighborsClassifier<br>from sklearn.discriminant_analysis import LinearDiscriminantAnalysis</pre><pre>from sklearn.naive_bayes import GaussianNB<br>from sklearn.svm import SVC</pre><blockquote><strong>Loading Iris data</strong></blockquote><p>This is the URL of the dataset, where the data of the iris flower is stored:<br>https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv</p><p>We will assign this link to “ url “ variable:</p><pre>url = &#39;https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv&#39;</pre><p>Creating the list of column name:</p><pre>col_name = [&#39;sepal-lenght&#39;,&#39;sepal-width&#39;,&#39;petal-lenght&#39;,&#39;petal-width&#39;,&#39;class&#39;]</pre><p>Pandas read_csv() is used for reading the csv file:</p><pre>dataset = pd.read_csv(url, names = col_name)</pre><h4>3. Summarize the Dataset</h4><p>Let’s check the shape of the data on which we have to work on:</p><pre>dataset.shape</pre><p>Output:</p><pre>(150, 5)</pre><p>This shows that we have 150 rows and 5 columns.</p><p>Now displaying the first 5 records of our dataset:</p><pre>dataset.head()</pre><p>Output:</p><pre>sepal-lenght sepal-width  petal-lenght petal-width       class<br>        5.1       3.5       1.4         0.2            Iris-setosa                 <br>        4.9       3.0       1.4         0.2            Iris-setosa                 <br>        4.7       3.2       1.3         0.2            Iris-setosa                 <br>        4.6       3.1       1.5         0.2            Iris-setosa                 <br>        5.0       3.6       1.4         0.2            Iris-setosa</pre><p>Pandas info() method prints information about a DataFrame including the index dtype and columns, non-null values and memory usage:</p><pre>dataset.info()</pre><p>Output:</p><pre>&lt;class &#39;pandas.core.frame.DataFrame&#39;&gt;<br>RangeIndex: 150 entries, 0 to 149<br>Data columns (total 5 columns):<br> #   Column        Non-Null Count  Dtype  <br>---  ------        --------------  -----  <br> 0   sepal-lenght  150 non-null    float64<br> 1   sepal-width   150 non-null    float64<br> 2   petal-lenght  150 non-null    float64<br> 3   petal-width   150 non-null    float64<br> 4   class         150 non-null    object <br>dtypes: float64(4), object(1)<br>memory usage: 6.0+ KB</pre><p>Pandas <strong>describe()</strong> is used to view some basic statistical details like percentile, mean, std etc. of a data frame or a series of numeric values:</p><pre>dataset.describe()</pre><p>Output:</p><pre>count  150.000000     150.000000     150.000000     150.000000                 <br>mean     5.843333       3.054000       3.758667       1.198667                 std       0.828066       0.433594       1.764420       0.763161                 min       4.300000       2.000000       1.000000       0.100000                 25%       5.100000       2.800000       1.600000       0.300000                 50%       5.800000       3.000000       4.350000       1.300000                 75%       6.400000       3.300000       5.100000       1.800000                 max       7.900000       4.400000       6.900000       2.500000</pre><p>We can see that numerical values are in range of 0 to 8.</p><p>Now let’s check the number of rows that belongs to each class:</p><pre>dataset[&#39;class&#39;].value_counts()</pre><p>Output:</p><pre>Iris-virginica     50<br>Iris-setosa        50<br>Iris-versicolor    50<br>Name: class, dtype: int64</pre><p>We can see that each class of flowers has 50 rows.</p><h4>4. Data Visualization</h4><p><strong>Violin plot</strong><br>Plotting the violin plot to check the comparison of a variable distribution:</p><pre>sns.violinplot(y=&#39;class&#39;, x=&#39;sepal-lenght&#39;, data=dataset, inner=&#39;quartile&#39;)<br>plt.show()<br>sns.violinplot(y=&#39;class&#39;, x=&#39;sepal-width&#39;, data=dataset, inner=&#39;quartile&#39;)<br>plt.show()<br>sns.violinplot(y=&#39;class&#39;, x=&#39;petal-lenght&#39;, data=dataset, inner=&#39;quartile&#39;)<br>plt.show()<br>sns.violinplot(y=&#39;class&#39;, x=&#39;petal-width&#39;, data=dataset, inner=&#39;quartile&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*2kYCB2xxO6UyWFV3HamqjA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*A6BUUwRln96olBU1CwLREQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*QQlruc-lmuApMvYDJefaiw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*OD71L_LNePMChknTqBrloQ.png" /></figure><p>The above-plotted violin plot says that Iris-Setosa class is having a smaller petal length and petal width compared to other class.</p><p><strong>Pair plot<br></strong>Plotting multiple pairwise bivariate distributions in a dataset using pairplot:</p><pre>sns.pairplot(dataset, hue=&#39;class&#39;, markers=&#39;+&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*2YLKFPkm3l-IgX55KEbStA.png" /></figure><p>From the above, we can see that Iris-Setosa is separated from both other species in all the features.</p><p><strong>Heatmap<br></strong>Plotting the heatmap to check the correlation. <br>dataset.corr() is used to find the pairwise correlation of all columns in the dataframe.</p><pre>plt.figure(figsize=(7,5))<br>sns.heatmap(dataset.corr(), annot=True, cmap=&#39;cubehelix_r&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*lBNkQ0ZNXbg_F_iIHUZDLQ.png" /></figure><h4>5. <strong>Model Building- part 1</strong></h4><p><strong>5.1 Splitting the dataset<br></strong>X is having all the dependent variables. <br>Y is having an independent variable (here in this case ‘class’ is an independent variable).</p><pre>X = dataset.drop([&#39;class&#39;], axis=1)<br>y = dataset[&#39;class&#39;]<br>print(f&#39;X shape: {X.shape} | y shape: {y.shape} &#39;)</pre><p>Output:</p><pre>X shape: (150, 4) | y shape: (150,)</pre><p>Here, we can see from the output that the X has 150 rows and 4 columns whereas Y has 150 rows and only one column.</p><p><strong>5.2 Train Test split<br></strong>Splitting our dataset into train and test using train_test_split(), what we are doing here is taking 80% of data to train our model, and 20% that we will hold back as a validation dataset:</p><pre>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=1)</pre><p><strong>5.3 Model Creation<br></strong>We don’t know which algorithms would be best for this problem.<br>Let’s check each algorithm in loop and print its accuracy, so that we can select our best algorithm.</p><p>Let’s test 6 different algorithms:</p><ul><li>Logistic Regression (LR)</li><li>Linear Discriminant Analysis (LDA)</li><li>K-Nearest Neighbors (KNN).</li><li>Classification and Regression Trees (CART).</li><li>Gaussian Naive Bayes (NB).</li><li>Support Vector Machines (SVM).</li></ul><p>I know at this point you are thinking that it is so complicated to understand the below code, but don’t worry about it. In the next step, I will explain how to train and check the accuracy of the model manually.</p><pre>models = []</pre><pre>models.append((&#39;LR&#39;, LogisticRegression()))</pre><pre>models.append((&#39;LDA&#39;, LinearDiscriminantAnalysis()))</pre><pre>models.append((&#39;KNN&#39;, KNeighborsClassifier()))</pre><pre>models.append((&#39;CART&#39;, DecisionTreeClassifier()))</pre><pre>models.append((&#39;NB&#39;, GaussianNB()))</pre><pre>models.append((&#39;SVC&#39;, SVC(gamma=&#39;auto&#39;)))</pre><pre># evaluate each model in turn</pre><pre>results = []</pre><pre>model_names = []</pre><pre>for name, model in models:</pre><pre>kfold = StratifiedKFold(n_splits=10, random_state=1, shuffle=True)</pre><pre>cv_results = cross_val_score(model, X_train, y_train, cv=kfold, scoring=&#39;accuracy&#39;)</pre><pre>results.append(cv_results)</pre><pre>model_names.append(name)</pre><pre>print(&#39;%s: %f (%f)&#39; % (name, cv_results.mean(), cv_results.std()))</pre><p>Output:</p><pre>LR: 0.966667 (0.040825)<br>LDA: 0.975000 (0.038188)<br>KNN: 0.958333 (0.041667)<br>CART: 0.950000 (0.055277)<br>NB: 0.950000 (0.055277)<br>SVC: 0.983333 (0.033333)</pre><p>Support Vector Classifier (SVC) is performing better than other algorithms.<br>Let’s train SVC model on our training set and predict on test set in the next step.</p><p><strong>6. Model Building- part 2<br></strong>6.1.<strong> </strong>We are defining our SVC model and passing gamma as auto. If you wanted to know more about parameter visit this <a href="https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html">link</a>.</p><p>6.2. After that fitting/training the model on X_train and Y_train using .fit() method.</p><p>6.3. Then we are predicting on X_test using .predict() method.</p><pre>model = SVC(gamma=&#39;auto&#39;)<br>model.fit(X_train, y_train)</pre><pre>prediction = model.predict(X_test)</pre><p>6.4. Now checking the accuracy of our model using <br><em>accuracy_score(y_test, prediction)</em><br>y_test: actual values of X_test<br>prediction: predicted values of X_test (refer to point 3).</p><p>6.5. Printing out the classification report using<br><em>classification_report(y_test, prediction).</em></p><pre>print(f&#39;Test Accuracy: {accuracy_score(y_test, prediction)}&#39;)<br>print(f&#39;Classification Report: \n {classification_report(y_test, prediction)}&#39;)</pre><p>Output:</p><pre>Accuracy: 0.9666666666666667<br>Classification Report: <br>                  precision    recall  f1-score   support</pre><pre>Iris-setosa           1.00      1.00      1.00        11<br>Iris-versicolor       1.00      0.92      0.96        13<br> Iris-virginica       0.86      1.00      0.92         6<br><br>       accuracy                           0.97        30<br>      macro avg       0.95      0.97      0.96        30<br>   weighted avg       0.97      0.97      0.97        30</pre><p>You can repeat this process with other algorithms to check the accuracy of model manually.</p><h3>Machine learning project tutorial for beginners</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*L2-9ZSi6m_mOFL1r" /><figcaption>Photo by <a href="https://unsplash.com/@eilisgarvey?utm_source=medium&amp;utm_medium=referral">Eilis Garvey</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>Congratulations!</strong> you just created you first machine learning project.</p><h4>What to do next ?</h4><p>As was mentioned earlier in this post that this is the<strong> 1 Day </strong>post of my <strong>10 days of machine learning post series</strong>. I will post beginner level to advanced level project tutorials, So follow this blog for more upcoming tutorials.</p><p>If you have any doubt regarding this tutorial please feel free to comment down your questions, I’m going to answer each and every comment you post in my blog.</p><p>Connect with me on Linkedin: <a href="https://www.linkedin.com/in/injemamul/">Md <strong>Injemam</strong>ul Irshad</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8728300dc9e" width="1" height="1" alt=""><hr><p><a href="https://ai.plainenglish.io/iris-flower-classification-step-by-step-tutorial-c8728300dc9e">Iris Flower Classification Step-by-Step Tutorial</a> was originally published in <a href="https://ai.plainenglish.io/">Artificial Intelligence in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>