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", + " \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", + " \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", + " \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", + " \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", + " \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", + " \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", + " \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", + " \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", + " \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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Unnamed: 0monthdayFFMCDMCDCISItempRHwind...monthfebmonthjanmonthjulmonthjunmonthmarmonthmaymonthnovmonthoctmonthsepsize_category
01marfri86.226.294.35.18.2516.7...000010000small
12octtue90.635.4669.16.718.0330.9...000000010small
23octsat90.643.7686.96.714.6331.3...000000010small
34marfri91.733.377.59.08.3974.0...000010000small
45marsun89.351.3102.29.611.4991.8...000010000small
..................................................................
512513augsun81.656.7665.61.927.8322.7...000000000large
513514augsun81.656.7665.61.921.9715.8...000000000large
514515augsun81.656.7665.61.921.2706.7...000000000large
515516augsat94.4146.0614.711.325.6424.0...000000000small
516517novtue79.53.0106.71.111.8314.5...000000100small
\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 +}