{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Crop_Yield_Prediction_DT.ipynb","provenance":[],"collapsed_sections":[],"authorship_tag":"ABX9TyNMlqFQQ8IXf6J3nCckpd5A"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","metadata":{"id":"R5QjBRaAyC_H"},"source":["# Decision Tree\n"]},{"cell_type":"markdown","metadata":{"id":"lIMTzfBSy8Vh"},"source":["## Importing the libraries"]},{"cell_type":"code","metadata":{"id":"rN03-Fe5mUhY","executionInfo":{"status":"ok","timestamp":1628941768873,"user_tz":-480,"elapsed":410,"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":1,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"aePtTVsdp2V9","executionInfo":{"status":"ok","timestamp":1628941802948,"user_tz":-480,"elapsed":34078,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"79b22cce-8c65-485d-b605-631aa3f6e774"},"source":["from google.colab import drive\n","drive.mount('/content/drive')"],"execution_count":2,"outputs":[{"output_type":"stream","text":["Mounted at /content/drive\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"Sx5g6jHGzE2_"},"source":["## Importing the dataset"]},{"cell_type":"code","metadata":{"id":"YL7Nm_VNnDiG","executionInfo":{"status":"ok","timestamp":1628941803729,"user_tz":-480,"elapsed":25,"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":3,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":414},"id":"u2Q5VuTxjoX2","executionInfo":{"status":"ok","timestamp":1628941803731,"user_tz":-480,"elapsed":24,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"22c73a5a-f5a6-4845-ef24-bf2fa68cf33f"},"source":["dataset"],"execution_count":4,"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":4}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Dk-0FTL8rUNx","executionInfo":{"status":"ok","timestamp":1628941803732,"user_tz":-480,"elapsed":22,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"7cbd49a3-380f-4491-c24a-596b8477eb65"},"source":["dataset.dtypes"],"execution_count":5,"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":5}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":294},"id":"00JKPC2LzVs3","executionInfo":{"status":"ok","timestamp":1628941803732,"user_tz":-480,"elapsed":18,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"ef38dbf3-c7d2-4778-e00b-800cb516b7bb"},"source":["dataset.describe()"],"execution_count":6,"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":6}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"8UuKznwlqBAZ","executionInfo":{"status":"ok","timestamp":1628941804288,"user_tz":-480,"elapsed":573,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"a8ccaf21-82d1-4af6-c7f4-4f1bf84d2f1b"},"source":["X"],"execution_count":7,"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":7}]},{"cell_type":"markdown","metadata":{"id":"8IkU-2mNztoR"},"source":["## Encoding categorical data"]},{"cell_type":"code","metadata":{"id":"S_CKi_rlqZ9t","executionInfo":{"status":"ok","timestamp":1628941804661,"user_tz":-480,"elapsed":379,"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":8,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"SCVMHxv9rIrU","executionInfo":{"status":"ok","timestamp":1628941804662,"user_tz":-480,"elapsed":9,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"4509a647-7cf7-4367-b74d-910426091130"},"source":["X"],"execution_count":9,"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":9}]},{"cell_type":"markdown","metadata":{"id":"8rkYhIE0z4SL"},"source":["## Feature Scaling"]},{"cell_type":"code","metadata":{"id":"VFoTVxApvn34","executionInfo":{"status":"ok","timestamp":1628941804662,"user_tz":-480,"elapsed":6,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}}},"source":["y = y.reshape(-1,1)"],"execution_count":10,"outputs":[]},{"cell_type":"code","metadata":{"id":"VZsm-HcyvVoC","executionInfo":{"status":"ok","timestamp":1628941804663,"user_tz":-480,"elapsed":6,"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":11,"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":1628941804664,"user_tz":-480,"elapsed":7,"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":12,"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":1628941805065,"user_tz":-480,"elapsed":408,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"0f7d51e3-595a-45e9-bfe6-43aa3fc22352"},"source":["from sklearn.tree import DecisionTreeRegressor\n","regressor = DecisionTreeRegressor(random_state = 0)\n","regressor.fit(X_train, y_train)"],"execution_count":13,"outputs":[{"output_type":"execute_result","data":{"text/plain":["DecisionTreeRegressor(ccp_alpha=0.0, criterion='mse', max_depth=None,\n"," max_features=None, max_leaf_nodes=None,\n"," min_impurity_decrease=0.0, min_impurity_split=None,\n"," min_samples_leaf=1, min_samples_split=2,\n"," min_weight_fraction_leaf=0.0, presort='deprecated',\n"," random_state=0, splitter='best')"]},"metadata":{"tags":[]},"execution_count":13}]},{"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":1628941805065,"user_tz":-480,"elapsed":22,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"3f22fbf2-0b32-45fe-aac9-3813fb46827c"},"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":14,"outputs":[{"output_type":"stream","text":["[[-0.66 -0.62]\n"," [ 1.49 1.53]\n"," [-0.69 -0.66]\n"," [-0.34 -0.23]\n"," [-0.64 -0.68]\n"," [-0.66 -0.66]\n"," [ 1.35 1.57]\n"," [-0.39 -0.28]\n"," [-0.66 -0.71]\n"," [ 1.73 2.07]\n"," [-0.36 -0.37]\n"," [ 1.49 1.65]\n"," [-0.36 -0.27]\n"," [-0.67 -0.66]\n"," [ 1.64 1.23]\n"," [-0.65 -0.66]\n"," [-0.71 -0.69]\n"," [-0.67 -0.67]\n"," [ 1.81 1.62]\n"," [-0.7 -0.71]\n"," [-0.66 -0.68]\n"," [-0.68 -0.67]\n"," [-0.67 -0.69]\n"," [ 1.57 1.6 ]\n"," [-0.36 -0.37]\n"," [-0.64 -0.71]\n"," [-0.36 -0.44]\n"," [ 1.49 1.77]\n"," [-0.44 -0.42]\n"," [-0.34 -0.36]\n"," [-0.72 -0.69]\n"," [-0.68 -0.67]]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"ZT52QzFdoVMj","executionInfo":{"status":"ok","timestamp":1628941805066,"user_tz":-480,"elapsed":21,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"358dff6b-93a4-40a7-d3fe-22adeded5ad4"},"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":15,"outputs":[{"output_type":"execute_result","data":{"text/plain":["array([9956.])"]},"metadata":{"tags":[]},"execution_count":15}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Gt_B80VAwnNJ","executionInfo":{"status":"ok","timestamp":1628941805067,"user_tz":-480,"elapsed":20,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"9db185b3-d1b3-4ef7-94f7-d144dd7a259f"},"source":["sc_y.inverse_transform(y_pred)"],"execution_count":16,"outputs":[{"output_type":"execute_result","data":{"text/plain":["array([ 9956., 160862., 7663., 32385., 11108., 9956., 151070.,\n"," 28829., 10010., 177543., 30627., 160862., 30627., 9322.,\n"," 171189., 10737., 6275., 9322., 183004., 6913., 10058.,\n"," 8604., 8865., 166136., 30627., 11487., 30627., 160862.,\n"," 25187., 32385., 5765., 8300.])"]},"metadata":{"tags":[]},"execution_count":16}]},{"cell_type":"markdown","metadata":{"id":"HODH9dWL3Vls"},"source":["## Evaluating the model"]},{"cell_type":"code","metadata":{"id":"lXyQjawuIjck","executionInfo":{"status":"ok","timestamp":1628941805068,"user_tz":-480,"elapsed":19,"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":17,"outputs":[]},{"cell_type":"code","metadata":{"id":"kiiMwpeSIvLa","executionInfo":{"status":"ok","timestamp":1628941805069,"user_tz":-480,"elapsed":19,"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":18,"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":1628941805070,"user_tz":-480,"elapsed":20,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"cf1bc49a-0f55-42aa-c6b9-e4623086e117"},"source":["from sklearn.metrics import mean_absolute_error\n","print(\"MAE\",mean_absolute_error(y_test,y_pred))"],"execution_count":19,"outputs":[{"output_type":"stream","text":["MAE 0.07770309513684688\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":1628941805070,"user_tz":-480,"elapsed":18,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"4f91b7d8-a6a6-4250-c324-2201cdada055"},"source":["from sklearn.metrics import mean_squared_error\n","print(\"MSE\",mean_squared_error(y_test,y_pred))"],"execution_count":20,"outputs":[{"output_type":"stream","text":["MSE 0.016410131096273546\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":1628941805070,"user_tz":-480,"elapsed":16,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"331e384e-4d4d-46cb-a37f-37dc6d2261d3"},"source":["print(\"RMSE\",np.sqrt(mean_squared_error(y_test,y_pred)))"],"execution_count":21,"outputs":[{"output_type":"stream","text":["RMSE 0.12810203392715333\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":1628941805071,"user_tz":-480,"elapsed":15,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"cb0f144a-2d97-4571-cdd7-5f277ee61d68"},"source":["print(\"RMSLE\",np.log(np.sqrt(mean_squared_error(y_test,y_pred))))"],"execution_count":22,"outputs":[{"output_type":"stream","text":["RMSLE -2.054928192554027\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":1628941805071,"user_tz":-480,"elapsed":14,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"f4ffd3f2-9e04-4e6d-b087-ef8b06029067"},"source":["from sklearn.metrics import r2_score\n","r2 = r2_score(y_test,y_pred)\n","print(r2)"],"execution_count":23,"outputs":[{"output_type":"stream","text":["0.9824658285421994\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":1628941805071,"user_tz":-480,"elapsed":12,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"429e07e3-9a0b-47cc-d9fd-2b918f955f07"},"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":24,"outputs":[{"output_type":"stream","text":["0.981877433526837\n"],"name":"stdout"}]}]}