Skip to content

Instantly share code, notes, and snippets.

@Prathmeshp20
Created October 18, 2021 17:41
Show Gist options
  • Save Prathmeshp20/a8a057a52deb3a5f2cb2da14fc18ebea to your computer and use it in GitHub Desktop.
Save Prathmeshp20/a8a057a52deb3a5f2cb2da14fc18ebea to your computer and use it in GitHub Desktop.
Assignment17-SVM-FireForests.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import pandas as pd\nimport numpy as np\nfrom sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer\nfrom sklearn.preprocessing import StandardScaler\n\nfrom sklearn import svm\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.metrics import classification_report\n\n\nfrom sklearn.metrics import accuracy_score, confusion_matrix\nfrom sklearn.model_selection import train_test_split, cross_val_score\n",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "forestfires = pd.read_csv(\"C:/Users/Prathmesh/Downloads/forestfires.csv\")",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "forestfires.head()",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": " month day FFMC DMC DC ISI temp RH wind rain ... monthfeb \\\n0 mar fri 86.2 26.2 94.3 5.1 8.2 51 6.7 0.0 ... 0 \n1 oct tue 90.6 35.4 669.1 6.7 18.0 33 0.9 0.0 ... 0 \n2 oct sat 90.6 43.7 686.9 6.7 14.6 33 1.3 0.0 ... 0 \n3 mar fri 91.7 33.3 77.5 9.0 8.3 97 4.0 0.2 ... 0 \n4 mar sun 89.3 51.3 102.2 9.6 11.4 99 1.8 0.0 ... 0 \n\n monthjan monthjul monthjun monthmar monthmay monthnov monthoct \\\n0 0 0 0 1 0 0 0 \n1 0 0 0 0 0 0 1 \n2 0 0 0 0 0 0 1 \n3 0 0 0 1 0 0 0 \n4 0 0 0 1 0 0 0 \n\n monthsep size_category \n0 0 small \n1 0 small \n2 0 small \n3 0 small \n4 0 small \n\n[5 rows x 31 columns]",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>month</th>\n <th>day</th>\n <th>FFMC</th>\n <th>DMC</th>\n <th>DC</th>\n <th>ISI</th>\n <th>temp</th>\n <th>RH</th>\n <th>wind</th>\n <th>rain</th>\n <th>...</th>\n <th>monthfeb</th>\n <th>monthjan</th>\n <th>monthjul</th>\n <th>monthjun</th>\n <th>monthmar</th>\n <th>monthmay</th>\n <th>monthnov</th>\n <th>monthoct</th>\n <th>monthsep</th>\n <th>size_category</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>mar</td>\n <td>fri</td>\n <td>86.2</td>\n <td>26.2</td>\n <td>94.3</td>\n <td>5.1</td>\n <td>8.2</td>\n <td>51</td>\n <td>6.7</td>\n <td>0.0</td>\n <td>...</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>1</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>small</td>\n </tr>\n <tr>\n <th>1</th>\n <td>oct</td>\n <td>tue</td>\n <td>90.6</td>\n <td>35.4</td>\n <td>669.1</td>\n <td>6.7</td>\n <td>18.0</td>\n <td>33</td>\n <td>0.9</td>\n <td>0.0</td>\n <td>...</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>1</td>\n <td>0</td>\n <td>small</td>\n </tr>\n <tr>\n <th>2</th>\n <td>oct</td>\n <td>sat</td>\n <td>90.6</td>\n <td>43.7</td>\n <td>686.9</td>\n <td>6.7</td>\n <td>14.6</td>\n <td>33</td>\n <td>1.3</td>\n <td>0.0</td>\n <td>...</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>1</td>\n <td>0</td>\n <td>small</td>\n </tr>\n <tr>\n <th>3</th>\n <td>mar</td>\n <td>fri</td>\n <td>91.7</td>\n <td>33.3</td>\n <td>77.5</td>\n <td>9.0</td>\n <td>8.3</td>\n <td>97</td>\n <td>4.0</td>\n <td>0.2</td>\n <td>...</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>1</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>small</td>\n </tr>\n <tr>\n <th>4</th>\n <td>mar</td>\n <td>sun</td>\n <td>89.3</td>\n <td>51.3</td>\n <td>102.2</td>\n <td>9.6</td>\n <td>11.4</td>\n <td>99</td>\n <td>1.8</td>\n <td>0.0</td>\n <td>...</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>1</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>0</td>\n <td>small</td>\n </tr>\n </tbody>\n</table>\n<p>5 rows × 31 columns</p>\n</div>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = forestfires.iloc[:,2:30]\ny = forestfires.iloc[:,30]",
"execution_count": 8,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x_train, x_test, y_train, y_test = train_test_split(x,y, test_size = 0.3)",
"execution_count": 11,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x_train.shape, y_train.shape, x_test.shape, y_test.shape",
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 15,
"data": {
"text/plain": "((361, 28), (361,), (156, 28), (156,))"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "clf = SVC()\nparam_grid = [{'kernel':['rbf'],'gamma':[50,5,10,0.5],'C':[15,14,13,12,11,10,0.1,0.001] }]\ngsv = GridSearchCV(clf,param_grid,cv=10)\ngsv.fit(x_train,y_train)",
"execution_count": 25,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 25,
"data": {
"text/plain": "GridSearchCV(cv=10, estimator=SVC(),\n param_grid=[{'C': [15, 14, 13, 12, 11, 10, 0.1, 0.001],\n 'gamma': [50, 5, 10, 0.5], 'kernel': ['rbf']}])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gsv.best_params_ , gsv.best_score_ ",
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 26,
"data": {
"text/plain": "({'C': 15, 'gamma': 0.5, 'kernel': 'rbf'}, 0.734009009009009)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "clf = SVC(C= 15, gamma = 50)\nclf.fit(x_train , y_train)\ny_pred = clf.predict(x_test)\nacc = accuracy_score(y_test, y_pred) * 100\nprint(\"Accuracy =\", acc)\nconfusion_matrix(y_test, y_pred)",
"execution_count": 27,
"outputs": [
{
"output_type": "stream",
"text": "Accuracy = 76.28205128205127\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 27,
"data": {
"text/plain": "array([[ 0, 37],\n [ 0, 119]], dtype=int64)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.8.5",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "Assignment17-SVM-FireForests.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment