From 9ce84d791090812a1c724997e6359780b8c9c327 Mon Sep 17 00:00:00 2001
From: jaydipkumar pipariya <58799320+jaydipkumar@users.noreply.github.com>
Date: Thu, 30 Apr 2020 15:35:26 +0530
Subject: [PATCH] Add files via upload
---
SVM.ipynb | 506 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 506 insertions(+)
create mode 100644 SVM.ipynb
diff --git a/SVM.ipynb b/SVM.ipynb
new file mode 100644
index 0000000..11ce4d3
--- /dev/null
+++ b/SVM.ipynb
@@ -0,0 +1,506 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 33,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# importing required libraries \n",
+ "import numpy as np \n",
+ "import pandas as pd \n",
+ "import matplotlib.pyplot as plt "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 34,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " Unnamed: 0 | \n",
+ " month | \n",
+ " day | \n",
+ " FFMC | \n",
+ " DMC | \n",
+ " DC | \n",
+ " ISI | \n",
+ " temp | \n",
+ " RH | \n",
+ " wind | \n",
+ " ... | \n",
+ " monthfeb | \n",
+ " monthjan | \n",
+ " monthjul | \n",
+ " monthjun | \n",
+ " monthmar | \n",
+ " monthmay | \n",
+ " monthnov | \n",
+ " monthoct | \n",
+ " monthsep | \n",
+ " size_category | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " 0 | \n",
+ " 1 | \n",
+ " mar | \n",
+ " fri | \n",
+ " 86.2 | \n",
+ " 26.2 | \n",
+ " 94.3 | \n",
+ " 5.1 | \n",
+ " 8.2 | \n",
+ " 51 | \n",
+ " 6.7 | \n",
+ " ... | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 1 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " small | \n",
+ "
\n",
+ " \n",
+ " 1 | \n",
+ " 2 | \n",
+ " oct | \n",
+ " tue | \n",
+ " 90.6 | \n",
+ " 35.4 | \n",
+ " 669.1 | \n",
+ " 6.7 | \n",
+ " 18.0 | \n",
+ " 33 | \n",
+ " 0.9 | \n",
+ " ... | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 1 | \n",
+ " 0 | \n",
+ " small | \n",
+ "
\n",
+ " \n",
+ " 2 | \n",
+ " 3 | \n",
+ " oct | \n",
+ " sat | \n",
+ " 90.6 | \n",
+ " 43.7 | \n",
+ " 686.9 | \n",
+ " 6.7 | \n",
+ " 14.6 | \n",
+ " 33 | \n",
+ " 1.3 | \n",
+ " ... | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 1 | \n",
+ " 0 | \n",
+ " small | \n",
+ "
\n",
+ " \n",
+ " 3 | \n",
+ " 4 | \n",
+ " mar | \n",
+ " fri | \n",
+ " 91.7 | \n",
+ " 33.3 | \n",
+ " 77.5 | \n",
+ " 9.0 | \n",
+ " 8.3 | \n",
+ " 97 | \n",
+ " 4.0 | \n",
+ " ... | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 1 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " small | \n",
+ "
\n",
+ " \n",
+ " 4 | \n",
+ " 5 | \n",
+ " mar | \n",
+ " sun | \n",
+ " 89.3 | \n",
+ " 51.3 | \n",
+ " 102.2 | \n",
+ " 9.6 | \n",
+ " 11.4 | \n",
+ " 99 | \n",
+ " 1.8 | \n",
+ " ... | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 1 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " small | \n",
+ "
\n",
+ " \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ "
\n",
+ " \n",
+ " 512 | \n",
+ " 513 | \n",
+ " aug | \n",
+ " sun | \n",
+ " 81.6 | \n",
+ " 56.7 | \n",
+ " 665.6 | \n",
+ " 1.9 | \n",
+ " 27.8 | \n",
+ " 32 | \n",
+ " 2.7 | \n",
+ " ... | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " large | \n",
+ "
\n",
+ " \n",
+ " 513 | \n",
+ " 514 | \n",
+ " aug | \n",
+ " sun | \n",
+ " 81.6 | \n",
+ " 56.7 | \n",
+ " 665.6 | \n",
+ " 1.9 | \n",
+ " 21.9 | \n",
+ " 71 | \n",
+ " 5.8 | \n",
+ " ... | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " large | \n",
+ "
\n",
+ " \n",
+ " 514 | \n",
+ " 515 | \n",
+ " aug | \n",
+ " sun | \n",
+ " 81.6 | \n",
+ " 56.7 | \n",
+ " 665.6 | \n",
+ " 1.9 | \n",
+ " 21.2 | \n",
+ " 70 | \n",
+ " 6.7 | \n",
+ " ... | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " large | \n",
+ "
\n",
+ " \n",
+ " 515 | \n",
+ " 516 | \n",
+ " aug | \n",
+ " sat | \n",
+ " 94.4 | \n",
+ " 146.0 | \n",
+ " 614.7 | \n",
+ " 11.3 | \n",
+ " 25.6 | \n",
+ " 42 | \n",
+ " 4.0 | \n",
+ " ... | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " small | \n",
+ "
\n",
+ " \n",
+ " 516 | \n",
+ " 517 | \n",
+ " nov | \n",
+ " tue | \n",
+ " 79.5 | \n",
+ " 3.0 | \n",
+ " 106.7 | \n",
+ " 1.1 | \n",
+ " 11.8 | \n",
+ " 31 | \n",
+ " 4.5 | \n",
+ " ... | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " 1 | \n",
+ " 0 | \n",
+ " 0 | \n",
+ " small | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
517 rows × 32 columns
\n",
+ "
"
+ ],
+ "text/plain": [
+ " Unnamed: 0 month day FFMC DMC DC ISI temp RH wind ... \\\n",
+ "0 1 mar fri 86.2 26.2 94.3 5.1 8.2 51 6.7 ... \n",
+ "1 2 oct tue 90.6 35.4 669.1 6.7 18.0 33 0.9 ... \n",
+ "2 3 oct sat 90.6 43.7 686.9 6.7 14.6 33 1.3 ... \n",
+ "3 4 mar fri 91.7 33.3 77.5 9.0 8.3 97 4.0 ... \n",
+ "4 5 mar sun 89.3 51.3 102.2 9.6 11.4 99 1.8 ... \n",
+ ".. ... ... ... ... ... ... ... ... .. ... ... \n",
+ "512 513 aug sun 81.6 56.7 665.6 1.9 27.8 32 2.7 ... \n",
+ "513 514 aug sun 81.6 56.7 665.6 1.9 21.9 71 5.8 ... \n",
+ "514 515 aug sun 81.6 56.7 665.6 1.9 21.2 70 6.7 ... \n",
+ "515 516 aug sat 94.4 146.0 614.7 11.3 25.6 42 4.0 ... \n",
+ "516 517 nov tue 79.5 3.0 106.7 1.1 11.8 31 4.5 ... \n",
+ "\n",
+ " monthfeb monthjan monthjul monthjun monthmar monthmay monthnov \\\n",
+ "0 0 0 0 0 1 0 0 \n",
+ "1 0 0 0 0 0 0 0 \n",
+ "2 0 0 0 0 0 0 0 \n",
+ "3 0 0 0 0 1 0 0 \n",
+ "4 0 0 0 0 1 0 0 \n",
+ ".. ... ... ... ... ... ... ... \n",
+ "512 0 0 0 0 0 0 0 \n",
+ "513 0 0 0 0 0 0 0 \n",
+ "514 0 0 0 0 0 0 0 \n",
+ "515 0 0 0 0 0 0 0 \n",
+ "516 0 0 0 0 0 0 1 \n",
+ "\n",
+ " monthoct monthsep size_category \n",
+ "0 0 0 small \n",
+ "1 1 0 small \n",
+ "2 1 0 small \n",
+ "3 0 0 small \n",
+ "4 0 0 small \n",
+ ".. ... ... ... \n",
+ "512 0 0 large \n",
+ "513 0 0 large \n",
+ "514 0 0 large \n",
+ "515 0 0 small \n",
+ "516 0 0 small \n",
+ "\n",
+ "[517 rows x 32 columns]"
+ ]
+ },
+ "execution_count": 34,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# reading csv file and extracting class column to y. \n",
+ "forestfires = pd.read_csv(\"~/Downloads/Data Science/data set/forestfires.csv\")\n",
+ "forestfires"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 35,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#drop unnecessary columan\n",
+ "forestfires.drop(['month','day','Unnamed: 0'], axis=1, inplace=True)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 36,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#feture selection\n",
+ "y = forestfires.size_category\n",
+ "forestfires.drop(['size_category'], axis=1, inplace=True)\n",
+ "X = forestfires"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 37,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#convert category to int\n",
+ "y.replace('small',0,regex=True, inplace = True)\n",
+ "y.replace('large',1,regex=True, inplace = True)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 38,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#Splitting Data\n",
+ "\n",
+ "from sklearn.model_selection import train_test_split \n",
+ "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4,random_state=109)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 39,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from sklearn import svm\n",
+ "#create a classifier\n",
+ "cls = svm.SVC(kernel=\"linear\")\n",
+ "#train the model\n",
+ "cls.fit(X_train,y_train)\n",
+ "#predict the response\n",
+ "pred = cls.predict(X_test)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 40,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "acuracy: 1.0\n",
+ "precision: 1.0\n",
+ "recall 1.0\n",
+ " precision recall f1-score support\n",
+ "\n",
+ " 0 1.00 1.00 1.00 150\n",
+ " 1 1.00 1.00 1.00 57\n",
+ "\n",
+ " accuracy 1.00 207\n",
+ " macro avg 1.00 1.00 1.00 207\n",
+ "weighted avg 1.00 1.00 1.00 207\n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Evaluating the Model\n",
+ "from sklearn import metrics\n",
+ "#accuracy\n",
+ "print(\"acuracy:\", metrics.accuracy_score(y_test,y_pred=pred))\n",
+ "#precision score\n",
+ "print(\"precision:\", metrics.precision_score(y_test,y_pred=pred))\n",
+ "#recall score\n",
+ "print(\"recall\" , metrics.recall_score(y_test,y_pred=pred))\n",
+ "print(metrics.classification_report(y_test, y_pred=pred))"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.6.8"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 4
+}