-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37c6222
commit 7561525
Showing
1 changed file
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,264 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"name": "Copy of support_vector_regression.ipynb", | ||
"provenance": [], | ||
"collapsed_sections": [], | ||
"toc_visible": true, | ||
"include_colab_link": true | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "view-in-github", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"<a href=\"https://colab.research.google.com/github/amansoni24/Regression/blob/main/Copy_of_support_vector_regression.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "m3PAEPRDRLA3" | ||
}, | ||
"source": [ | ||
"# Support Vector Regression (SVR)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "0VCUAVIjRdzZ" | ||
}, | ||
"source": [ | ||
"## Importing the libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "56oRF-QfSDzC" | ||
}, | ||
"source": [ | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import pandas as pd" | ||
], | ||
"execution_count": 4, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "fXVXoFWtSF4_" | ||
}, | ||
"source": [ | ||
"## Importing the dataset" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "xfoa8OSORfHQ" | ||
}, | ||
"source": [ | ||
"dataset = pd.read_csv('Data.csv')\n", | ||
"X = dataset.iloc[:, :-1].values\n", | ||
"y = dataset.iloc[:, -1].values" | ||
], | ||
"execution_count": 5, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "_8Ny1GfPiV3m" | ||
}, | ||
"source": [ | ||
"y = y.reshape(len(y),1)" | ||
], | ||
"execution_count": 6, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "6Vk3nNlrCVCN" | ||
}, | ||
"source": [ | ||
"## Splitting the dataset into the Training set and Test set" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "x_fXqrziCV3_" | ||
}, | ||
"source": [ | ||
"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.2, random_state = 0)" | ||
], | ||
"execution_count": 7, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "YS8FeLHYS-nI" | ||
}, | ||
"source": [ | ||
"## Feature Scaling" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "PGeAlD1HTDI1" | ||
}, | ||
"source": [ | ||
"from sklearn.preprocessing import StandardScaler\n", | ||
"sc_X = StandardScaler()\n", | ||
"sc_y = StandardScaler()\n", | ||
"X_train = sc_X.fit_transform(X_train)\n", | ||
"y_train = sc_y.fit_transform(y_train)" | ||
], | ||
"execution_count": 8, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "eiU6D2QFRjxY" | ||
}, | ||
"source": [ | ||
"## Training the SVR model on the Training set" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "y6R4rt_GRz15", | ||
"outputId": "21dcbd44-743a-47d6-d02a-39b34b5af75f", | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
} | ||
}, | ||
"source": [ | ||
"from sklearn.svm import SVR\n", | ||
"regressor = SVR(kernel = 'rbf')\n", | ||
"regressor.fit(X_train, y_train)" | ||
], | ||
"execution_count": 9, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": [ | ||
"/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py:760: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n", | ||
" y = column_or_1d(y, warn=True)\n" | ||
], | ||
"name": "stderr" | ||
}, | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='scale',\n", | ||
" kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)" | ||
] | ||
}, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"execution_count": 9 | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "aPYA5W1pDBOE" | ||
}, | ||
"source": [ | ||
"## Predicting the Test set results" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "vSqFVDYrDROW", | ||
"outputId": "9f10a8af-1407-4e51-f338-baab8d46bfab", | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
} | ||
}, | ||
"source": [ | ||
"y_pred = sc_y.inverse_transform(regressor.predict(sc_X.transform(X_test)))\n", | ||
"np.set_printoptions(precision=2)\n", | ||
"print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))" | ||
], | ||
"execution_count": 10, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": [ | ||
"[[434.05 431.23]\n", | ||
" [457.94 460.01]\n", | ||
" [461.03 461.14]\n", | ||
" ...\n", | ||
" [470.6 473.26]\n", | ||
" [439.42 438. ]\n", | ||
" [460.92 463.28]]\n" | ||
], | ||
"name": "stdout" | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "CMsYlps2DX1d" | ||
}, | ||
"source": [ | ||
"## Evaluating the Model Performance" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "HxsRWlURDr6S", | ||
"outputId": "9e671c8f-26c6-4107-b7b8-6775dfa8d4f6", | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
} | ||
}, | ||
"source": [ | ||
"from sklearn.metrics import r2_score\n", | ||
"r2_score(y_test, y_pred)" | ||
], | ||
"execution_count": 11, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"0.9480784049986258" | ||
] | ||
}, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"execution_count": 11 | ||
} | ||
] | ||
} | ||
] | ||
} |