{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Crop_Yield_Prediction_MLR.ipynb","provenance":[],"collapsed_sections":[],"authorship_tag":"ABX9TyONwS0T7Li3k/T3RKX692yJ"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","metadata":{"id":"R5QjBRaAyC_H"},"source":["# Multiple Linear Regression\n"]},{"cell_type":"markdown","metadata":{"id":"lIMTzfBSy8Vh"},"source":["## Importing the libraries"]},{"cell_type":"code","metadata":{"id":"rN03-Fe5mUhY","executionInfo":{"status":"ok","timestamp":1628941880039,"user_tz":-480,"elapsed":3,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}}},"source":["import numpy as np\n","import matplotlib.pyplot as plt\n","import pandas as pd"],"execution_count":8,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"aePtTVsdp2V9","executionInfo":{"status":"ok","timestamp":1628941880375,"user_tz":-480,"elapsed":5,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"4ae0b567-a46e-4393-948b-d8691d128033"},"source":["from google.colab import drive\n","drive.mount('/content/drive')"],"execution_count":9,"outputs":[{"output_type":"stream","text":["Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"Sx5g6jHGzE2_"},"source":["## Importing the dataset"]},{"cell_type":"code","metadata":{"id":"YL7Nm_VNnDiG","executionInfo":{"status":"ok","timestamp":1628941881076,"user_tz":-480,"elapsed":704,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}}},"source":["dataset = pd.read_csv('/content/drive/MyDrive/Omdena/Local - Malaysia/Malaysia Chapter/Improving Food Security and Crop Yield in Malaysia with Machine Learning/Task 4 - Crop Yield Prediction/Dataset/data_crop_yield.csv')\n","X = dataset.iloc[:, :-1].values\n","y = dataset.iloc[:, -1].values"],"execution_count":10,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":414},"id":"u2Q5VuTxjoX2","executionInfo":{"status":"ok","timestamp":1628941881077,"user_tz":-480,"elapsed":21,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"d4dc63e0-487a-4e52-94e2-c7aa360d4c85"},"source":["dataset"],"execution_count":11,"outputs":[{"output_type":"execute_result","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","
CropPrecipitation (mm day-1)Specific Humidity at 2 Meters (g/kg)Relative Humidity at 2 Meters (%)Temperature at 2 Meters (C)Yield
0Cocoa, beans2248.9217.7283.4026.0111560
1Cocoa, beans1938.4217.5482.1126.1111253
2Cocoa, beans2301.5417.8182.7926.249456
3Cocoa, beans2592.3517.6185.0725.569321
4Cocoa, beans2344.7217.6184.1225.768800
.....................
151Rubber, natural2308.5118.2783.6526.476721
152Rubber, natural2410.1318.5883.4526.816248
153Rubber, natural2967.4118.6785.4826.466842
154Rubber, natural2333.4618.5084.8526.435571
155Rubber, natural2109.3418.5183.5226.725903
\n","

156 rows × 6 columns

\n","
"],"text/plain":[" Crop ... Yield\n","0 Cocoa, beans ... 11560\n","1 Cocoa, beans ... 11253\n","2 Cocoa, beans ... 9456\n","3 Cocoa, beans ... 9321\n","4 Cocoa, beans ... 8800\n",".. ... ... ...\n","151 Rubber, natural ... 6721\n","152 Rubber, natural ... 6248\n","153 Rubber, natural ... 6842\n","154 Rubber, natural ... 5571\n","155 Rubber, natural ... 5903\n","\n","[156 rows x 6 columns]"]},"metadata":{"tags":[]},"execution_count":11}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Dk-0FTL8rUNx","executionInfo":{"status":"ok","timestamp":1628941881078,"user_tz":-480,"elapsed":19,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"e3cb3362-1a72-4c5d-993a-8a2dfaac58ce"},"source":["dataset.dtypes"],"execution_count":12,"outputs":[{"output_type":"execute_result","data":{"text/plain":["Crop object\n","Precipitation (mm day-1) float64\n","Specific Humidity at 2 Meters (g/kg) float64\n","Relative Humidity at 2 Meters (%) float64\n","Temperature at 2 Meters (C) float64\n","Yield int64\n","dtype: object"]},"metadata":{"tags":[]},"execution_count":12}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":294},"id":"00JKPC2LzVs3","executionInfo":{"status":"ok","timestamp":1628941881078,"user_tz":-480,"elapsed":16,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"fd55233a-f0da-4854-d674-a2ba90b6689b"},"source":["dataset.describe()"],"execution_count":13,"outputs":[{"output_type":"execute_result","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","
Precipitation (mm day-1)Specific Humidity at 2 Meters (g/kg)Relative Humidity at 2 Meters (%)Temperature at 2 Meters (C)Yield
count156.000000156.000000156.000000156.00000156.000000
mean2486.49897418.20307784.73769226.1835956153.096154
std289.4579140.2939230.9962260.2610570421.958897
min1934.62000017.54000082.11000025.560005249.000000
25%2302.99000018.03000084.12000026.020008327.750000
50%2424.55000018.27000084.85000026.1300018871.000000
75%2718.08000018.40000085.51000026.3000067518.750000
max3085.79000018.70000086.10000026.81000203399.000000
\n","
"],"text/plain":[" Precipitation (mm day-1) ... Yield\n","count 156.000000 ... 156.000000\n","mean 2486.498974 ... 56153.096154\n","std 289.457914 ... 70421.958897\n","min 1934.620000 ... 5249.000000\n","25% 2302.990000 ... 8327.750000\n","50% 2424.550000 ... 18871.000000\n","75% 2718.080000 ... 67518.750000\n","max 3085.790000 ... 203399.000000\n","\n","[8 rows x 5 columns]"]},"metadata":{"tags":[]},"execution_count":13}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"8UuKznwlqBAZ","executionInfo":{"status":"ok","timestamp":1628941881078,"user_tz":-480,"elapsed":14,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"9dbbecbe-a4f9-4a69-d9c5-07421faca5e2"},"source":["X"],"execution_count":14,"outputs":[{"output_type":"execute_result","data":{"text/plain":["array([['Cocoa, beans', 2248.92, 17.72, 83.4, 26.01],\n"," ['Cocoa, beans', 1938.42, 17.54, 82.11, 26.11],\n"," ['Cocoa, beans', 2301.54, 17.81, 82.79, 26.24],\n"," ['Cocoa, beans', 2592.35, 17.61, 85.07, 25.56],\n"," ['Cocoa, beans', 2344.72, 17.61, 84.12, 25.76],\n"," ['Cocoa, beans', 2339.3, 17.7, 84.54, 25.76],\n"," ['Cocoa, beans', 2326.09, 18.09, 84.63, 26.11],\n"," ['Cocoa, beans', 2718.08, 18.3, 85.43, 26.12],\n"," ['Cocoa, beans', 2061.61, 17.8, 84.36, 25.88],\n"," ['Cocoa, beans', 1934.62, 17.94, 83.43, 26.21],\n"," ['Cocoa, beans', 2217.12, 18.03, 84.39, 26.1],\n"," ['Cocoa, beans', 2249.7, 18.01, 84.24, 26.11],\n"," ['Cocoa, beans', 2530.96, 18.15, 85.33, 26.02],\n"," ['Cocoa, beans', 2504.7, 18.19, 85.45, 26.02],\n"," ['Cocoa, beans', 2686.2, 18.38, 85.77, 26.13],\n"," ['Cocoa, beans', 2414.79, 18.18, 85.76, 25.93],\n"," ['Cocoa, beans', 1999.53, 18.2, 84.36, 26.27],\n"," ['Cocoa, beans', 2362.8, 18.7, 84.03, 26.79],\n"," ['Cocoa, beans', 2792.95, 18.24, 86.04, 25.95],\n"," ['Cocoa, beans', 2751.24, 18.31, 85.98, 26.02],\n"," ['Cocoa, beans', 2646.28, 18.4, 86.02, 26.1],\n"," ['Cocoa, beans', 2302.99, 18.22, 84.48, 26.25],\n"," ['Cocoa, beans', 2692.34, 18.37, 85.54, 26.18],\n"," ['Cocoa, beans', 2424.55, 18.28, 85.16, 26.18],\n"," ['Cocoa, beans', 2363.63, 18.35, 84.92, 26.28],\n"," ['Cocoa, beans', 2892.78, 18.49, 86.09, 26.18],\n"," ['Cocoa, beans', 2729.53, 18.41, 86.03, 26.11],\n"," ['Cocoa, beans', 3085.79, 18.34, 86.1, 26.03],\n"," ['Cocoa, beans', 2922.18, 18.43, 85.51, 26.23],\n"," ['Cocoa, beans', 2546.33, 18.43, 84.23, 26.49],\n"," ['Cocoa, beans', 2938.29, 18.22, 85.42, 26.06],\n"," ['Cocoa, beans', 2771.73, 18.38, 84.98, 26.3],\n"," ['Cocoa, beans', 2607.96, 18.37, 84.67, 26.35],\n"," ['Cocoa, beans', 2604.59, 18.19, 83.44, 26.43],\n"," ['Cocoa, beans', 2308.51, 18.27, 83.65, 26.47],\n"," ['Cocoa, beans', 2410.13, 18.58, 83.45, 26.81],\n"," ['Cocoa, beans', 2967.41, 18.67, 85.48, 26.46],\n"," ['Cocoa, beans', 2333.46, 18.5, 84.85, 26.43],\n"," ['Cocoa, beans', 2109.34, 18.51, 83.52, 26.72],\n"," ['Oil palm fruit', 2248.92, 17.72, 83.4, 26.01],\n"," ['Oil palm fruit', 1938.42, 17.54, 82.11, 26.11],\n"," ['Oil palm fruit', 2301.54, 17.81, 82.79, 26.24],\n"," ['Oil palm fruit', 2592.35, 17.61, 85.07, 25.56],\n"," ['Oil palm fruit', 2344.72, 17.61, 84.12, 25.76],\n"," ['Oil palm fruit', 2339.3, 17.7, 84.54, 25.76],\n"," ['Oil palm fruit', 2326.09, 18.09, 84.63, 26.11],\n"," ['Oil palm fruit', 2718.08, 18.3, 85.43, 26.12],\n"," ['Oil palm fruit', 2061.61, 17.8, 84.36, 25.88],\n"," ['Oil palm fruit', 1934.62, 17.94, 83.43, 26.21],\n"," ['Oil palm fruit', 2217.12, 18.03, 84.39, 26.1],\n"," ['Oil palm fruit', 2249.7, 18.01, 84.24, 26.11],\n"," ['Oil palm fruit', 2530.96, 18.15, 85.33, 26.02],\n"," ['Oil palm fruit', 2504.7, 18.19, 85.45, 26.02],\n"," ['Oil palm fruit', 2686.2, 18.38, 85.77, 26.13],\n"," ['Oil palm fruit', 2414.79, 18.18, 85.76, 25.93],\n"," ['Oil palm fruit', 1999.53, 18.2, 84.36, 26.27],\n"," ['Oil palm fruit', 2362.8, 18.7, 84.03, 26.79],\n"," ['Oil palm fruit', 2792.95, 18.24, 86.04, 25.95],\n"," ['Oil palm fruit', 2751.24, 18.31, 85.98, 26.02],\n"," ['Oil palm fruit', 2646.28, 18.4, 86.02, 26.1],\n"," ['Oil palm fruit', 2302.99, 18.22, 84.48, 26.25],\n"," ['Oil palm fruit', 2692.34, 18.37, 85.54, 26.18],\n"," ['Oil palm fruit', 2424.55, 18.28, 85.16, 26.18],\n"," ['Oil palm fruit', 2363.63, 18.35, 84.92, 26.28],\n"," ['Oil palm fruit', 2892.78, 18.49, 86.09, 26.18],\n"," ['Oil palm fruit', 2729.53, 18.41, 86.03, 26.11],\n"," ['Oil palm fruit', 3085.79, 18.34, 86.1, 26.03],\n"," ['Oil palm fruit', 2922.18, 18.43, 85.51, 26.23],\n"," ['Oil palm fruit', 2546.33, 18.43, 84.23, 26.49],\n"," ['Oil palm fruit', 2938.29, 18.22, 85.42, 26.06],\n"," ['Oil palm fruit', 2771.73, 18.38, 84.98, 26.3],\n"," ['Oil palm fruit', 2607.96, 18.37, 84.67, 26.35],\n"," ['Oil palm fruit', 2604.59, 18.19, 83.44, 26.43],\n"," ['Oil palm fruit', 2308.51, 18.27, 83.65, 26.47],\n"," ['Oil palm fruit', 2410.13, 18.58, 83.45, 26.81],\n"," ['Oil palm fruit', 2967.41, 18.67, 85.48, 26.46],\n"," ['Oil palm fruit', 2333.46, 18.5, 84.85, 26.43],\n"," ['Oil palm fruit', 2109.34, 18.51, 83.52, 26.72],\n"," ['Rice, paddy', 2248.92, 17.72, 83.4, 26.01],\n"," ['Rice, paddy', 1938.42, 17.54, 82.11, 26.11],\n"," ['Rice, paddy', 2301.54, 17.81, 82.79, 26.24],\n"," ['Rice, paddy', 2592.35, 17.61, 85.07, 25.56],\n"," ['Rice, paddy', 2344.72, 17.61, 84.12, 25.76],\n"," ['Rice, paddy', 2339.3, 17.7, 84.54, 25.76],\n"," ['Rice, paddy', 2326.09, 18.09, 84.63, 26.11],\n"," ['Rice, paddy', 2718.08, 18.3, 85.43, 26.12],\n"," ['Rice, paddy', 2061.61, 17.8, 84.36, 25.88],\n"," ['Rice, paddy', 1934.62, 17.94, 83.43, 26.21],\n"," ['Rice, paddy', 2217.12, 18.03, 84.39, 26.1],\n"," ['Rice, paddy', 2249.7, 18.01, 84.24, 26.11],\n"," ['Rice, paddy', 2530.96, 18.15, 85.33, 26.02],\n"," ['Rice, paddy', 2504.7, 18.19, 85.45, 26.02],\n"," ['Rice, paddy', 2686.2, 18.38, 85.77, 26.13],\n"," ['Rice, paddy', 2414.79, 18.18, 85.76, 25.93],\n"," ['Rice, paddy', 1999.53, 18.2, 84.36, 26.27],\n"," ['Rice, paddy', 2362.8, 18.7, 84.03, 26.79],\n"," ['Rice, paddy', 2792.95, 18.24, 86.04, 25.95],\n"," ['Rice, paddy', 2751.24, 18.31, 85.98, 26.02],\n"," ['Rice, paddy', 2646.28, 18.4, 86.02, 26.1],\n"," ['Rice, paddy', 2302.99, 18.22, 84.48, 26.25],\n"," ['Rice, paddy', 2692.34, 18.37, 85.54, 26.18],\n"," ['Rice, paddy', 2424.55, 18.28, 85.16, 26.18],\n"," ['Rice, paddy', 2363.63, 18.35, 84.92, 26.28],\n"," ['Rice, paddy', 2892.78, 18.49, 86.09, 26.18],\n"," ['Rice, paddy', 2729.53, 18.41, 86.03, 26.11],\n"," ['Rice, paddy', 3085.79, 18.34, 86.1, 26.03],\n"," ['Rice, paddy', 2922.18, 18.43, 85.51, 26.23],\n"," ['Rice, paddy', 2546.33, 18.43, 84.23, 26.49],\n"," ['Rice, paddy', 2938.29, 18.22, 85.42, 26.06],\n"," ['Rice, paddy', 2771.73, 18.38, 84.98, 26.3],\n"," ['Rice, paddy', 2607.96, 18.37, 84.67, 26.35],\n"," ['Rice, paddy', 2604.59, 18.19, 83.44, 26.43],\n"," ['Rice, paddy', 2308.51, 18.27, 83.65, 26.47],\n"," ['Rice, paddy', 2410.13, 18.58, 83.45, 26.81],\n"," ['Rice, paddy', 2967.41, 18.67, 85.48, 26.46],\n"," ['Rice, paddy', 2333.46, 18.5, 84.85, 26.43],\n"," ['Rice, paddy', 2109.34, 18.51, 83.52, 26.72],\n"," ['Rubber, natural', 2248.92, 17.72, 83.4, 26.01],\n"," ['Rubber, natural', 1938.42, 17.54, 82.11, 26.11],\n"," ['Rubber, natural', 2301.54, 17.81, 82.79, 26.24],\n"," ['Rubber, natural', 2592.35, 17.61, 85.07, 25.56],\n"," ['Rubber, natural', 2344.72, 17.61, 84.12, 25.76],\n"," ['Rubber, natural', 2339.3, 17.7, 84.54, 25.76],\n"," ['Rubber, natural', 2326.09, 18.09, 84.63, 26.11],\n"," ['Rubber, natural', 2718.08, 18.3, 85.43, 26.12],\n"," ['Rubber, natural', 2061.61, 17.8, 84.36, 25.88],\n"," ['Rubber, natural', 1934.62, 17.94, 83.43, 26.21],\n"," ['Rubber, natural', 2217.12, 18.03, 84.39, 26.1],\n"," ['Rubber, natural', 2249.7, 18.01, 84.24, 26.11],\n"," ['Rubber, natural', 2530.96, 18.15, 85.33, 26.02],\n"," ['Rubber, natural', 2504.7, 18.19, 85.45, 26.02],\n"," ['Rubber, natural', 2686.2, 18.38, 85.77, 26.13],\n"," ['Rubber, natural', 2414.79, 18.18, 85.76, 25.93],\n"," ['Rubber, natural', 1999.53, 18.2, 84.36, 26.27],\n"," ['Rubber, natural', 2362.8, 18.7, 84.03, 26.79],\n"," ['Rubber, natural', 2792.95, 18.24, 86.04, 25.95],\n"," ['Rubber, natural', 2751.24, 18.31, 85.98, 26.02],\n"," ['Rubber, natural', 2646.28, 18.4, 86.02, 26.1],\n"," ['Rubber, natural', 2302.99, 18.22, 84.48, 26.25],\n"," ['Rubber, natural', 2692.34, 18.37, 85.54, 26.18],\n"," ['Rubber, natural', 2424.55, 18.28, 85.16, 26.18],\n"," ['Rubber, natural', 2363.63, 18.35, 84.92, 26.28],\n"," ['Rubber, natural', 2892.78, 18.49, 86.09, 26.18],\n"," ['Rubber, natural', 2729.53, 18.41, 86.03, 26.11],\n"," ['Rubber, natural', 3085.79, 18.34, 86.1, 26.03],\n"," ['Rubber, natural', 2922.18, 18.43, 85.51, 26.23],\n"," ['Rubber, natural', 2546.33, 18.43, 84.23, 26.49],\n"," ['Rubber, natural', 2938.29, 18.22, 85.42, 26.06],\n"," ['Rubber, natural', 2771.73, 18.38, 84.98, 26.3],\n"," ['Rubber, natural', 2607.96, 18.37, 84.67, 26.35],\n"," ['Rubber, natural', 2604.59, 18.19, 83.44, 26.43],\n"," ['Rubber, natural', 2308.51, 18.27, 83.65, 26.47],\n"," ['Rubber, natural', 2410.13, 18.58, 83.45, 26.81],\n"," ['Rubber, natural', 2967.41, 18.67, 85.48, 26.46],\n"," ['Rubber, natural', 2333.46, 18.5, 84.85, 26.43],\n"," ['Rubber, natural', 2109.34, 18.51, 83.52, 26.72]], dtype=object)"]},"metadata":{"tags":[]},"execution_count":14}]},{"cell_type":"markdown","metadata":{"id":"8IkU-2mNztoR"},"source":["## Encoding categorical data"]},{"cell_type":"code","metadata":{"id":"S_CKi_rlqZ9t","executionInfo":{"status":"ok","timestamp":1628941881480,"user_tz":-480,"elapsed":411,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}}},"source":["from sklearn.compose import ColumnTransformer\n","from sklearn.preprocessing import OneHotEncoder\n","ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [0])], remainder='passthrough')\n","X = np.array(ct.fit_transform(X))"],"execution_count":15,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"SCVMHxv9rIrU","executionInfo":{"status":"ok","timestamp":1628941881481,"user_tz":-480,"elapsed":13,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"b61e21ab-aea9-4568-b5a3-742c849adf32"},"source":["X"],"execution_count":16,"outputs":[{"output_type":"execute_result","data":{"text/plain":["array([[1.0, 0.0, 0.0, ..., 17.72, 83.4, 26.01],\n"," [1.0, 0.0, 0.0, ..., 17.54, 82.11, 26.11],\n"," [1.0, 0.0, 0.0, ..., 17.81, 82.79, 26.24],\n"," ...,\n"," [0.0, 0.0, 0.0, ..., 18.67, 85.48, 26.46],\n"," [0.0, 0.0, 0.0, ..., 18.5, 84.85, 26.43],\n"," [0.0, 0.0, 0.0, ..., 18.51, 83.52, 26.72]], dtype=object)"]},"metadata":{"tags":[]},"execution_count":16}]},{"cell_type":"markdown","metadata":{"id":"8rkYhIE0z4SL"},"source":["## Feature Scaling"]},{"cell_type":"code","metadata":{"id":"VFoTVxApvn34","executionInfo":{"status":"ok","timestamp":1628941881481,"user_tz":-480,"elapsed":5,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}}},"source":["y = y.reshape(-1,1)"],"execution_count":17,"outputs":[]},{"cell_type":"code","metadata":{"id":"VZsm-HcyvVoC","executionInfo":{"status":"ok","timestamp":1628941881813,"user_tz":-480,"elapsed":336,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}}},"source":["from sklearn.preprocessing import StandardScaler\n","sc_X = StandardScaler()\n","sc_y = StandardScaler()\n","X = sc_X.fit_transform(X)\n","y = sc_y.fit_transform(y)"],"execution_count":18,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"nXw71vqOz-BJ"},"source":["## Splitting the dataset into the Training set and Test set"]},{"cell_type":"code","metadata":{"id":"PSUPbmfhxM_j","executionInfo":{"status":"ok","timestamp":1628941881813,"user_tz":-480,"elapsed":33,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}}},"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":19,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Upx-psgM0HXI"},"source":["## Training the Multiple Linear Regression model on the Training set"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Yre9-at3zUq0","executionInfo":{"status":"ok","timestamp":1628941881813,"user_tz":-480,"elapsed":32,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"356c905f-2cff-41ff-b364-97fb35ac8416"},"source":["from sklearn.linear_model import LinearRegression\n","regressor = LinearRegression()\n","regressor.fit(X_train, y_train)"],"execution_count":20,"outputs":[{"output_type":"execute_result","data":{"text/plain":["LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)"]},"metadata":{"tags":[]},"execution_count":20}]},{"cell_type":"markdown","metadata":{"id":"6JPVSuzl0N6c"},"source":["## Predicting the Test set results"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"JLuBqwt9zWhn","executionInfo":{"status":"ok","timestamp":1628941881814,"user_tz":-480,"elapsed":21,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"6f519dcc-f42a-48cb-903b-ce025714877f"},"source":["y_pred = regressor.predict(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":21,"outputs":[{"output_type":"stream","text":["[[-0.68 -0.62]\n"," [ 1.72 1.53]\n"," [-0.67 -0.66]\n"," [-0.32 -0.23]\n"," [-0.68 -0.68]\n"," [-0.64 -0.66]\n"," [ 1.67 1.57]\n"," [-0.28 -0.28]\n"," [-0.69 -0.71]\n"," [ 1.69 2.07]\n"," [-0.38 -0.37]\n"," [ 1.68 1.65]\n"," [-0.3 -0.27]\n"," [-0.71 -0.66]\n"," [ 1.76 1.23]\n"," [-0.65 -0.66]\n"," [-0.59 -0.69]\n"," [-0.71 -0.67]\n"," [ 1.66 1.62]\n"," [-0.72 -0.71]\n"," [-0.63 -0.68]\n"," [-0.73 -0.67]\n"," [-0.69 -0.69]\n"," [ 1.71 1.6 ]\n"," [-0.26 -0.37]\n"," [-0.65 -0.71]\n"," [-0.3 -0.44]\n"," [ 1.67 1.77]\n"," [-0.39 -0.42]\n"," [-0.42 -0.36]\n"," [-0.76 -0.69]\n"," [-0.66 -0.67]]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"ZT52QzFdoVMj","executionInfo":{"status":"ok","timestamp":1628941881814,"user_tz":-480,"elapsed":19,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"4b759914-8b17-4893-a0f2-7265cd483fd8"},"source":["sc_y.inverse_transform(regressor.predict(sc_X.transform([[1.0, 0.0, 0.0, 0.0, 2729.53, 18.41, 86.03, 26.11]])))"],"execution_count":22,"outputs":[{"output_type":"execute_result","data":{"text/plain":["array([[8709.12]])"]},"metadata":{"tags":[]},"execution_count":22}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Gt_B80VAwnNJ","executionInfo":{"status":"ok","timestamp":1628941881814,"user_tz":-480,"elapsed":16,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"bc831d59-4a18-4793-ae63-78ae5befd302"},"source":["sc_y.inverse_transform(y_pred)"],"execution_count":23,"outputs":[{"output_type":"execute_result","data":{"text/plain":["array([[ 8709.12],\n"," [176613.59],\n"," [ 9341.76],\n"," [ 33656.63],\n"," [ 8154.52],\n"," [ 11459.53],\n"," [173449. ],\n"," [ 36705.54],\n"," [ 7383.65],\n"," [174920.66],\n"," [ 29672.19],\n"," [174175.52],\n"," [ 34879.03],\n"," [ 6124.29],\n"," [179923.83],\n"," [ 10488.3 ],\n"," [ 14728.46],\n"," [ 6026.61],\n"," [172949.67],\n"," [ 5389.96],\n"," [ 12113.71],\n"," [ 4690.26],\n"," [ 7565.5 ],\n"," [176277.7 ],\n"," [ 38215.68],\n"," [ 10589.67],\n"," [ 34946.76],\n"," [173512.64],\n"," [ 28446.34],\n"," [ 26558.76],\n"," [ 3071.54],\n"," [ 9749.02]])"]},"metadata":{"tags":[]},"execution_count":23}]},{"cell_type":"markdown","metadata":{"id":"HODH9dWL3Vls"},"source":["## Evaluating the model"]},{"cell_type":"code","metadata":{"id":"lXyQjawuIjck","executionInfo":{"status":"ok","timestamp":1628941881815,"user_tz":-480,"elapsed":13,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}}},"source":["y_test = y_test.reshape(-1,1)"],"execution_count":24,"outputs":[]},{"cell_type":"code","metadata":{"id":"kiiMwpeSIvLa","executionInfo":{"status":"ok","timestamp":1628941881815,"user_tz":-480,"elapsed":13,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}}},"source":["y_pred = y_pred.reshape(-1,1)"],"execution_count":25,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"shQkxEy13jIX"},"source":["### Mean Absolute Error (MAE)\n","\n","\n","\n","\n","\n"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"czABznrA4CD2","executionInfo":{"status":"ok","timestamp":1628941881815,"user_tz":-480,"elapsed":12,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"61ebacf7-1f45-4b87-8e09-ad9f5bcac4ca"},"source":["from sklearn.metrics import mean_absolute_error\n","print(\"MAE\",mean_absolute_error(y_test,y_pred))"],"execution_count":26,"outputs":[{"output_type":"stream","text":["MAE 0.07946000359260787\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"bL1gHSDH3qQ8"},"source":["### Mean Squared Error (MSE)"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Xgut-LREm4sS","executionInfo":{"status":"ok","timestamp":1628941881815,"user_tz":-480,"elapsed":10,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"e486a59e-afa9-44ae-ef23-28a8013ced78"},"source":["from sklearn.metrics import mean_squared_error\n","print(\"MSE\",mean_squared_error(y_test,y_pred))"],"execution_count":27,"outputs":[{"output_type":"stream","text":["MSE 0.01809884686959964\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"FO4KGqhu35AK"},"source":["### Root Mean Squared Error (RMSE)"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"cIElkWlvm_0S","executionInfo":{"status":"ok","timestamp":1628941881816,"user_tz":-480,"elapsed":9,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"b9b4204c-ad0d-4392-ebf6-857b1bb301ce"},"source":["print(\"RMSE\",np.sqrt(mean_squared_error(y_test,y_pred)))"],"execution_count":28,"outputs":[{"output_type":"stream","text":["RMSE 0.1345319548270954\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"CurClKpP3_uD"},"source":["### Root Mean Squared Log Error (RMSLE)"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"p0_gKkEBnD33","executionInfo":{"status":"ok","timestamp":1628941882160,"user_tz":-480,"elapsed":352,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"9482fe12-0ebf-48f2-e6f7-f0e38fe0708e"},"source":["print(\"RMSLE\",np.log(np.sqrt(mean_squared_error(y_test,y_pred))))"],"execution_count":29,"outputs":[{"output_type":"stream","text":["RMSLE -2.0059535258008756\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"eqFwLEHk4H3k"},"source":["### R Squared (R2)"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"xGKr5H8RnGXN","executionInfo":{"status":"ok","timestamp":1628941882161,"user_tz":-480,"elapsed":13,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"712202d9-bf57-4a17-eb61-5886b613ed55"},"source":["from sklearn.metrics import r2_score\n","r2 = r2_score(y_test,y_pred)\n","print(r2)"],"execution_count":30,"outputs":[{"output_type":"stream","text":["0.9806614412561213\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"jO8Y2i2L4L1n"},"source":["### Adjusted R Squared"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"vXQtPejrnKLn","executionInfo":{"status":"ok","timestamp":1628941882161,"user_tz":-480,"elapsed":9,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"ab9200c7-6b8a-40e1-8e21-91fb3117dc3c"},"source":["n=155\n","k=5\n","adj_r2_score = 1 - ((1-r2)*(n-1)/(n-k-1))\n","print(adj_r2_score)"],"execution_count":31,"outputs":[{"output_type":"stream","text":["0.9800124963318301\n"],"name":"stdout"}]}]}