{"id":9575,"date":"2022-11-29T20:36:15","date_gmt":"2022-11-29T11:36:15","guid":{"rendered":"https:\/\/www.stechstar.com\/user\/wordpress\/?p=9575"},"modified":"2022-11-29T20:36:15","modified_gmt":"2022-11-29T11:36:15","slug":"python%eb%a8%b8%ec%8b%a0%eb%9f%ac%eb%8b%9d-scikit-learn-tutorial-machine-learning-in-python","status":"publish","type":"post","link":"https:\/\/www.stechstar.com\/user\/wordpress\/python%eb%a8%b8%ec%8b%a0%eb%9f%ac%eb%8b%9d-scikit-learn-tutorial-machine-learning-in-python\/","title":{"rendered":"[python][\uba38\uc2e0\ub7ec\ub2dd] Scikit-learn Tutorial: Machine Learning in Python"},"content":{"rendered":"<h1>[python][\uba38\uc2e0\ub7ec\ub2dd] Scikit-learn Tutorial: Machine Learning in Python<\/h1>\n<h1 class=\"display-6 mt-0 pt-0 pb-2\">Scikit-learn Tutorial: Machine Learning in Python<\/h1>\n<div class=\"tve_shortcode_rendered\">\n<div class=\"kg-card-markdown\">\n<p>Scikit-learn is a free machine learning library for Python. It features various algorithms like support vector machine, random forests, and k-neighbours, and it also supports Python numerical and scientific libraries like&nbsp;<code>NumPy<\/code>&nbsp;and&nbsp;<code>SciPy<\/code>.<\/p>\n<p>In this tutorial we will learn to code python and apply Machine Learning with the help of the scikit-learn library, which was created to make doing machine learning in Python easier and more robust.<\/p>\n<p>To do this, we\u2019ll be using the&nbsp;<a href=\"https:\/\/www.ibm.com\/communities\/analytics\/watson-analytics-blog\/sales-win-loss-sample-dataset\/\" data-cke-saved-href=\"https:\/\/www.ibm.com\/communities\/analytics\/watson-analytics-blog\/sales-win-loss-sample-dataset\/\">Sales_Win_Loss data<\/a>&nbsp;set from IBM\u2019s Watson repository. We will import the data set using pandas, explore the data using pandas methods like&nbsp;<code>head()<\/code>,&nbsp;<code>tail()<\/code>,&nbsp;<code>dtypes()<\/code>, and then try our hand at using plotting techniques from&nbsp;<code>Seaborn<\/code>&nbsp;to visualize our data.<\/p>\n<p>Then we\u2019ll dive into scikit-learn and use&nbsp;<code>preprocessing.LabelEncoder()<\/code>&nbsp;in scikit-learn to process the data, and&nbsp;<code>train_test_split()<\/code>&nbsp;to split the data set into test and train samples. We will also use a cheat sheet to help us decide which algorithms to use for the data set. Finally we will use three different algorithms (Naive-Bayes, LinearSVC, K-Neighbors Classifier) to make predictions and compare their performance using methods like&nbsp;<code>accuracy_score()<\/code>&nbsp;provided by the scikit-learn library. We will also visualize the performance score of different models using scikit-learn and Yellowbrick visualization.<\/p>\n<p>To get the most out of this post, you should probably already be comfortable with:<\/p>\n<ul>\n<li>pandas fundamentals<\/li>\n<li>Seaborn and matplotlib basics<\/li>\n<\/ul>\n<p>If you need to brush up on these topics, check out these&nbsp;<a href=\"https:\/\/www.dataquest.io\/blog\/pandas-python-tutorial\/\" data-cke-saved-href=\"https:\/\/www.dataquest.io\/blog\/pandas-python-tutorial\/\">pandas<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/www.dataquest.io\/blog\/python-data-visualization-libraries\/\" data-cke-saved-href=\"https:\/\/www.dataquest.io\/blog\/python-data-visualization-libraries\/\">data visualization<\/a>&nbsp;blog posts.<\/p>\n<h2 id=\"thedataset\">The data set<\/h2>\n<p>For this tutorial, we will use the&nbsp;<a href=\"https:\/\/community.watsonanalytics.com\/wp-content\/uploads\/2015\/04\/WA_Fn-UseC_-Sales-Win-Loss.csv\" data-cke-saved-href=\"https:\/\/community.watsonanalytics.com\/wp-content\/uploads\/2015\/04\/WA_Fn-UseC_-Sales-Win-Loss.csv\">Sales-Win-Loss data set<\/a>&nbsp;available on the IBM Watson website. This data set contains the sales campaign data of an automotive parts wholesale supplier.<\/p>\n<p>We will use scikit-learn to build a predictive model to tell us which sales campaign will result in a loss and which will result in a win.<\/p>\n<p>Let\u2019s begin by importing the data set.<\/p>\n<h2 id=\"importingthedataset\">Importing the data set<\/h2>\n<p>First we will import the pandas module and use a variable&nbsp;<code>url<\/code>&nbsp;to store the url from which the data set is to be downloaded.<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\">#import necessary modules<\/span>\n<span class=\"token keyword\">import<\/span> pandas <span class=\"token keyword\">as<\/span> pd\n<span class=\"token comment\" spellcheck=\"true\">#store the url in a variable<\/span>\nurl <span class=\"token operator\">=<\/span> <span class=\"token string\">\"https:\/\/community.watsonanalytics.com\/wp-content\/uploads\/2015\/04\/WA_Fn-UseC_-Sales-Win-Loss.csv\"<\/span><\/code><\/pre>\n<p>Next, we will use the&nbsp;<code>read_csv()<\/code>&nbsp;method provided by the pandas module to read the&nbsp;<code>csv<\/code>&nbsp;file which contains comma separated values and convert that into a pandas DataFrame.<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\"># Read in the data with `read_csv()`<\/span>\nsales_data <span class=\"token operator\">=<\/span> pd<span class=\"token punctuation\">.<\/span>read_csv<span class=\"token punctuation\">(<\/span>url<span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<p>The code snippet above returns a variable&nbsp;<code>sales_data<\/code>&nbsp;where the dataframe is now stored.<\/p>\n<p>For those who are new to pandas, the&nbsp;<code>pd.read_csv()<\/code>&nbsp;method in the above code creates a tabular data-structure known as a&nbsp;<code>Dataframe<\/code>, where the first column contains the index which marks each row of data uniquely and the first row contains a label\/name for each column, which are the original column names retained from the data set. The&nbsp;<code>sales_data<\/code>&nbsp;variable in the above code snippet will have a structure similar to the diagram represented below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.stechstar.com\/user\/zbxe\/files\/attach\/images\/3263\/244\/089\/c7f9b950a3b420843dffaa952962c9a4.png\" alt=\"dataframe-1\" data-autoattach=\"success\" data-cke-saved-src=\".\/files\/attach\/images\/3263\/244\/089\/c7f9b950a3b420843dffaa952962c9a4.png\"><\/p>\n<p>Source:&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/25773245\/ambiguity-in-pandas-dataframe-numpy-array-axis-definition\" data-cke-saved-href=\"https:\/\/stackoverflow.com\/questions\/25773245\/ambiguity-in-pandas-dataframe-numpy-array-axis-definition\">Stack Overflow<\/a><\/p>\n<p>In the above diagram the row0, row1, row2 are the index for each record in the data set and the col0, col1, col2 etc are the column names for each columns(features) of the data set.<\/p>\n<p>Now that we have downloaded the data set from its source and converted that into a pandas Dataframe, let\u2019s display a few records from this dataframe. For this we will use the&nbsp;<code>head()<\/code>&nbsp;method.<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\"># Using .head() method to view the first few records of the data set<\/span>\nsales_data<span class=\"token punctuation\">.<\/span>head<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<div>\n<div>\n<div class=\"table-responsive\">\n<table class=\"dataframe table table-bordered\" border=\"1\">\n<thead>\n<tr>\n<th>&nbsp;<\/th>\n<th>Opportunity Number<\/th>\n<th>Supplies Subgroup<\/th>\n<th>Supplies Group<\/th>\n<th>Region<\/th>\n<th>Route To Market<\/th>\n<th>Elapsed Days In Sales Stage<\/th>\n<th>Opportunity Result<\/th>\n<th>Sales Stage Change Count<\/th>\n<th>Total Days Identified Through Closing<\/th>\n<th>Total Days Identified Through Qualified<\/th>\n<th>Opportunity Amount USD<\/th>\n<th>Client Size By Revenue<\/th>\n<th>Client Size By Employee Count<\/th>\n<th>Revenue From Client Past Two Years<\/th>\n<th>Competitor Type<\/th>\n<th>Ratio Days Identified To Total Days<\/th>\n<th>Ratio Days Validated To Total Days<\/th>\n<th>Ratio Days Qualified To Total Days<\/th>\n<th>Deal Size Category<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>0<\/th>\n<td>1641984<\/td>\n<td>Exterior Accessories<\/td>\n<td>Car Accessories<\/td>\n<td>Northwest<\/td>\n<td>Fields Sales<\/td>\n<td>76<\/td>\n<td>Won<\/td>\n<td>13<\/td>\n<td>104<\/td>\n<td>101<\/td>\n<td>0<\/td>\n<td>5<\/td>\n<td>5<\/td>\n<td>0<\/td>\n<td>Unknown<\/td>\n<td>0.69636<\/td>\n<td>0.113985<\/td>\n<td>0.154215<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<th>1<\/th>\n<td>1658010<\/td>\n<td>Exterior Accessories<\/td>\n<td>Car Accessories<\/td>\n<td>Pacific<\/td>\n<td>Reseller<\/td>\n<td>63<\/td>\n<td>Loss<\/td>\n<td>2<\/td>\n<td>163<\/td>\n<td>163<\/td>\n<td>0<\/td>\n<td>3<\/td>\n<td>5<\/td>\n<td>0<\/td>\n<td>Unknown<\/td>\n<td>0.00000<\/td>\n<td>1.000000<\/td>\n<td>0.000000<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<th>2<\/th>\n<td>1674737<\/td>\n<td>Motorcycle Parts<\/td>\n<td>Performance &amp; Non-auto<\/td>\n<td>Pacific<\/td>\n<td>Reseller<\/td>\n<td>24<\/td>\n<td>Won<\/td>\n<td>7<\/td>\n<td>82<\/td>\n<td>82<\/td>\n<td>7750<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>Unknown<\/td>\n<td>1.00000<\/td>\n<td>0.000000<\/td>\n<td>0.000000<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<th>3<\/th>\n<td>1675224<\/td>\n<td>Shelters &amp; RV<\/td>\n<td>Performance &amp; Non-auto<\/td>\n<td>Midwest<\/td>\n<td>Reseller<\/td>\n<td>16<\/td>\n<td>Loss<\/td>\n<td>5<\/td>\n<td>124<\/td>\n<td>124<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>Known<\/td>\n<td>1.00000<\/td>\n<td>0.000000<\/td>\n<td>0.000000<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<th>4<\/th>\n<td>1689785<\/td>\n<td>Exterior Accessories<\/td>\n<td>Car Accessories<\/td>\n<td>Pacific<\/td>\n<td>Reseller<\/td>\n<td>69<\/td>\n<td>Loss<\/td>\n<td>11<\/td>\n<td>91<\/td>\n<td>13<\/td>\n<td>69756<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>Unknown<\/td>\n<td>0.00000<\/td>\n<td>0.141125<\/td>\n<td>0.000000<\/td>\n<td>4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p>As can be seen from the above display, the&nbsp;<code>head()<\/code>&nbsp;method shows us the first few records from the data set. The&nbsp;<code>head()<\/code>&nbsp;method is a very nifty tool provided by pandas that helps us to get a feel of the content of a data set. We will talk more about the&nbsp;<code>head()<\/code>&nbsp;method in the next section.<\/p>\n<h2 id=\"dataexploration\">Data Exploration<\/h2>\n<p>Now that we have got the data set downloaded and converted into a pandas dataframe, lets do a quick exploration of the data see what stories the data can tell us so that we can plan our course of action.<\/p>\n<p>Data exploration is a very important step in any Data Science or Machine Learning project. Even a quick exploration of the data set can give us important information that we might otherwise miss, and that information can suggest important questions we can try to answer through our project.<\/p>\n<p>For exploring the data set, we will use some third party Python libraries to help us process the data so that it can be effectively used with scikit-learn\u2019s powerful algorithms. But we can start with the same&nbsp;<code>head()<\/code>&nbsp;method we used in the previous section to view the first few records of the imported data set, because&nbsp;<code>head()<\/code>&nbsp;is actually capable of doing much more than that! We can customize the&nbsp;<code>head()<\/code>&nbsp;method to show only a specific number of records as well:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\"># Using head() method with an argument which helps us to restrict the number of initial records that should be displayed<\/span>\nsales_data<span class=\"token punctuation\">.<\/span>head<span class=\"token punctuation\">(<\/span>n<span class=\"token operator\">=<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<div>\n<div>\n<div class=\"table-responsive\">\n<table class=\"dataframe table table-bordered\" border=\"1\">\n<thead>\n<tr>\n<th>&nbsp;<\/th>\n<th>Opportunity Number<\/th>\n<th>Supplies Subgroup<\/th>\n<th>Supplies Group<\/th>\n<th>Region<\/th>\n<th>Route To Market<\/th>\n<th>Elapsed Days In Sales Stage<\/th>\n<th>Opportunity Result<\/th>\n<th>Sales Stage Change Count<\/th>\n<th>Total Days Identified Through Closing<\/th>\n<th>Total Days Identified Through Qualified<\/th>\n<th>Opportunity Amount USD<\/th>\n<th>Client Size By Revenue<\/th>\n<th>Client Size By Employee Count<\/th>\n<th>Revenue From Client Past Two Years<\/th>\n<th>Competitor Type<\/th>\n<th>Ratio Days Identified To Total Days<\/th>\n<th>Ratio Days Validated To Total Days<\/th>\n<th>Ratio Days Qualified To Total Days<\/th>\n<th>Deal Size Category<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>0<\/th>\n<td>1641984<\/td>\n<td>Exterior Accessories<\/td>\n<td>Car Accessories<\/td>\n<td>Northwest<\/td>\n<td>Fields Sales<\/td>\n<td>76<\/td>\n<td>Won<\/td>\n<td>13<\/td>\n<td>104<\/td>\n<td>101<\/td>\n<td>0<\/td>\n<td>5<\/td>\n<td>5<\/td>\n<td>0<\/td>\n<td>Unknown<\/td>\n<td>0.69636<\/td>\n<td>0.113985<\/td>\n<td>0.154215<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<th>1<\/th>\n<td>1658010<\/td>\n<td>Exterior Accessories<\/td>\n<td>Car Accessories<\/td>\n<td>Pacific<\/td>\n<td>Reseller<\/td>\n<td>63<\/td>\n<td>Loss<\/td>\n<td>2<\/td>\n<td>163<\/td>\n<td>163<\/td>\n<td>0<\/td>\n<td>3<\/td>\n<td>5<\/td>\n<td>0<\/td>\n<td>Unknown<\/td>\n<td>0.00000<\/td>\n<td>1.000000<\/td>\n<td>0.000000<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p>In the code snippet above, we used an argument inside the&nbsp;<code>head()<\/code>&nbsp;method to display only the first two records from our data set. The integer \u20182\u2019 in the argument&nbsp;<code>n=2<\/code>&nbsp;actually denotes the second&nbsp;<code>index<\/code>&nbsp;of the Dataframe&nbsp;<code>Sales_data<\/code>. Using this we can get a quick look into the kind of data we have to work with. For example, we can see that columns like \u2018Supplies Group\u2019 and \u2018Region\u2019 contain string data, while columns like Opportunity Result, Opportunity Number etc. contain integers. Also, we can see that the \u2018Opportunity Number\u2019 column contains unique identifiers for each record.<\/p>\n<p>Now that we have viewed the initial records of our dataframe, let\u2019s try to view the last few records in the data set. This can be done using the&nbsp;<code>tail()<\/code>&nbsp;method, which has similar syntax as the&nbsp;<code>head()<\/code>&nbsp;method. Let\u2019s see what the&nbsp;<code>tail()<\/code>&nbsp;method can do:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\"># Using .tail() method to view the last few records from the dataframe<\/span>\nsales_data<span class=\"token punctuation\">.<\/span>tail<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<div>\n<div>\n<div class=\"table-responsive\">\n<table class=\"dataframe table table-bordered\" border=\"1\">\n<thead>\n<tr>\n<th>&nbsp;<\/th>\n<th>Opportunity Number<\/th>\n<th>Supplies Subgroup<\/th>\n<th>Supplies Group<\/th>\n<th>Region<\/th>\n<th>Route To Market<\/th>\n<th>Elapsed Days In Sales Stage<\/th>\n<th>Opportunity Result<\/th>\n<th>Sales Stage Change Count<\/th>\n<th>Total Days Identified Through Closing<\/th>\n<th>Total Days Identified Through Qualified<\/th>\n<th>Opportunity Amount USD<\/th>\n<th>Client Size By Revenue<\/th>\n<th>Client Size By Employee Count<\/th>\n<th>Revenue From Client Past Two Years<\/th>\n<th>Competitor Type<\/th>\n<th>Ratio Days Identified To Total Days<\/th>\n<th>Ratio Days Validated To Total Days<\/th>\n<th>Ratio Days Qualified To Total Days<\/th>\n<th>Deal Size Category<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>78020<\/th>\n<td>10089932<\/td>\n<td>Batteries &amp; Accessories<\/td>\n<td>Car Accessories<\/td>\n<td>Southeast<\/td>\n<td>Reseller<\/td>\n<td>0<\/td>\n<td>Loss<\/td>\n<td>2<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>250000<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>3<\/td>\n<td>Unknown<\/td>\n<td>0.0<\/td>\n<td>0.0<\/td>\n<td>0.0<\/td>\n<td>6<\/td>\n<\/tr>\n<tr>\n<th>78021<\/th>\n<td>10089961<\/td>\n<td>Shelters &amp; RV<\/td>\n<td>Performance &amp; Non-auto<\/td>\n<td>Northeast<\/td>\n<td>Reseller<\/td>\n<td>0<\/td>\n<td>Won<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>180000<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>Unknown<\/td>\n<td>0.0<\/td>\n<td>0.0<\/td>\n<td>0.0<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<th>78022<\/th>\n<td>10090145<\/td>\n<td>Exterior Accessories<\/td>\n<td>Car Accessories<\/td>\n<td>Southeast<\/td>\n<td>Reseller<\/td>\n<td>0<\/td>\n<td>Loss<\/td>\n<td>2<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>90000<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>Unknown<\/td>\n<td>0.0<\/td>\n<td>0.0<\/td>\n<td>0.0<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<th>78023<\/th>\n<td>10090430<\/td>\n<td>Exterior Accessories<\/td>\n<td>Car Accessories<\/td>\n<td>Southeast<\/td>\n<td>Fields Sales<\/td>\n<td>0<\/td>\n<td>Loss<\/td>\n<td>2<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>120000<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>Unknown<\/td>\n<td>1.0<\/td>\n<td>0.0<\/td>\n<td>0.0<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<th>78024<\/th>\n<td>10094255<\/td>\n<td>Interior Accessories<\/td>\n<td>Car Accessories<\/td>\n<td>Mid-Atlantic<\/td>\n<td>Reseller<\/td>\n<td>0<\/td>\n<td>Loss<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>90000<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>Unknown<\/td>\n<td>0.0<\/td>\n<td>0.0<\/td>\n<td>0.0<\/td>\n<td>4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p>The&nbsp;<code>tail()<\/code>&nbsp;method in the code snippet above returns us the last few records from the dataframe&nbsp;<code>sales_data<\/code>. We can pass an argument to the&nbsp;<code>tail()<\/code>&nbsp;method to view only a limited number of records from our dataframe, too:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\"># Using .tail() method with an argument which helps us to restrict the number of initial records that should be displayed<\/span>\nsales_data<span class=\"token punctuation\">.<\/span>tail<span class=\"token punctuation\">(<\/span>n<span class=\"token operator\">=<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<div>\n<div>\n<div class=\"table-responsive\">\n<table class=\"table table-bordered cke_show_border\">\n<tbody>\n<tr>\n<th>Opportunity Number<\/th>\n<th>Supplies Subgroup<\/th>\n<th>Supplies Group<\/th>\n<th>Region<\/th>\n<th>Route To Market<\/th>\n<th>Elapsed Days In Sales Stage<\/th>\n<th>Opportunity Result<\/th>\n<th>Sales Stage Change Count<\/th>\n<th>Total Days Identified Through Closing<\/th>\n<th>Total Days Identified Through Qualified<\/th>\n<th>Opportunity Amount USD<\/th>\n<th>Client Size By Revenue<\/th>\n<th>Client Size By Employee Count<\/th>\n<th>Revenue From Client Past Two Years<\/th>\n<th>Competitor Type<\/th>\n<th>Ratio Days Identified To Total Days<\/th>\n<th>Ratio Days Validated To Total Days<\/th>\n<th>Ratio Days Qualified To Total Days<\/th>\n<th>Deal Size Category<\/th>\n<\/tr>\n<tr>\n<th>78023<\/th>\n<td>10090430<\/td>\n<td>Exterior Accessories<\/td>\n<td>Car Accessories<\/td>\n<td>Southeast<\/td>\n<td>Fields Sales<\/td>\n<td>0<\/td>\n<td>Loss<\/td>\n<td>2<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>120000<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>Unknown<\/td>\n<td>1.0<\/td>\n<td>0.0<\/td>\n<td>0.0<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<th>78024<\/th>\n<td>10094255<\/td>\n<td>Interior Accessories<\/td>\n<td>Car Accessories<\/td>\n<td>Mid-Atlantic<\/td>\n<td>Reseller<\/td>\n<td>0<\/td>\n<td>Loss<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>90000<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>Unknown<\/td>\n<td>0.0<\/td>\n<td>0.0<\/td>\n<td>0.0<\/td>\n<td>4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p>We can now view only the last two records from the dataframe, as indicated by the argument&nbsp;<code>n=2<\/code>&nbsp;inside the&nbsp;<code>tail()<\/code>&nbsp;method. Similar to the&nbsp;<code>head()<\/code>&nbsp;method, the integer \u20182\u2019 in the argument&nbsp;<code>n=2<\/code>&nbsp;in the&nbsp;<code>tail()<\/code>&nbsp;method points to the second index from the last two records in the data set&nbsp;<code>sales_data<\/code>.<\/p>\n<p>What story do these last two records tell us? Looking at the \u2018Opportunity Number\u2019 column of the trailer records from the dataframe, it becomes clear to us that a total of 78,024 records are available. This is evident from the \u2018index\u2019 number of the records displayed with the&nbsp;<code>tail()<\/code>&nbsp;method.<\/p>\n<p>Now, it would be good if we could see the different datatypes that are available in this data set; this information can be handy in case we need to do some conversion later on. We can do that with the&nbsp;<code>dtypes()<\/code>&nbsp;method in pandas:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\"># using the dtypes() method to display the different datatypes available<\/span>\nsales_data<span class=\"token punctuation\">.<\/span>dtypes<\/code><\/pre>\n<pre class=\"language-python\"><code class=\"language-python\">Opportunity Number int64\nSupplies Subgroup object\nSupplies Group object\nRegion object\nRoute To Market object\nElapsed Days In Sales Stage int64\nOpportunity Result object\nSales Stage Change Count int64\nTotal Days Identified Through Closing int64\nTotal Days Identified Through Qualified int64\nOpportunity Amount USD int64\nClient Size By Revenue int64\nClient Size By Employee Count int64\nRevenue From Client Past Two Years int64\nCompetitor Type object\nRatio Days Identified To Total Days float64\nRatio Days Validated To Total Days float64\nRatio Days Qualified To Total Days float64\nDeal Size Category int64\ndtype<span class=\"token punctuation\">:<\/span> object<\/code><\/pre>\n<p>As we can see in the code snippet above, using the&nbsp;<code>dtypes<\/code>&nbsp;method, we can list the different columns available in the Dataframe along with their respective datatypes. For example, we can see that the Supplies Subgroup column is an&nbsp;<code>object<\/code>&nbsp;datatype and the \u2018Client Size By Revenue\u2019 column is an&nbsp;<code>integer<\/code>&nbsp;datatype. So, now we know which columns have integers in them and which columns have string data in them.<\/p>\n<h2 id=\"datavisualization\">Data Visualization<\/h2>\n<p>Now that we\u2019ve done some basic data exploration, let\u2019s try to create some nice plots to visually represent the data and uncover more stories hidden in the data set.<\/p>\n<p>There are many python libraries that provide functions for doing data visualization; one such library is&nbsp;<code>Seaborn<\/code>. To use Seaborn plots, we should make sure that this python module is downloaded and installed.<\/p>\n<p>Let\u2019s set up the code to use the&nbsp;<code>Seaborn<\/code>&nbsp;module:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\"># import the seaborn module<\/span>\n<span class=\"token keyword\">import<\/span> seaborn <span class=\"token keyword\">as<\/span> sns\n<span class=\"token comment\" spellcheck=\"true\"># import the matplotlib module<\/span>\n<span class=\"token keyword\">import<\/span> matplotlib<span class=\"token punctuation\">.<\/span>pyplot <span class=\"token keyword\">as<\/span> plt\n<span class=\"token comment\" spellcheck=\"true\"># set the background colour of the plot to white<\/span>\nsns<span class=\"token punctuation\">.<\/span>set<span class=\"token punctuation\">(<\/span>style<span class=\"token operator\">=<\/span><span class=\"token string\">\"whitegrid\"<\/span><span class=\"token punctuation\">,<\/span> color_codes<span class=\"token operator\">=<\/span><span class=\"token boolean\">True<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\"># setting the plot size for all plots<\/span>\nsns<span class=\"token punctuation\">.<\/span>set<span class=\"token punctuation\">(<\/span>rc<span class=\"token operator\">=<\/span><span class=\"token punctuation\">{<\/span><span class=\"token string\">'figure.figsize'<\/span><span class=\"token punctuation\">:<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">11.7<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">8.27<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\"># create a countplot<\/span>\nsns<span class=\"token punctuation\">.<\/span>countplot<span class=\"token punctuation\">(<\/span><span class=\"token string\">'Route To Market'<\/span><span class=\"token punctuation\">,<\/span>data<span class=\"token operator\">=<\/span>sales_data<span class=\"token punctuation\">,<\/span>hue <span class=\"token operator\">=<\/span> <span class=\"token string\">'Opportunity Result'<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\"># Remove the top and down margin<\/span>\nsns<span class=\"token punctuation\">.<\/span>despine<span class=\"token punctuation\">(<\/span>offset<span class=\"token operator\">=<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">,<\/span> trim<span class=\"token operator\">=<\/span><span class=\"token boolean\">True<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\"># display the plotplt.show()<\/span><\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.stechstar.com\/user\/zbxe\/files\/attach\/images\/3263\/244\/089\/9c6e6c832317b41f106189b6ea480364.png\" alt=\"output_16_0\" data-autoattach=\"success\" data-cke-saved-src=\".\/files\/attach\/images\/3263\/244\/089\/9c6e6c832317b41f106189b6ea480364.png\"><\/p>\n<p>Now that we\u2019ve got Seaborn set up, let\u2019s take a deeper look at what we just did.<\/p>\n<p>First we imported the Seaborn module and the matplotlib module. The&nbsp;<code>set()<\/code>&nbsp;method in the next line helps to set different properties for our plot, like \u2018styles\u2019, \u2018color\u2019 etc. Using the&nbsp;<code>sns.set(style=\"whitegrid\", color_codes=True)<\/code>&nbsp;code snippet we set the background of the plot to a light color. Then we set the plot size with the&nbsp;<code>sns.set(rc={'figure.figsize':(11.7,8.27)})<\/code>code snippet, which defines the plot figure size to be 11.7px and 8.27px.<\/p>\n<p>Next, we create the plot using&nbsp;<code>sns.countplot('Route To Market',data=sales_data,hue = 'Opportunity Result')<\/code>. The&nbsp;<code>countplot()<\/code>&nbsp;method helps us to create a countplot and it exposes several arguments to customize the countplot per our needs. Here, in the first argument of the&nbsp;<code>countplot()<\/code>&nbsp;method, we defined the X-axis as the column \u2018Route To Market\u2019 from our data set. The second argument is the data source, which in this case is the dataframe&nbsp;<code>sales_data<\/code>&nbsp;that we created in the first section of this tutorial. The third argument is the color of the barplots which we assigned to \u2018blue\u2019 for the label \u2018won\u2019 and \u2018green\u2019 for the label \u2018loss\u2019 from the \u2018Opportunity Result\u2019 column of the&nbsp;<code>sales_data<\/code>&nbsp;dataframe.<\/p>\n<p>More details about Seaborn countplots can be found&nbsp;<a href=\"https:\/\/seaborn.pydata.org\/generated\/seaborn.countplot.html\" data-cke-saved-href=\"https:\/\/seaborn.pydata.org\/generated\/seaborn.countplot.html\">here<\/a>.<\/p>\n<p>So, what does the countplot tell us about the data? The first thing is that the data set has more records of the type \u2018loss\u2019 than records of the type \u2018won\u2019, as we can see from the size of the bars. Looking at the x axis and the corresponding bars for each label on the x axis, we can see that most of the data from our data set is concentrated towards the left side of the plot: towards the \u2018Field Sales\u2019 and \u2018Reseller\u2019 categories. Another thing to notice is that the category \u2018Field Sales\u2019 has more losses than the category \u2018Reseller\u2019.<\/p>\n<p>We selected the Route To Market column for our plot because it seemed like it would provide helpful information after our initial study of the&nbsp;<code>head()<\/code>&nbsp;and&nbsp;<code>tail()<\/code>&nbsp;methods\u2019 output. But other fields like \u2018Region\u2019 , \u2018Supplies Group\u2019 etc. can also be used to make plots in the same manner.<\/p>\n<p>Now that we have got a pretty good visualization of what our overall data looks like, let\u2019s see what more information can we dig out with the help of other Seaborn plots. Another popular option is&nbsp;<code>violinplots<\/code>, so let\u2019s create a violin plot and see what that style of plot can tell us.<\/p>\n<p>We will use the&nbsp;<code>violinplot()<\/code>&nbsp;method provided by the Seaborn module to create the violin plot. Let\u2019s first import the&nbsp;<code>seaborn<\/code>&nbsp;module and use the&nbsp;<code>set()<\/code>&nbsp;method to customize the size of our plot. We will seet the size of the plot as 16.7px by 13.27px:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\"># import the seaborn module<\/span>\n<span class=\"token keyword\">import<\/span> seaborn <span class=\"token keyword\">as<\/span> sns\n<span class=\"token comment\" spellcheck=\"true\"># import the matplotlib module<\/span>\n<span class=\"token keyword\">import<\/span> matplotlib<span class=\"token punctuation\">.<\/span>pyplot <span class=\"token keyword\">as<\/span> plt\n<span class=\"token comment\" spellcheck=\"true\"># setting the plot size for all plots<\/span>\nsns<span class=\"token punctuation\">.<\/span>set<span class=\"token punctuation\">(<\/span>rc<span class=\"token operator\">=<\/span><span class=\"token punctuation\">{<\/span><span class=\"token string\">'figure.figsize'<\/span><span class=\"token punctuation\">:<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">16.7<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">13.27<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<p>Next, we will use the&nbsp;<code>violinplot()<\/code>&nbsp;method to create the violinplot and then use the&nbsp;<code>show()<\/code>&nbsp;mehtod to display the plot \u2013<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\"># plotting the violinplot<\/span>\nsns<span class=\"token punctuation\">.<\/span>violinplot<span class=\"token punctuation\">(<\/span>x<span class=\"token operator\">=<\/span><span class=\"token string\">\"Opportunity Result\"<\/span><span class=\"token punctuation\">,<\/span>y<span class=\"token operator\">=<\/span><span class=\"token string\">\"Client Size By Revenue\"<\/span><span class=\"token punctuation\">,<\/span> hue<span class=\"token operator\">=<\/span><span class=\"token string\">\"Opportunity Result\"<\/span><span class=\"token punctuation\">,<\/span> data<span class=\"token operator\">=<\/span>sales_data<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\nplt<span class=\"token punctuation\">.<\/span>show<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<pre class=\"language-python\"><img decoding=\"async\" src=\"https:\/\/www.stechstar.com\/user\/zbxe\/files\/attach\/images\/3263\/244\/089\/fedcf09d4fc39e5492c3aa51ef18ad43.png\" alt=\"output_20_0\" data-autoattach=\"success\" data-cke-saved-src=\".\/files\/attach\/images\/3263\/244\/089\/fedcf09d4fc39e5492c3aa51ef18ad43.png\"><\/pre>\n<p>Now, that our plot is created, let\u2019s see what it tells us. In its simplest form, a violin plot displays the distribution of data across labels. In the above plot we have labels \u2018won\u2019 and \u2018loss\u2019 on the x-axis and the values of \u2018Client Size By Revenue\u2019 in the y-axis. The violin plot shows us that the largest distribution of data is in the client size \u20181\u2019, and the rest of the client size labels have less data.<\/p>\n<p>This violin plot gives us very valuable insight into how the data is distributed and which features and labels have the largest concentration of data, but there is more than what meets the eye in case of violin plots. You can dig deeper into the additional uses of violin plots via&nbsp;<a href=\"https:\/\/seaborn.pydata.org\/generated\/seaborn.violinplot.html\" data-cke-saved-href=\"https:\/\/seaborn.pydata.org\/generated\/seaborn.violinplot.html\">the official documentation of the&nbsp;<code>Seaborn<\/code>&nbsp;module<\/a><\/p>\n<h2 id=\"preprocessingdata\">Preprocessing Data<\/h2>\n<p>Now that we have a good understanding of what our data looks like, we can move towards preparing it to build prediction models using scikit-learn.<\/p>\n<p>We saw in our initial exploration that most of the columns in our data set are strings, but the algorithms in scikit-learn understand only numeric data. Luckily, the scikit-learn library provides us with many methods for converting string data into numerical data. One such method is the&nbsp;<code>LabelEncoder()<\/code>&nbsp;method. We will use this method to convert the categorical labels in our data set like \u2018won\u2019 and \u2018loss\u2019 into numerical labels. To visualize what we are trying to to achieve with the&nbsp;<code>LabelEncoder()<\/code>&nbsp;method let\u2019s consider the images below.<\/p>\n<p>The image below represents a dataframe that has one column named \u2018color\u2019 and three records \u2018Red\u2019, \u2018Green\u2019 and \u2018Blue\u2019.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.stechstar.com\/user\/zbxe\/files\/attach\/images\/3263\/244\/089\/aa054b792a1dc6037e1d37d93758fd19.png\" alt=\"dataframe_before-1\" data-autoattach=\"success\" data-cke-saved-src=\".\/files\/attach\/images\/3263\/244\/089\/aa054b792a1dc6037e1d37d93758fd19.png\"><\/p>\n<p>Since the machine learning algorithms in scikit-learn understand only numeric inputs, we would like to convert the categorical labels like \u2018Red, \u2018Green\u2019 and \u2018Blue\u2019 into numeric labels. When we are done converting the categorical labels in the original dataframe, we would get something like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.stechstar.com\/user\/zbxe\/files\/attach\/images\/3263\/244\/089\/482177d288fc156587133536ffbd15b5.png\" alt=\"dataframe_after-1\" data-autoattach=\"success\" data-cke-saved-src=\".\/files\/attach\/images\/3263\/244\/089\/482177d288fc156587133536ffbd15b5.png\"><\/p>\n<p>Now, let\u2019s start the actual conversion process. We will use the&nbsp;<code>fit_transform()<\/code>&nbsp;method provided by&nbsp;<code>LabelEncoder()<\/code>&nbsp;to encode the labels in the categorical column such as \u2018Route To Market\u2019 in the&nbsp;<code>sales_data<\/code>&nbsp;dataframe and convert them into numeric labels similar to what we visualized in the above diagrams. The&nbsp;<code>fit_transform()<\/code>&nbsp;function takes user defined labels as input and then returns encoded labels. Let\u2019s go through a quick example to understand how the encoding is done. In the code example below we have a list of cities i.e.&nbsp;<code>[\"paris\", \"paris\", \"tokyo\", \"amsterdam\"]<\/code>&nbsp;and we will try to encode these string labels into something similar to this \u2013&nbsp;<code>[2, 2, 1,3]<\/code>.<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\">#import the necessary module<\/span>\n<span class=\"token keyword\">from<\/span> sklearn <span class=\"token keyword\">import<\/span> preprocessing\n<span class=\"token comment\" spellcheck=\"true\"># create the Labelencoder object<\/span>\nle <span class=\"token operator\">=<\/span> preprocessing<span class=\"token punctuation\">.<\/span>LabelEncoder<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\">#convert the categorical columns into numeric<\/span>\nencoded_value <span class=\"token operator\">=<\/span> le<span class=\"token punctuation\">.<\/span>fit_transform<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">\"paris\"<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">\"paris\"<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">\"tokyo\"<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">\"amsterdam\"<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>encoded_value<span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<pre class=\"language-python\"><code><span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span> <span class=\"token number\">1<\/span> <span class=\"token number\">2<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><\/code><\/pre>\n<p>Voila! We have successfully converted the string labels into numeric labels. How\u2019d we do that? First we imported the&nbsp;<code>preprocessing<\/code>&nbsp;module which provides the&nbsp;<code>LabelEncoder()<\/code>&nbsp;method. Then we created an object which represents the&nbsp;<code>LabelEncoder()<\/code>&nbsp;type. Next we used this object\u2019s&nbsp;<code>fit_transform()<\/code>&nbsp;function to differentiate between different unique classes of the list&nbsp;<code>[\"paris\", \"paris\", \"tokyo\", \"amsterdam\"]<\/code>&nbsp;and then return a list with the respective encoded values, i.e.&nbsp;<code>[1 1 2 0]<\/code>.<\/p>\n<p>Notice how the&nbsp;<code>LabelEncoder()<\/code>&nbsp;method assigns the numeric values to the classes in the order of the first letter of the classes from the original list: \u201c(a)msterdam\u201d gets an encoding of \u20180\u2019 , \u201c(p)aris gets an encoding of 1\u201d and \u201c(t)okyo\u201d gets an encoding of 2.<\/p>\n<p>There are many more functions provided by&nbsp;<code>LabelEncoder()<\/code>&nbsp;that are handy under a variety of encoding requirements. We won\u2019t need them here, but to learn more, a good place to start is&nbsp;<a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.preprocessing.LabelEncoder.html\" data-cke-saved-href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.preprocessing.LabelEncoder.html\">the official page of scikit-learn<\/a>&nbsp;where the&nbsp;<code>LabelEncoder()<\/code>&nbsp;and its related functions are described in detail.<\/p>\n<p>Since, we now have a good idea of how the&nbsp;<code>LabelEncoder()<\/code>&nbsp;works, we can move forward with using this method to encode the categorical labels from the&nbsp;<code>sales_data<\/code>&nbsp;dataframe and convert them into numeric labels. In the previous sections during the initial exploration of the data set we saw that the following columns contain string values: \u2018Supplies Subgroup\u2019, \u2018Region\u2019, \u2018Route To Market\u2019, \u2018Opportunity Result\u2019, \u2018Competitor Type\u2019, and \u2018Supplies Group\u2019. Before we start encoding these string labels, let\u2019s take a quick look into the different labels that these columns contain:-<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Supplies Subgroup' : \"<\/span><span class=\"token punctuation\">,<\/span>sales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Supplies Subgroup'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">.<\/span>unique<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Region : \"<\/span><span class=\"token punctuation\">,<\/span>sales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Region'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">.<\/span>unique<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Route To Market : \"<\/span><span class=\"token punctuation\">,<\/span>sales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Route To Market'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">.<\/span>unique<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Opportunity Result : \"<\/span><span class=\"token punctuation\">,<\/span>sales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Opportunity Result'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">.<\/span>unique<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Competitor Type : \"<\/span><span class=\"token punctuation\">,<\/span>sales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Competitor Type'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">.<\/span>unique<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"'Supplies Group : \"<\/span><span class=\"token punctuation\">,<\/span>sales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Supplies Group'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">.<\/span>unique<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<pre class=\"language-python\"><code class=\"language-python\">Supplies Subgroup<span class=\"token string\">' : ['<\/span>Exterior Accessories<span class=\"token string\">' '<\/span>Motorcycle Parts<span class=\"token string\">' '<\/span>Shelters <span class=\"token operator\">&amp;<\/span> RV'\n<span class=\"token string\">'Garage &amp; Car Care'<\/span> <span class=\"token string\">'Batteries &amp; Accessories'<\/span> <span class=\"token string\">'Performance Parts'<\/span>\n<span class=\"token string\">'Towing &amp; Hitches'<\/span> <span class=\"token string\">'Replacement Parts'<\/span> <span class=\"token string\">'Tires &amp; Wheels'<\/span>\n<span class=\"token string\">'Interior Accessories'<\/span> <span class=\"token string\">'Car Electronics'<\/span><span class=\"token punctuation\">]<\/span>\nRegion <span class=\"token punctuation\">:<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token string\">'Northwest'<\/span> <span class=\"token string\">'Pacific'<\/span> <span class=\"token string\">'Midwest'<\/span> <span class=\"token string\">'Southwest'<\/span> <span class=\"token string\">'Mid-Atlantic'<\/span> <span class=\"token string\">'Northeast'<\/span>\n<span class=\"token string\">'Southeast'<\/span><span class=\"token punctuation\">]<\/span>\nRoute To Market <span class=\"token punctuation\">:<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token string\">'Fields Sales'<\/span> <span class=\"token string\">'Reseller'<\/span> <span class=\"token string\">'Other'<\/span> <span class=\"token string\">'Telesales'<\/span> <span class=\"token string\">'Telecoverage'<\/span><span class=\"token punctuation\">]<\/span>\nOpportunity Result <span class=\"token punctuation\">:<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token string\">'Won'<\/span> <span class=\"token string\">'Loss'<\/span><span class=\"token punctuation\">]<\/span>\nCompetitor Type <span class=\"token punctuation\">:<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token string\">'Unknown'<\/span> <span class=\"token string\">'Known'<\/span> <span class=\"token string\">'None'<\/span><span class=\"token punctuation\">]<\/span>\n<span class=\"token string\">'Supplies Group : ['<\/span>Car Accessories<span class=\"token string\">' '<\/span>Performance <span class=\"token operator\">&amp;<\/span> Non<span class=\"token operator\">-<\/span>auto<span class=\"token string\">' '<\/span>Tires <span class=\"token operator\">&amp;<\/span> Wheels'\n<span class=\"token string\">'Car Electronics'<\/span><span class=\"token punctuation\">]<\/span><\/code><\/pre>\n<p>We have now laid out the different categorical columns from the&nbsp;<code>sales_data<\/code>&nbsp;dataframe and the unique classes under each of these columns. Now, it\u2019s time to encode these strings into numeric labels. To do this, we will run the code below and then do a deep dive to understand how it works:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\">#import the necessary module<\/span>\n<span class=\"token keyword\">from<\/span> sklearn <span class=\"token keyword\">import<\/span> preprocessing\n<span class=\"token comment\" spellcheck=\"true\"># create the Labelencoder object<\/span>\nle <span class=\"token operator\">=<\/span> preprocessing<span class=\"token punctuation\">.<\/span>LabelEncoder<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\">#convert the categorical columns into numeric<\/span>\nsales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Supplies Subgroup'<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span> le<span class=\"token punctuation\">.<\/span>fit_transform<span class=\"token punctuation\">(<\/span>sales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Supplies Subgroup'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>\nsales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Region'<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span> le<span class=\"token punctuation\">.<\/span>fit_transform<span class=\"token punctuation\">(<\/span>sales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Region'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>\nsales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Route To Market'<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span> le<span class=\"token punctuation\">.<\/span>fit_transform<span class=\"token punctuation\">(<\/span>sales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Route To Market'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>\nsales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Opportunity Result'<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span> le<span class=\"token punctuation\">.<\/span>fit_transform<span class=\"token punctuation\">(<\/span>sales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Opportunity Result'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>\nsales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Competitor Type'<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span> le<span class=\"token punctuation\">.<\/span>fit_transform<span class=\"token punctuation\">(<\/span>sales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Competitor Type'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>\nsales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Supplies Group'<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span> le<span class=\"token punctuation\">.<\/span>fit_transform<span class=\"token punctuation\">(<\/span>sales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Supplies Group'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\">#display the initial records<\/span>\nsales_data<span class=\"token punctuation\">.<\/span>head<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<div>\n<div>\n<div class=\"table-responsive\">\n<table class=\"dataframe table table-bordered\" border=\"1\">\n<thead>\n<tr>\n<th>&nbsp;<\/th>\n<th>Opportunity Number<\/th>\n<th>Supplies Subgroup<\/th>\n<th>Supplies Group<\/th>\n<th>Region<\/th>\n<th>Route To Market<\/th>\n<th>Elapsed Days In Sales Stage<\/th>\n<th>Opportunity Result<\/th>\n<th>Sales Stage Change Count<\/th>\n<th>Total Days Identified Through Closing<\/th>\n<th>Total Days Identified Through Qualified<\/th>\n<th>Opportunity Amount USD<\/th>\n<th>Client Size By Revenue<\/th>\n<th>Client Size By Employee Count<\/th>\n<th>Revenue From Client Past Two Years<\/th>\n<th>Competitor Type<\/th>\n<th>Ratio Days Identified To Total Days<\/th>\n<th>Ratio Days Validated To Total Days<\/th>\n<th>Ratio Days Qualified To Total Days<\/th>\n<th>Deal Size Category<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>0<\/th>\n<td>1641984<\/td>\n<td>2<\/td>\n<td>0<\/td>\n<td>3<\/td>\n<td>0<\/td>\n<td>76<\/td>\n<td>1<\/td>\n<td>13<\/td>\n<td>104<\/td>\n<td>101<\/td>\n<td>0<\/td>\n<td>5<\/td>\n<td>5<\/td>\n<td>0<\/td>\n<td>2<\/td>\n<td>0.69636<\/td>\n<td>0.113985<\/td>\n<td>0.154215<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<th>1<\/th>\n<td>1658010<\/td>\n<td>2<\/td>\n<td>0<\/td>\n<td>4<\/td>\n<td>2<\/td>\n<td>63<\/td>\n<td>0<\/td>\n<td>2<\/td>\n<td>163<\/td>\n<td>163<\/td>\n<td>0<\/td>\n<td>3<\/td>\n<td>5<\/td>\n<td>0<\/td>\n<td>2<\/td>\n<td>0.00000<\/td>\n<td>1.000000<\/td>\n<td>0.000000<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<th>2<\/th>\n<td>1674737<\/td>\n<td>5<\/td>\n<td>2<\/td>\n<td>4<\/td>\n<td>2<\/td>\n<td>24<\/td>\n<td>1<\/td>\n<td>7<\/td>\n<td>82<\/td>\n<td>82<\/td>\n<td>7750<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>2<\/td>\n<td>1.00000<\/td>\n<td>0.000000<\/td>\n<td>0.000000<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<th>3<\/th>\n<td>1675224<\/td>\n<td>8<\/td>\n<td>2<\/td>\n<td>1<\/td>\n<td>2<\/td>\n<td>16<\/td>\n<td>0<\/td>\n<td>5<\/td>\n<td>124<\/td>\n<td>124<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>1.00000<\/td>\n<td>0.000000<\/td>\n<td>0.000000<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<th>4<\/th>\n<td>1689785<\/td>\n<td>2<\/td>\n<td>0<\/td>\n<td>4<\/td>\n<td>2<\/td>\n<td>69<\/td>\n<td>0<\/td>\n<td>11<\/td>\n<td>91<\/td>\n<td>13<\/td>\n<td>69756<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>2<\/td>\n<td>0.00000<\/td>\n<td>0.141125<\/td>\n<td>0.000000<\/td>\n<td>4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p>So what did we just do? First we imported the&nbsp;<code>preprocessing<\/code>&nbsp;module which provides the&nbsp;<code>LabelEncoder()<\/code>&nbsp;method. Then we created an object&nbsp;<code>le<\/code>&nbsp;of the type&nbsp;<code>labelEncoder()<\/code>. In the next couple of lines we used the&nbsp;<code>fit_transform()<\/code>&nbsp;function provided by&nbsp;<code>LabelEncoder()<\/code>&nbsp;and converted the categorical labels of different columns like \u2018Supplies Subgroup\u2019, \u2018Region\u2019, Route To Market\u2019 into numeric labels. In doing this, we successfully converted all the categorical (string) columns into numeric values.<\/p>\n<p>Now that we have our data prepared and converted it is&nbsp;<em>almost<\/em>&nbsp;ready to be used for building our predictive model. But we still need to do one critical thing:<\/p>\n<h2 id=\"trainingsettestset\">Training Set &amp; Test Set<\/h2>\n<p>A Machine Learning algorithm needs to be trained on a set of data to learn the relationships between different features and how these features affect the target variable. For this we need to divide the entire data set into two sets. One is the training set on which we are going to train our algorithm to build a model. The other is the testing set on which we will test our model to see how accurate its predictions are.<\/p>\n<p>But before doing all this splitting, let\u2019s first separate our features and target variables. As before in this tutorial, we will first run the code below, and then take a closer look at what it does:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\"># select columns other than 'Opportunity Number','Opportunity Result'cols = [col for col in sales_data.columns if col not in ['Opportunity Number','Opportunity Result']]<\/span>\n<span class=\"token comment\" spellcheck=\"true\"># dropping the 'Opportunity Number'and 'Opportunity Result' columns<\/span>\ndata <span class=\"token operator\">=<\/span> sales_data<span class=\"token punctuation\">[<\/span>cols<span class=\"token punctuation\">]<\/span>\n<span class=\"token comment\" spellcheck=\"true\">#assigning the Oppurtunity Result column as target<\/span>\ntarget <span class=\"token operator\">=<\/span> sales_data<span class=\"token punctuation\">[<\/span><span class=\"token string\">'Opportunity Result'<\/span><span class=\"token punctuation\">]<\/span>\ndata<span class=\"token punctuation\">.<\/span>head<span class=\"token punctuation\">(<\/span>n<span class=\"token operator\">=<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<div>\n<div>\n<div class=\"table-responsive\">\n<table class=\"dataframe table table-bordered\" border=\"1\">\n<thead>\n<tr>\n<th>&nbsp;<\/th>\n<th>Supplies Subgroup<\/th>\n<th>Supplies Group<\/th>\n<th>Region<\/th>\n<th>Route To Market<\/th>\n<th>Elapsed Days In Sales Stage<\/th>\n<th>Sales Stage Change Count<\/th>\n<th>Total Days Identified Through Closing<\/th>\n<th>Total Days Identified Through Qualified<\/th>\n<th>Opportunity Amount USD<\/th>\n<th>Client Size By Revenue<\/th>\n<th>Client Size By Employee Count<\/th>\n<th>Revenue From Client Past Two Years<\/th>\n<th>Competitor Type<\/th>\n<th>Ratio Days Identified To Total Days<\/th>\n<th>Ratio Days Validated To Total Days<\/th>\n<th>Ratio Days Qualified To Total Days<\/th>\n<th>Deal Size Category<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>0<\/th>\n<td>2<\/td>\n<td>0<\/td>\n<td>3<\/td>\n<td>0<\/td>\n<td>76<\/td>\n<td>13<\/td>\n<td>104<\/td>\n<td>101<\/td>\n<td>0<\/td>\n<td>5<\/td>\n<td>5<\/td>\n<td>0<\/td>\n<td>2<\/td>\n<td>0.69636<\/td>\n<td>0.113985<\/td>\n<td>0.154215<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<th>1<\/th>\n<td>2<\/td>\n<td>0<\/td>\n<td>4<\/td>\n<td>2<\/td>\n<td>63<\/td>\n<td>2<\/td>\n<td>163<\/td>\n<td>163<\/td>\n<td>0<\/td>\n<td>3<\/td>\n<td>5<\/td>\n<td>0<\/td>\n<td>2<\/td>\n<td>0.00000<\/td>\n<td>1.000000<\/td>\n<td>0.000000<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p>OK, so what did we just do? First, we don\u2019t need the \u2018Opportunity Number\u2019 column as it is just a unique identifier for each record. Also, we want to predict the \u2018Opportunity Result\u2019, so it should be our \u2018target\u2019 rather than part of \u2018data\u2019. So, in the first line of the code above, we selected only the columns which didn\u2019t match \u2018Opportunity Number\u2019and \u2018Opportunity Result\u2019 and assigned them to a variable&nbsp;<code>cols<\/code>. Next, we created a new dataframe&nbsp;<code>data<\/code>&nbsp;with the columns in the list&nbsp;<code>cols<\/code>. This will serve as our feature set. Then we took the \u2018Opportunity Result\u2019 column from the dataframe&nbsp;<code>sales_data<\/code>&nbsp;and created a new dataframe&nbsp;<code>target<\/code>.<\/p>\n<p>That\u2019s it! We are all set with defining our features and target into two separate dataframes. Next we will divide the dataframes&nbsp;<code>data<\/code>&nbsp;and&nbsp;<code>target<\/code>&nbsp;into training sets and testing sets. When splitting the data set we will keep 30% of the data as the test data and the remaining 70% as the training data. But keep in mind that those numbers are arbitrary and the best split will depend on the specific data you\u2019re working with. If you\u2019re not sure how to split your data, the 80\/20 principle where you keep 80% of the data as training data and use the remaining 20% as test data is a decent default. However, for this tutorial, we are going to stick with our earlier decision of keeping aside 30% of the data as test data. The&nbsp;<code>train_test_split()<\/code>&nbsp;method in scikit-learn can be used to split the data:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\">#import the necessary module<\/span>\n<span class=\"token keyword\">from<\/span> sklearn<span class=\"token punctuation\">.<\/span>model_selection <span class=\"token keyword\">import<\/span> train_test_split\n<span class=\"token comment\" spellcheck=\"true\">#split data set into train and test setsdata_train, data_test, target_train, target_test = train_test_split(data,target, test_size = 0.30, random_state = 10)<\/span><\/code><\/pre>\n<p>With this, we have now successfully prepared a testing set and a training set. In the above code first we imported the train_test_split module. Next we used the&nbsp;<code>train_test_split()<\/code>&nbsp;method to divide the data into a training set (data_train,target_train) and a test set (data_test,data_train). The first argument of the&nbsp;<code>train_test_split()<\/code>&nbsp;method are the features that we separated out in the previous section, the second argument is the target(\u2018Opportunity Result\u2019). The third argument \u2018test_size\u2019 is the percentage of the data that we want to separate out as training data . In our case it\u2019s 30% , although this can be any number. The fourth argument \u2018random_state\u2019 just ensures that we get reproducible results every time.<\/p>\n<p>Now, we have everything ready and here comes the most important and interesting part of this tutorial: building a prediction model using the vast library of algorithms available through scikit-learn.<\/p>\n<h2 id=\"buildingthemodel\">Building The Model<\/h2>\n<p>There\u2019s a&nbsp;<code>machine_learning_map<\/code>&nbsp;available&nbsp;<a href=\"https:\/\/scikit-learn.org\/stable\/tutorial\/machine_learning_map\/\" data-cke-saved-href=\"https:\/\/scikit-learn.org\/stable\/tutorial\/machine_learning_map\/\">on scikit learn\u2019s website<\/a>&nbsp;that we can use as a quick reference when choosing an algorithm. It looks something like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.stechstar.com\/user\/zbxe\/files\/attach\/images\/3263\/244\/089\/219481142e660c62bfa5e995569f2a2c.jpg\" alt=\"ML-cheat-sheet-1\" data-autoattach=\"success\" data-cke-saved-src=\".\/files\/attach\/images\/3263\/244\/089\/219481142e660c62bfa5e995569f2a2c.jpg\"><\/p>\n<p>We can use this map as a cheat sheet to shortlist the algorithms that we can try out to build our prediction model. Using the checklist let\u2019s see under which category we fall:<\/p>\n<ul>\n<li>More than 50 samples \u2013 Check<\/li>\n<li>Are we predicting a category \u2013 Check<\/li>\n<li>We have labeled data? (&nbsp;<em>data with clear names like opportunity amount etc.<\/em>) \u2013 Check<\/li>\n<li>Less than 100k samples \u2013 Check<\/li>\n<\/ul>\n<p>Based on the checklist that we prepared above and going by the&nbsp;<code>machine_learning_map<\/code>&nbsp;we can try out the below mentioned algorithms.<\/p>\n<ul>\n<li>Naive Bayes<\/li>\n<li>Linear SVC<\/li>\n<li>K-Neighbours Classifier<\/li>\n<\/ul>\n<p>The real beauty of the scikit-learn library is that it exposes high level APIs for different algorithms, making it easier for us to try out different algorithms and compare the accuracy of the models to see what works best for our data set.<\/p>\n<p>Let\u2019s begin trying out the different algorithms one by one.<\/p>\n<h3 id=\"naivebayes\">Naive-Bayes<\/h3>\n<p>Scikit-learn provides a set of classification algorithms which \u201cnaively\u201d assumes that in a data set every pair of features are independent. This assumption is the underlying principle of&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Bayes%27_theorem\" data-cke-saved-href=\"https:\/\/en.wikipedia.org\/wiki\/Bayes%27_theorem\">Bayes theorem<\/a>. The algorithms based on this principle are known as Naive-Bayes algorithms.<\/p>\n<p>On a very high level a Naive-Bayes algorithm calculates the probability of the connection of a feature with a target variable and then it selects the feature with the highest probability. Let\u2019s try to understand this with a very simple problem statement: Will it rain today? Suppose we have a set of weather data with us that will be our feature set, and the probability of \u2018Rain\u2019 will be our target. Based on this feature set we can create a table to show us the number of times a particular feature\/target pair occur. It would look something like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.stechstar.com\/user\/zbxe\/files\/attach\/images\/3263\/244\/089\/ecab41b204114faf8845b9a305d34a50.png\" alt=\"NB_occurancetable-1\" data-autoattach=\"success\" data-cke-saved-src=\".\/files\/attach\/images\/3263\/244\/089\/ecab41b204114faf8845b9a305d34a50.png\"><\/p>\n<p>In the table above the feature (column) \u2018Weather\u2019 contains the labels (\u2018Partially Cloudy\u2019 and \u2018Cloudy\u2019) and the column \u2018Rain\u2019 contains the occurrence of rain coinciding with the feature \u2018Weather\u2019 (Yes\/No). Whenever a feature lcoincides with rain, it\u2019s recorded as a \u2018Yes\u2019 and when the feature didn\u2019t lead to rain it is recorded as a \u2018No\u2019. We can now use the data from the occurrence table to create another table known as the \u2018Frequency table\u2019 where we can record the number of \u2018Yes\u2019 and the number of \u2018No\u2019 answers that each feature relates to:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.stechstar.com\/user\/zbxe\/files\/attach\/images\/3263\/244\/089\/1ad2716be416e7bf5b1cfbaa556a0bd3.png\" alt=\"NB-Frequency_Table\" data-autoattach=\"success\" data-cke-saved-src=\".\/files\/attach\/images\/3263\/244\/089\/1ad2716be416e7bf5b1cfbaa556a0bd3.png\"><\/p>\n<p>Finally, we combine the data from the \u2018occurrence table\u2019 and the \u2018frequency table\u2019 and create a \u2018likelihood table\u2019. This table lists the amount of \u2018Yes\u2019 and \u2018No\u2019 for each feature and then uses this data to calculate the probability of contibution of each feature towards the occurrence of rain:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.stechstar.com\/user\/zbxe\/files\/attach\/images\/3263\/244\/089\/c13177e7e2b3a3d60503c32e483293a8.png\" alt=\"NB-Probability_Table\" data-autoattach=\"success\" data-cke-saved-src=\".\/files\/attach\/images\/3263\/244\/089\/c13177e7e2b3a3d60503c32e483293a8.png\"><\/p>\n<p>Notice the \u2018Individual Probability\u2019 column in the table above. We had 6 occurrences of the features \u2018Partially Cloudy\u2019 and \u2018Cloudy\u2019 from the \u2018Occurrence table\u2019 and from the \u2018Likelihood table\u2019 it was clear that the feature \u2018Partially Cloudy\u2019 had 4 occurrences (2 for \u2018No\u2019 and 2 for \u2018yes\u2019). When we divide the number of occurrences of \u2018No\u2019 and \u2018Yes\u2019 of a particular feature with the \u2018total\u2019 of the \u2018occurrence table\u2019, we get the probability of that particular feature. In our case if we need to find out that which feature has the strongest probability of contributing to the occurrence of Rain then we take the total number of \u2018No\u2019 of each feature and add it to their respective number of \u2018Yes\u2019 from the \u2018frequency table\u2019 and then divide the sum with the \u2018Total\u2019 from the \u00f3ccurances table\u2019. This gives us the probability of each of these features coinciding with rain.<\/p>\n<p>The algorithm that we are going to use for our sales data is the&nbsp;<code>Gaussian Naive Bayes<\/code>&nbsp;and it is based on a concept similar to the weather example we just explored above, although significantly more mathematically complicated. A more detailed explanation of \u2018Naive-Bayes\u2019 algorithms can be found&nbsp;<a href=\"https:\/\/scikit-learn.org\/stable\/modules\/naive_bayes.html\" data-cke-saved-href=\"https:\/\/scikit-learn.org\/stable\/modules\/naive_bayes.html\">here<\/a>&nbsp;for those who wish to delve deeper.<\/p>\n<p>Now let\u2019s implement the Gaussian Naive Bayes or&nbsp;<code>GaussianNB<\/code>&nbsp;algorithm from scikit-learn to create our prediction model:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\"># import the necessary module<\/span>\n<span class=\"token keyword\">from<\/span> sklearn<span class=\"token punctuation\">.<\/span>naive_bayes <span class=\"token keyword\">import<\/span> GaussianNB\n<span class=\"token keyword\">from<\/span> sklearn<span class=\"token punctuation\">.<\/span>metrics <span class=\"token keyword\">import<\/span> accuracy_score\n<span class=\"token comment\" spellcheck=\"true\">#create an object of the type GaussianNB<\/span>\ngnb <span class=\"token operator\">=<\/span> GaussianNB<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\">#train the algorithm on training data and predict using the testing data<\/span>\npred <span class=\"token operator\">=<\/span> gnb<span class=\"token punctuation\">.<\/span>fit<span class=\"token punctuation\">(<\/span>data_train<span class=\"token punctuation\">,<\/span> target_train<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>predict<span class=\"token punctuation\">(<\/span>data_test<span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\">#print(pred.tolist())<\/span>\n<span class=\"token comment\" spellcheck=\"true\">#print the accuracy score of the model<\/span>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Naive-Bayes accuracy : \"<\/span><span class=\"token punctuation\">,<\/span>accuracy_score<span class=\"token punctuation\">(<\/span>target_test<span class=\"token punctuation\">,<\/span> pred<span class=\"token punctuation\">,<\/span> normalize <span class=\"token operator\">=<\/span> <span class=\"token boolean\">True<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<pre class=\"language-python\"><code>Naive<span class=\"token operator\">-<\/span>Bayes accuracy <span class=\"token punctuation\">:<\/span> <span class=\"token number\">0.759056732741<\/span><\/code><\/pre>\n<p>Now let\u2019s take a closer look at what we just did. First, we imported the&nbsp;<code>GaussianNB<\/code>&nbsp;method and the&nbsp;<code>accuracy_score<\/code>&nbsp;method. Then we created an object&nbsp;<code>gnb<\/code>&nbsp;of the type&nbsp;<code>GaussianNB<\/code>. After this, we trained the algorithm on the testing data(data_train) and testing target(target_train) using the&nbsp;<code>fit()<\/code>&nbsp;method, and then predicted the targets in the test data using the&nbsp;<code>predict()<\/code>&nbsp;method. Finally we printed the score using the&nbsp;<code>accuracy_score()<\/code>&nbsp;method and with this we have successfully applied the&nbsp;<code>Naive-Bayes<\/code>&nbsp;algorithm to build a prediction model.<\/p>\n<p>Now lets see how the other algorithms in our list perform as compared to the Naive-Bayes algorithm.<\/p>\n<h3 id=\"linearsvc\">LinearSVC<\/h3>\n<p>LinearSVC or Linear Support Vector Classification is a subclass of the&nbsp;<code>SVM<\/code>&nbsp;(Support Vector Machine) class. We won\u2019t go into the intricacies of the mathematics involved in this class of algorithms, but on a very basic level LinearSVC tries to divide the data into different planes so that it can find a best possible grouping of different classes. To get a clear understanding of this concept let\u2019s imagine a data set of \u2018dots\u2019 and \u2018squares\u2019 divided into a two dimensional space along two axis, as shown in the image below:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.stechstar.com\/user\/zbxe\/files\/attach\/images\/3263\/244\/089\/03d98a3a04b247ca6756f487e82eb6e4.png\" alt=\"SVM-1\" data-autoattach=\"success\" data-cke-saved-src=\".\/files\/attach\/images\/3263\/244\/089\/03d98a3a04b247ca6756f487e82eb6e4.png\"><br \/>\nSource:<a href=\"https:\/\/stats.stackexchange.com\/questions\/23391\/how-does-a-support-vector-machine-svm-work\" data-cke-saved-href=\"https:\/\/stats.stackexchange.com\/questions\/23391\/how-does-a-support-vector-machine-svm-work\">StackOverflow<\/a><\/p>\n<p>In the image above a&nbsp;<code>LinearSVC<\/code>&nbsp;implementation tries to divide the two-dimensional space in such a way that the two classes of data i.e the&nbsp;<code>dots<\/code>&nbsp;and&nbsp;<code>squares<\/code>&nbsp;are clearly divided. Here the two lines visually represent the various division that the&nbsp;<code>LinearSVC<\/code>&nbsp;tries to implement to separate out the two available classes.<\/p>\n<p>A very good writeup explaining a&nbsp;<code>Support Vector Machine(SVM)<\/code>&nbsp;can be found&nbsp;<a href=\"https:\/\/stats.stackexchange.com\/questions\/23391\/how-does-a-support-vector-machine-svm-work\" data-cke-saved-href=\"https:\/\/stats.stackexchange.com\/questions\/23391\/how-does-a-support-vector-machine-svm-work\">here<\/a>&nbsp;for those who\u2019d like more detail, but for now, let\u2019s just dive in and get our hands dirty:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\">#import the necessary modules<\/span>\n<span class=\"token keyword\">from<\/span> sklearn<span class=\"token punctuation\">.<\/span>svm <span class=\"token keyword\">import<\/span> LinearSVC\n<span class=\"token keyword\">from<\/span> sklearn<span class=\"token punctuation\">.<\/span>metrics <span class=\"token keyword\">import<\/span> accuracy_score\n<span class=\"token comment\" spellcheck=\"true\">#create an object of type LinearSVC<\/span>\nsvc_model <span class=\"token operator\">=<\/span> LinearSVC<span class=\"token punctuation\">(<\/span>random_state<span class=\"token operator\">=<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\">#train the algorithm on training data and predict using the testing data<\/span>\npred <span class=\"token operator\">=<\/span> svc_model<span class=\"token punctuation\">.<\/span>fit<span class=\"token punctuation\">(<\/span>data_train<span class=\"token punctuation\">,<\/span> target_train<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>predict<span class=\"token punctuation\">(<\/span>data_test<span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\">#print the accuracy score of the model<\/span>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"LinearSVC accuracy : \"<\/span><span class=\"token punctuation\">,<\/span>accuracy_score<span class=\"token punctuation\">(<\/span>target_test<span class=\"token punctuation\">,<\/span> pred<span class=\"token punctuation\">,<\/span> normalize <span class=\"token operator\">=<\/span> <span class=\"token boolean\">True<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<pre class=\"language-python\"><code>LinearSVC accuracy <span class=\"token punctuation\">:<\/span> <span class=\"token number\">0.777811004785<\/span><\/code><\/pre>\n<p>Similar to what we did during the implementation of GaussianNB, we imported the required modules in the first two lines. Then we created an object&nbsp;<code>svc_model<\/code>&nbsp;of type LinearSVC with random_state as \u20180\u2019. Hold on! What is a \u201crandom_state\u201d ? Simply put the&nbsp;<code>random_state<\/code>&nbsp;is an instruction to the built-in random number generator to shuffle the data in a specific order.<\/p>\n<p>Next, we trained the LinearSVC on the training data and then predicted the target using the test data. Finally, we checked the accuracy score using the&nbsp;<code>accuracy_score()<\/code>&nbsp;method.<\/p>\n<p>Now that we have tried out the&nbsp;<code>GaussianNB<\/code>&nbsp;and&nbsp;<code>LinearSVC<\/code>&nbsp;algorithms we will try out the last algorithm in our list and that\u2019s the&nbsp;<code>K-nearest neighbours classifier<\/code><\/p>\n<h3 id=\"kneighborsclassifier\">K-Neighbors Classifier<\/h3>\n<p>Compared to the previous two algorithms we\u2019ve worked with, this classifier is a bit more complex. For the purposes of this tutorial we are better off using the&nbsp;<code>KNeighborsClassifier<\/code>&nbsp;class provided by scikit-learn without worrying much about how the algorithm works. (But if you\u2019re interested, a very detailed explanation of this class of algorithms can be found&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/K-nearest_neighbors_algorithm\" data-cke-saved-href=\"https:\/\/en.wikipedia.org\/wiki\/K-nearest_neighbors_algorithm\">here<\/a>)<\/p>\n<p>Now, let\u2019s implement the K-Neighbors Classifier and see how it scores:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\" spellcheck=\"true\">#import necessary modules<\/span>\n<span class=\"token keyword\">from<\/span> sklearn<span class=\"token punctuation\">.<\/span>neighbors <span class=\"token keyword\">import<\/span> KNeighborsClassifier\n<span class=\"token keyword\">from<\/span> sklearn<span class=\"token punctuation\">.<\/span>metrics <span class=\"token keyword\">import<\/span> accuracy_score\n<span class=\"token comment\" spellcheck=\"true\">#create object of the lassifier<\/span>\nneigh <span class=\"token operator\">=<\/span> KNeighborsClassifier<span class=\"token punctuation\">(<\/span>n_neighbors<span class=\"token operator\">=<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\">#Train the algorithm<\/span>\nneigh<span class=\"token punctuation\">.<\/span>fit<span class=\"token punctuation\">(<\/span>data_train<span class=\"token punctuation\">,<\/span> target_train<span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\"># predict the response<\/span>\npred <span class=\"token operator\">=<\/span> neigh<span class=\"token punctuation\">.<\/span>predict<span class=\"token punctuation\">(<\/span>data_test<span class=\"token punctuation\">)<\/span>\n<span class=\"token comment\" spellcheck=\"true\"># evaluate accuracy<\/span>\n<span class=\"token keyword\">print<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token string\">\"KNeighbors accuracy score : \"<\/span><span class=\"token punctuation\">,<\/span>accuracy_score<span class=\"token punctuation\">(<\/span>target_test<span class=\"token punctuation\">,<\/span> pred<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<pre class=\"language-python\"><code>KNeighbors accuracy score <span class=\"token punctuation\">:<\/span> <span class=\"token number\">0.814550580998<\/span><\/code><\/pre>\n<p>The above code can be explained just like the previous implementations. First we imported the necessary modules, then we created the object&nbsp;<code>neigh<\/code>&nbsp;of type KNeighborsClassifier with the number of neighbors being&nbsp;<code>n_neighbors=3<\/code>. Then we used the&nbsp;<code>fit()<\/code>&nbsp;method to train our algorithm on the training set, then we tested the model on the test data. Finally, we printed out the accuracy score.<\/p>\n<p>Now that we have implemented all the algorithms in our list, we can simply compare the scores of all the models to select the model with the highest score. But wouldn\u2019t it be nice if we had a way to visually compare the performance of the different models? We can use the&nbsp;<a href=\"https:\/\/www.scikit-yb.org\/en\/latest\/tutorial.html#visual-model-evaluation\" data-cke-saved-href=\"https:\/\/www.scikit-yb.org\/en\/latest\/tutorial.html#visual-model-evaluation\"><code>yellowbrick<\/code><\/a>&nbsp;library in scikit-learn, which provides methods for visually representing different scoring methods.<\/p>\n<h2 id=\"performancecomparison\">Performance Comparison<\/h2>\n<p>In the previous sections we have used the&nbsp;<code>accuracy_score()<\/code>&nbsp;method to measure the accuracy of the different algorithms. Now, we will use the&nbsp;<code>ClassificationReport<\/code>&nbsp;class provided by the&nbsp;<code>Yellowbrick<\/code>&nbsp;library to give us a visual report of how our models perform.<\/p>\n<h3 id=\"gaussiannb\">GaussianNB<\/h3>\n<p>Let\u2019s start off with the&nbsp;<code>GaussianNB<\/code>&nbsp;model:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">from<\/span> yellowbrick<span class=\"token punctuation\">.<\/span>classifier <span class=\"token keyword\">import<\/span> ClassificationReport\n<span class=\"token comment\" spellcheck=\"true\"># Instantiate the classification model and visualizer<\/span>\nvisualizer <span class=\"token operator\">=<\/span> ClassificationReport<span class=\"token punctuation\">(<\/span>gnb<span class=\"token punctuation\">,<\/span> classes<span class=\"token operator\">=<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'Won'<\/span><span class=\"token punctuation\">,<\/span><span class=\"token string\">'Loss'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>\nvisualizer<span class=\"token punctuation\">.<\/span>fit<span class=\"token punctuation\">(<\/span>data_train<span class=\"token punctuation\">,<\/span> target_train<span class=\"token punctuation\">)<\/span> <span class=\"token comment\" spellcheck=\"true\"># Fit the training data to the visualizer<\/span>\nvisualizer<span class=\"token punctuation\">.<\/span>score<span class=\"token punctuation\">(<\/span>data_test<span class=\"token punctuation\">,<\/span> target_test<span class=\"token punctuation\">)<\/span> <span class=\"token comment\" spellcheck=\"true\"># Evaluate the model on the test data<\/span>\ng <span class=\"token operator\">=<\/span> visualizer<span class=\"token punctuation\">.<\/span>poof<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token comment\" spellcheck=\"true\"># Draw\/show\/poof the data<\/span><\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.stechstar.com\/user\/zbxe\/files\/attach\/images\/3263\/244\/089\/adc266ee1649b036346509f9f5f05a27.png\" alt=\"output_38_0\" data-autoattach=\"success\" data-cke-saved-src=\".\/files\/attach\/images\/3263\/244\/089\/adc266ee1649b036346509f9f5f05a27.png\"><\/p>\n<p>In the code above, first we import the&nbsp;<code>ClassificationReport<\/code>&nbsp;class provided by the&nbsp;<code>yellowbrick.classifier<\/code>&nbsp;module. Next, an object&nbsp;<code>visualizer<\/code>&nbsp;of the type&nbsp;<code>ClassificationReport<\/code>&nbsp;is created. Here the first argument is the&nbsp;<code>GaussianNB<\/code>&nbsp;object&nbsp;<code>gnb<\/code>&nbsp;that was created while implementing the&nbsp;<code>Naive-Bayes<\/code>&nbsp;algorithm in the \u2018Naive-Bayes\u2019 section. The second argument contains the labels \u2018Won\u2019 and \u2018Loss\u2019 from the \u2018Opportunity Result\u2019 column from the&nbsp;<code>sales_data<\/code>&nbsp;dataframe.<\/p>\n<p>Next, we use the&nbsp;<code>fit()<\/code>&nbsp;method to train the&nbsp;<code>visualizer<\/code>&nbsp;object. This is followed by the&nbsp;<code>score()<\/code>&nbsp;method, which uses&nbsp;<code>gnb<\/code>&nbsp;object to carry out predictions as per the&nbsp;<code>GaussianNB<\/code>&nbsp;algorithm and then calculate the accuracy score of the predictions made by this algorithm. Finally, we use the&nbsp;<code>poof()<\/code>&nbsp;method to draw a plot of the different scores for the&nbsp;<code>GaussianNB<\/code>&nbsp;algorithm. Notice how the different scores are laid out against each of the labels \u2018Won\u2019 and \u2018Loss\u2019; this enables us to visualize the scores across the different target classes.<\/p>\n<h3 id=\"linearsvc\">LinearSVC<\/h3>\n<p>Similar to what we just did in the previous section, we can also plot the accuracy scores of the&nbsp;<code>LinearSVC<\/code>&nbsp;algorithm:<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">from<\/span> yellowbrick<span class=\"token punctuation\">.<\/span>classifier <span class=\"token keyword\">import<\/span> ClassificationReport\n<span class=\"token comment\" spellcheck=\"true\"># Instantiate the classification model and visualizer<\/span>\nvisualizer <span class=\"token operator\">=<\/span> ClassificationReport<span class=\"token punctuation\">(<\/span>svc_model<span class=\"token punctuation\">,<\/span> classes<span class=\"token operator\">=<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'Won'<\/span><span class=\"token punctuation\">,<\/span><span class=\"token string\">'Loss'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>\nvisualizer<span class=\"token punctuation\">.<\/span>fit<span class=\"token punctuation\">(<\/span>data_train<span class=\"token punctuation\">,<\/span> target_train<span class=\"token punctuation\">)<\/span> <span class=\"token comment\" spellcheck=\"true\"># Fit the training data to the visualizer<\/span>\nvisualizer<span class=\"token punctuation\">.<\/span>score<span class=\"token punctuation\">(<\/span>data_test<span class=\"token punctuation\">,<\/span> target_test<span class=\"token punctuation\">)<\/span> <span class=\"token comment\" spellcheck=\"true\"># Evaluate the model on the test data<\/span>\ng <span class=\"token operator\">=<\/span> visualizer<span class=\"token punctuation\">.<\/span>poof<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token comment\" spellcheck=\"true\"># Draw\/show\/poof the data<\/span><\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.stechstar.com\/user\/zbxe\/files\/attach\/images\/3263\/244\/089\/679d5bea1efae89d00d108fbc23b7cbd.png\" alt=\"output_41_0\" data-autoattach=\"success\" data-cke-saved-src=\".\/files\/attach\/images\/3263\/244\/089\/679d5bea1efae89d00d108fbc23b7cbd.png\"><\/p>\n<p>In the code above, first we imported the&nbsp;<code>ClassificationReport<\/code>&nbsp;class provided by the&nbsp;<code>yellowbrick.classifier<\/code>&nbsp;module. Next, an object&nbsp;<code>visualizer<\/code>&nbsp;of the type&nbsp;<code>ClassificationReport<\/code>&nbsp;was created. Here the first argument is the&nbsp;<code>LinearSVC<\/code>&nbsp;object&nbsp;<code>svc_model<\/code>, that was created while implementing the&nbsp;<code>LinearSVC<\/code>&nbsp;algorithm in the \u2018LinearSVC\u2019 section. The second argument contains the labels \u2018Won\u2019 and \u2018Loss\u2019 from the \u2018Opportunity Result\u2019 column from the&nbsp;<code>sales_data<\/code>&nbsp;dataframe.<\/p>\n<p>Next, we used the&nbsp;<code>fit()<\/code>&nbsp;method to train the \u2018svc_model\u2019 object. This is followed by the&nbsp;<code>score()<\/code>&nbsp;method which uses the&nbsp;<code>svc_model<\/code>&nbsp;object to carry out predictions according to the&nbsp;<code>LinearSVC<\/code>&nbsp;algorithm and then calculate the accuracy score of the predictions made by this algorithm. Finally, we used the&nbsp;<code>poof()<\/code>&nbsp;method to draw a plot of the different scores for the&nbsp;<code>LinearSVC<\/code>&nbsp;algorithm.<\/p>\n<h3 id=\"kneighborsclassifier\">KNeighborsClassifier<\/h3>\n<p>Now, let\u2019s do the same thing for the K-Neighbors Classifier scores.<\/p>\n<pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">from<\/span> yellowbrick<span class=\"token punctuation\">.<\/span>classifier <span class=\"token keyword\">import<\/span> ClassificationReport\n<span class=\"token comment\" spellcheck=\"true\"># Instantiate the classification model and visualizer<\/span>\nvisualizer <span class=\"token operator\">=<\/span> ClassificationReport<span class=\"token punctuation\">(<\/span>neigh<span class=\"token punctuation\">,<\/span> classes<span class=\"token operator\">=<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'Won'<\/span><span class=\"token punctuation\">,<\/span><span class=\"token string\">'Loss'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>\nvisualizer<span class=\"token punctuation\">.<\/span>fit<span class=\"token punctuation\">(<\/span>data_train<span class=\"token punctuation\">,<\/span> target_train<span class=\"token punctuation\">)<\/span> <span class=\"token comment\" spellcheck=\"true\"># Fit the training data to the visualizer<\/span>\nvisualizer<span class=\"token punctuation\">.<\/span>score<span class=\"token punctuation\">(<\/span>data_test<span class=\"token punctuation\">,<\/span> target_test<span class=\"token punctuation\">)<\/span> <span class=\"token comment\" spellcheck=\"true\"># Evaluate the model on the test data<\/span>\ng <span class=\"token operator\">=<\/span> visualizer<span class=\"token punctuation\">.<\/span>poof<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token comment\" spellcheck=\"true\"># Draw\/show\/poof the data<\/span><\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.stechstar.com\/user\/zbxe\/files\/attach\/images\/3263\/244\/089\/b6cec720ead5a6cef7f3a4309edc75fa.png\" alt=\"output_43_0\" data-autoattach=\"success\" data-cke-saved-src=\".\/files\/attach\/images\/3263\/244\/089\/b6cec720ead5a6cef7f3a4309edc75fa.png\"><\/p>\n<p>Once again, we first import the&nbsp;<code>ClassificationReport<\/code>&nbsp;class provided by the&nbsp;<code>yellowbrick.classifier<\/code>&nbsp;module. Next, an object&nbsp;<code>visualizer<\/code>&nbsp;of the type&nbsp;<code>ClassificationReport<\/code>&nbsp;is created. Here the first argument is the&nbsp;<code>KNeighborsClassifier<\/code>&nbsp;object&nbsp;<code>neigh<\/code>, that was created while implementing the&nbsp;<code>KNeighborsClassifier<\/code>&nbsp;algorithm in the \u2018KNeighborsClassifier\u2019 section. The second argument contains the labels \u2018Won\u2019 and \u2018Loss\u2019 from the \u2018Opportunity Result\u2019 column from the&nbsp;<code>sales_data<\/code>&nbsp;dataframe.<\/p>\n<p>Next, we use the&nbsp;<code>fit()<\/code>&nbsp;method to train the \u2018neigh\u2019 object. This is followed by the&nbsp;<code>score()<\/code>&nbsp;method which uses the&nbsp;<code>neigh<\/code>&nbsp;object to carry out predictions according to the&nbsp;<code>KNeighborsClassifier<\/code>&nbsp;algorithm and then calculate the accuracy score of the predictions made by this algorithm. Finally we use the&nbsp;<code>poof()<\/code>&nbsp;method to draw a plot of the different scores for the&nbsp;<code>KNeighborsClassifier<\/code>&nbsp;algorithm.<\/p>\n<p>Now that we\u2019ve visualized the results, it\u2019s much easier for us to compare the scores and choose the algorithm that\u2019s going to work best for our needs.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>The scikit-learn library provides many different algorithms which can be imported into the code and then used to build models just like we would import any other Python library. This makes it easier to quickly build different models and compare these models to select the highest scoring one.<\/p>\n<p>In this tutorial, we have only scratched the surface of what is possible with the scikit-learn library. To use this Machine Learning library to the fullest, there are many resources available on the&nbsp;<a href=\"https:\/\/scikit-learn.org\/\" data-cke-saved-href=\"https:\/\/scikit-learn.org\/\">official page of scikit-learn<\/a>&nbsp;with detailed documentation that you can dive into. The quick start guide for scikit-learn can be found&nbsp;<a href=\"https:\/\/scikit-learn.org\/stable\/tutorial\/basic\/tutorial.html\" data-cke-saved-href=\"https:\/\/scikit-learn.org\/stable\/tutorial\/basic\/tutorial.html\">here<\/a>, and that\u2019s a good entry point for beginners who have just started exploring the world of Machine Learning.<\/p>\n<p>But to really appreciate the true power of the scikit-learn library, what you really need to do is start using it on different open data sets and building predictive models using these data sets. Sources for open data sets include&nbsp;<a href=\"https:\/\/www.kaggle.com\/\" data-cke-saved-href=\"https:\/\/www.kaggle.com\/\">Kaggle<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/data.world\/open-data-community\" data-cke-saved-href=\"https:\/\/data.world\/open-data-community\">Data.world<\/a>. Both contain many interesting data sets on which one can practice building predictive models by using the algorithms provided by the scikit-learn library.<\/p>\n<\/div>\n<\/div>\n<p>[\ucd9c\ucc98]&nbsp;https:\/\/www.dataquest.io\/blog\/sci-kit-learn-tutorial\/<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_9575\" class=\"pvc_stats all  \" data-element-id=\"9575\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/www.stechstar.com\/user\/wordpress\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>[python][\uba38\uc2e0\ub7ec\ub2dd] Scikit-learn Tutorial: Machine Learning in Python Scikit-learn Tutorial: Machine Learning in Python Scikit-learn is a free machine learning library for Python. It features various algorithms like support vector machine, random forests, and k-neighbours, and it also supports Python numerical and scientific libraries like&nbsp;NumPy&nbsp;and&nbsp;SciPy. In this tutorial we will learn to code python and apply [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_9575\" class=\"pvc_stats all  \" data-element-id=\"9575\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/www.stechstar.com\/user\/wordpress\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[15,21,20],"tags":[],"class_list":["post-9575","post","type-post","status-publish","format-standard","hentry","category-stechstar-com-","category-21","category-20"],"a3_pvc":{"activated":true,"total_views":295,"today_views":0},"_links":{"self":[{"href":"https:\/\/www.stechstar.com\/user\/wordpress\/wp-json\/wp\/v2\/posts\/9575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.stechstar.com\/user\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stechstar.com\/user\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stechstar.com\/user\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stechstar.com\/user\/wordpress\/wp-json\/wp\/v2\/comments?post=9575"}],"version-history":[{"count":1,"href":"https:\/\/www.stechstar.com\/user\/wordpress\/wp-json\/wp\/v2\/posts\/9575\/revisions"}],"predecessor-version":[{"id":9577,"href":"https:\/\/www.stechstar.com\/user\/wordpress\/wp-json\/wp\/v2\/posts\/9575\/revisions\/9577"}],"wp:attachment":[{"href":"https:\/\/www.stechstar.com\/user\/wordpress\/wp-json\/wp\/v2\/media?parent=9575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stechstar.com\/user\/wordpress\/wp-json\/wp\/v2\/categories?post=9575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stechstar.com\/user\/wordpress\/wp-json\/wp\/v2\/tags?post=9575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}