Files
fenago f3b24b4b7f added
2021-02-07 15:16:01 +05:00

347 lines
6.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "RCQKxdZOjw_2"
},
"outputs": [],
"source": [
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.preprocessing import StandardScaler\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.metrics import mean_squared_error\n",
"import altair as alt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "tm5mPWzJkRLO"
},
"outputs": [],
"source": [
"file_url = '../Dataset/phpYYZ4Qc.csv'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "FEC78ZbAj3Vb"
},
"outputs": [],
"source": [
"df = pd.read_csv(file_url)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 214
},
"colab_type": "code",
"id": "YMQiyk9IoSTr",
"outputId": "ecd53b58-0628-4d56-be59-c8effd01e6a5"
},
"outputs": [],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "IxiwVfiJq8KL"
},
"outputs": [],
"source": [
"y = df.pop('rej')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 302
},
"colab_type": "code",
"id": "sAKI1Aq8tkPF",
"outputId": "64feb05f-deb8-4cc9-8850-af292d83a983"
},
"outputs": [],
"source": [
"df.describe()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "ifR4fTCIrJBe"
},
"outputs": [],
"source": [
"X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=0.3, random_state=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "14KemFv6t-PZ"
},
"outputs": [],
"source": [
"scaler = StandardScaler()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "P5gQ7yqBuIIw"
},
"outputs": [],
"source": [
"X_train = scaler.fit_transform(X_train)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "_Tq7gmTaud86"
},
"outputs": [],
"source": [
"X_test = scaler.transform(X_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "fDhW9UiFl-AE"
},
"outputs": [],
"source": [
"lr_model = LinearRegression()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"colab_type": "code",
"id": "A22jb3xUoZvX",
"outputId": "66bc4c30-15d8-4598-f184-3a903b7274af"
},
"outputs": [],
"source": [
"lr_model.fit(X_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "arae5xgLrlcO"
},
"outputs": [],
"source": [
"preds_train = lr_model.predict(X_train)\n",
"preds_test = lr_model.predict(X_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"colab_type": "code",
"id": "0L0vYPIOri1M",
"outputId": "7715bc22-0359-45b2-e9af-8547279c44ca"
},
"outputs": [],
"source": [
"train_mse = mean_squared_error(y_train, preds_train)\n",
"train_mse "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"colab_type": "code",
"id": "4qIcCT_7rwrQ",
"outputId": "98b8f444-6382-4f47-d1ae-0a917fc1416a"
},
"outputs": [],
"source": [
"test_mse = mean_squared_error(y_test, preds_test)\n",
"test_mse"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 159
},
"colab_type": "code",
"id": "lUuLmYo8r0JK",
"outputId": "0de7ecbf-2bf8-4381-a474-7a11fdd33eae"
},
"outputs": [],
"source": [
"lr_model.coef_"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "qYJAfNV4s120"
},
"outputs": [],
"source": [
"coef_df = pd.DataFrame()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "_updjbMss5js"
},
"outputs": [],
"source": [
"coef_df['feature'] = df.columns"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "dN4nrheJtAAg"
},
"outputs": [],
"source": [
"coef_df['coefficient'] = lr_model.coef_"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 194
},
"colab_type": "code",
"id": "F7YMpWyFtE5B",
"outputId": "eac4b816-30f1-4312-ca7d-6ac845940452"
},
"outputs": [],
"source": [
"coef_df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 702
},
"colab_type": "code",
"id": "LxFxUajHs2Ge",
"outputId": "c344d4df-8d73-4d31-8e5e-8b0e24a379d4"
},
"outputs": [],
"source": [
"alt.Chart(coef_df).mark_bar().encode(\n",
" x='coefficient',\n",
" y=\"feature\"\n",
")"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "Exercise9_01.ipynb",
"provenance": []
},
"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.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 1
}