{"id":29475,"date":"2022-06-30T08:56:36","date_gmt":"2022-06-30T08:56:36","guid":{"rendered":"https:\/\/www.askpython.com\/?p=29475"},"modified":"2023-02-16T19:56:42","modified_gmt":"2023-02-16T19:56:42","slug":"support-vector-machines-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/support-vector-machines-in-python","title":{"rendered":"Support Vector Machines in Python"},"content":{"rendered":"\n<p>When it comes to the implementation of Machine Learning algorithms, the list starts from linear regression to decision trees. They are of various types. Mainly when it comes to separations of data points along a linear axis the recommendations from the experts are: <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/k-means-clustering-from-scratch\" data-type=\"post\" data-id=\"11673\"><strong>K-means clustering.<\/strong><\/a><\/li><li><strong><strong>Support Vector Machines.<\/strong><\/strong><\/li><\/ol>\n\n\n\n<p>As we all know that an ML model is of two types: <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python\/supervised-vs-unsupervised-learning\" data-type=\"post\" data-id=\"19538\">Supervised Learning<\/a>: Needs a guide to select the input data from the programmer.<\/li><li><a href=\"https:\/\/www.askpython.com\/python\/machine-learning-introduction\" data-type=\"post\" data-id=\"22853\">Unsupervised Learning<\/a>:  Needs no guide to select the input data. It&#8217;s a learn itself model.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">General Theory<\/h2>\n\n\n\n<p>The main aim of this article is to make the reader aware of how the technique of SVM works. On the internet, data is available in raw. So, when we structure the data and visualize it, the results are either a <strong>discrete or continuous distribution<\/strong>. According to this, SVMs are used for two purposes:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Classification: <em>For discrete data parameters.<\/em><\/li><li>Regression: <em>For Continuous data parameters.<\/em><\/li><\/ol>\n\n\n\n<p>This is one of the main reasons why Support Vector Machines are highly used for classification and regression purposes. The definition says that: <strong>Support Vector Machines are a set of learning algorithms that help us classify and analyze the nature of data.<\/strong> <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Components of SVM<\/h4>\n\n\n\n<ol class=\"has-ast-global-color-8-color has-ast-global-color-5-background-color has-text-color has-background wp-block-list\"><li>Support vectors: <em>These are the main components. They are simple data points that lie on both sides of the Maximum margin.<\/em><\/li><li>Maximum margin: <em>The maximum limit till the data classification takes place.<\/em><\/li><li>Maximum margin hyperplane: <em>The maximum mid-limit margin that lies between the positive and negative hyperplanes.<\/em><\/li><li>Positive hyperplane: <em>Right side of the margin plane.<\/em><\/li><li>Negative hyperplane: <em>Left side of the margin plane.<\/em><\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Diagram<\/h4>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"572\" height=\"402\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/SVM-diagram-simplified-1.png\" alt=\"SVM Diagram Simplified 1\" class=\"wp-image-29762\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/SVM-diagram-simplified-1.png 572w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/SVM-diagram-simplified-1-300x211.png 300w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><figcaption>SVM Diagram Simplified <\/figcaption><\/figure>\n\n\n\n<p>In this diagram, we can clearly see that the main margin is separating all the different data points according to the color. We have used the color for showing their nature of difference. The main aim of SVM is to show the distinction and classify each point with the best possible marginal line.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example and applications<\/h4>\n\n\n\n<p>Suppose we have a class: <strong>Vehicle<\/strong>. <strong>Our task is to fetch the Sports Utility Vehicle (SUV) from that class. <\/strong>Now there are various other types. Now, when we try to arrange them manually in order then it may take a lot of time. This also creates some errors. So, to make the classification more stable we can create a Support Vector Machine that will classify all the models of cars from the parent vehicle class. It shall work on the following steps: <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The model will take a sample image.<\/li><li>Then it compares it with the test data of vehicle types provided already. <\/li><li>After that, it tells us which type of model of the car is there in that input image.<\/li><li>No other algorithm can make things simpler than an SVM.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Implementing Support Vector Machines<\/h2>\n\n\n\n<p>In this section, we shall implement all the necessary implementation for the Support Vector Machine. So, let&#8217;s get started!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Environment details:<\/h4>\n\n\n\n<ol class=\"wp-block-list\"><li><em>Python 3.9.7<\/em><\/li><li><em>IDE: Jupyter Notebooks<\/em><\/li><li><em>Environment: Anaconda 3<\/em><\/li><li><em>Dataset: Cancer dataset (cell_samples.csv)<\/em><\/li><\/ol>\n\n\n\n<p>Importing the necessary libraries for data reading and preprocessing<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom warnings import filterwarnings\nfilterwarnings(&quot;ignore&quot;)\n<\/pre><\/div>\n\n\n<p>Reading the dataset<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncancer_data = pd.read_csv(&quot;Datasets\/cell_samples.csv&quot;, sep = &quot;,&quot;)\ncancer_data.head()\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"938\" height=\"210\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Viewing-the-dataset.png\" alt=\"Viewing The Dataset\" class=\"wp-image-29771\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Viewing-the-dataset.png 938w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Viewing-the-dataset-300x67.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Viewing-the-dataset-768x172.png 768w\" sizes=\"auto, (max-width: 938px) 100vw, 938px\" \/><figcaption>Viewing The Dataset<\/figcaption><\/figure>\n\n\n\n<p>Checking for null values<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncancer_Data.isna().sum()\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"157\" height=\"236\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/No-null-values.png\" alt=\"No Null Values\" class=\"wp-image-29773\"\/><figcaption>No Null Values<\/figcaption><\/figure>\n\n\n\n<p>Getting the general info about the dataset<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;The shape of the dataset is: &quot;, cancer_data.shape)\nprint(&quot;The size of the dataset is: &quot;, cancer_data.size, &quot; bytes\\n&quot;)\nprint(&quot;The count of each attribute of the dataset is: \\n&quot;)\nprint(cancer_data.count())\nprint(&quot;\\nThe datatype of each attribute is: \\n&quot;)\nprint(cancer_data.dtypes)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nThe shape of the dataset is:  (699, 11)\nThe size of the dataset is:  7689  bytes\n\nThe count of each attribute of the dataset is: \n\nID             699\nClump          699\nUnifSize       699\nUnifShape      699\nMargAdh        699\nSingEpiSize    699\nBareNuc        699\nBlandChrom     699\nNormNucl       699\nMit            699\nClass          699\ndtype: int64\n\nThe datatype of each attribute is: \n\nID              int64\nClump           int64\nUnifSize        int64\nUnifShape       int64\nMargAdh         int64\nSingEpiSize     int64\nBareNuc        object\nBlandChrom      int64\nNormNucl        int64\nMit             int64\nClass           int64\ndtype: object\n<\/pre><\/div>\n\n\n<p><strong>Converting the BareNuc column into integer type<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncancer_data = cancer_data&#x5B;pd.to_numeric(cancer_data&#x5B;&quot;BareNuc&quot;], errors = &quot;coerce&quot;).notnull()]\ncancer_data&#x5B;&quot;BareNuc&quot;] = cancer_data&#x5B;&quot;BareNuc&quot;].astype(&quot;int&quot;)\ncancer_data.dtypes\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nID             int64\nClump          int64\nUnifSize       int64\nUnifShape      int64\nMargAdh        int64\nSingEpiSize    int64\nBareNuc        int32\nBlandChrom     int64\nNormNucl       int64\nMit            int64\nClass          int64\ndtype: object\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">Separating the two classes from the data frame<\/h4>\n\n\n\n<p><strong>For cancer cells type classification we have two types of cells for classification:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Malignant: value = 4 in our dataset<\/li><li>Benign: value = 2 in our dataset<\/li><\/ol>\n\n\n\n<p>We create two separate data frames of the same names. Then, try to classify them using data visualization techniques. Taking only the first fifty value from the core dataset. This makes plotting easier.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmalignant = cancer_data&#x5B;cancer_data&#x5B;&quot;Class&quot;] == 4]&#x5B;0:50]\nbenign = cancer_data&#x5B;cancer_data&#x5B;&quot;Class&quot;] == 2]&#x5B;0:50]\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nplt.figure(figsize = (10, 5))\nax = plt.axes()\nax.set_facecolor(&quot;white&quot;)\nplt.title(&quot;Separating the data points - Clump and UniformShape&quot;)\nplt.scatter(malignant&#x5B;&quot;Clump&quot;], malignant&#x5B;&quot;UnifShape&quot;] , color = &quot;red&quot;, marker = &quot;*&quot;)\nplt.scatter(benign&#x5B;&quot;Clump&quot;], benign&#x5B;&quot;UnifShape&quot;], color = &quot;green&quot;, marker = &quot;+&quot;)\nplt.legend(&#x5B;&quot;Malignant cell class&quot;, &quot;Benign cell class&quot;])\nplt.show()\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"374\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Scatter-plots.png\" alt=\"Scatter Plots\" class=\"wp-image-29777\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Scatter-plots.png 601w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Scatter-plots-300x187.png 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><figcaption>Scatter Plots<\/figcaption><\/figure>\n\n\n\n<p>Creating independent and dependent data column lists with their numpy arrays:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndependent_data = cancer_data&#x5B;&#x5B;&quot;ID&quot;, &quot;Class&quot;]]\nindependent_data = cancer_data&#x5B;&#x5B;&#039;Clump&#039;, &#039;UnifSize&#039;, &#039;UnifShape&#039;, &#039;MargAdh&#039;, &#039;SingEpiSize&#039;,\n       &#039;BareNuc&#039;, &#039;BlandChrom&#039;, &#039;NormNucl&#039;, &#039;Mit&#039;]]\n\nX_data = np.array(independent_data)\nX_data&#x5B;0:5]\n\nY_data = np.array(dependent_data&#x5B;&quot;Class&quot;])\nY_data&#x5B;0:5]\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narray(&#x5B;&#x5B; 5,  1,  1,  1,  2,  1,  3,  1,  1],\n       &#x5B; 5,  4,  4,  5,  7, 10,  3,  2,  1],\n       &#x5B; 3,  1,  1,  1,  2,  2,  3,  1,  1],\n       &#x5B; 6,  8,  8,  1,  3,  4,  3,  7,  1],\n       &#x5B; 4,  1,  1,  3,  2,  1,  3,  1,  1]], dtype=int64)\n\narray(&#x5B;2, 2, 2, 2, 2], dtype=int64)\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">Splitting the data into train and test variables<\/h4>\n\n\n\n<p>From the sklearn.model_selection import the <a href=\"https:\/\/www.askpython.com\/python\/examples\/split-data-training-and-testing-set\" data-type=\"post\" data-id=\"9234\">train_test_split function<\/a>. This splits the data into four arrays:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>X_train<\/li><li>X_test<\/li><li>y_train<\/li><li>y_test<\/li><\/ol>\n\n\n\n<p>Out of these the training arrays are tow dimensional and the testing arrays are one dimensional. Just remember to take the test_size = 0.2 as we need only 20 percent of the total dataset to test our model accuracy.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X_data, Y_data, test_size = 0.2, random_state = 4)\nprint(X_train.shape)\nprint(y_train.shape)\nprint(X_test.shape)\nprint(y_test.shape)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n(546, 9)\n(546,)\n(137, 9)\n(137,)\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">Importing the SVM from sklearn and creating a classifier instance<\/h4>\n\n\n\n<p>First we import the model and then we import SVC. It is the classifier class for separating the support vectors. Create an instance &#8220;Classify&#8221;. Give the kernel value as &#8220;linear&#8221; it will linearly separate the support vectors. Then we fit the X_train data and Y_train data inside the model using the fit() function. After that create an instance &#8220;y_predict&#8221;, which holds all the predictions in a one-dimensional array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom sklearn import svm\nclassify = svm.SVC(kernel = &quot;linear&quot;)\nClassify.fit(X_train, y_train)\ny_predict = Classify.predict(X_test)\nprint(y_predict)\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narray(&#x5B;2, 4, 2, 4, 2, 2, 2, 2, 4, 2, 2, 4, 4, 4, 4, 2, 2, 2, 2, 2, 4, 2,\n       4, 4, 4, 4, 2, 2, 4, 4, 4, 2, 4, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4,\n       4, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 4, 4, 2, 4, 4,\n       4, 2, 2, 2, 4, 4, 2, 2, 2, 4, 2, 2, 4, 4, 2, 2, 2, 2, 4, 4, 2, 4,\n       2, 2, 4, 4, 2, 2, 2, 4, 2, 2, 2, 4, 2, 4, 2, 2, 4, 2, 4, 2, 2, 4,\n       2, 2, 4, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 2, 2, 4, 2, 2, 4, 2, 4, 2,\n       2, 2, 2, 2, 4], dtype=int64)\n<\/pre><\/div>\n\n\n<p>So, we have successfully separated all the cancerous patients with the noncancerous ones. The cells having 4 as value are cancerous and with that 2 are noncancerous. Now, that we have got the predictions we can run them against our Y_test array to check how accurate the model is. For that we can prepare a classification report.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Preparing the classification report<\/h4>\n\n\n\n<p>For this, we need to import the classification_report function from the sklearn.metrics module. Then call it inside the print() function. we test it with our Y_test array and the results are as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom sklearn.metrics import classification_report\nprint(classification_report(y_test, y_predict))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n                   precision    recall  f1-score   support\n\n           2       1.00           0.94      0.97        90\n           4       0.90           1.00      0.95        47\n\naccuracy                           0.96       137\nmacro avg       0.95      0.97      0.96       137\nweighted avg       0.97      0.96      0.96       137\n\n<\/pre><\/div>\n\n\n<p>As the result says the precision of the model is very good. For <strong>malignant class (value  = 4)<\/strong>: The precision score is: 100%. For the <strong>benign class (value = 2)<\/strong> the precision score is: 90%<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>So, in this way we have successfully implemented the Support Vector Machines using Python and built a predictive model from the given input data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When it comes to the implementation of Machine Learning algorithms, the list starts from linear regression to decision trees. They are of various types. Mainly when it comes to separations of data points along a linear axis the recommendations from the experts are: K-means clustering. Support Vector Machines. As we all know that an ML [&hellip;]<\/p>\n","protected":false},"author":36,"featured_media":29801,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-29475","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/29475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=29475"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/29475\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/29801"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=29475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=29475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=29475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}