{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Crop_Yield_Prediction_SVR.ipynb","provenance":[],"collapsed_sections":[],"authorship_tag":"ABX9TyOVQnIMQGD9h0U9dQssXilE"},"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":1628941842594,"user_tz":-480,"elapsed":10,"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":25,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"aePtTVsdp2V9","executionInfo":{"status":"ok","timestamp":1628941840226,"user_tz":-480,"elapsed":64091,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"1e724bb7-062a-4ff1-cc74-2b6ab0a874b9"},"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":1628941840694,"user_tz":-480,"elapsed":21,"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":1628941840695,"user_tz":-480,"elapsed":20,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"baa6984e-5267-4188-ced1-9d9156f05aef"},"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":1628941840695,"user_tz":-480,"elapsed":18,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"566e8d1d-b7ad-408c-c593-e1325d9c082a"},"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":1628941840696,"user_tz":-480,"elapsed":13,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"71836abd-55dd-4f26-f8a9-99a012917fa9"},"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":1628941840696,"user_tz":-480,"elapsed":12,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"16b736bd-b9a8-474c-b6a6-235bb22b493e"},"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":1628941841806,"user_tz":-480,"elapsed":1118,"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":1628941841806,"user_tz":-480,"elapsed":7,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"addb6312-02e6-456c-8d18-3b5bf3eba882"},"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":1628941841806,"user_tz":-480,"elapsed":3,"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":1628941841807,"user_tz":-480,"elapsed":4,"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":1628941841808,"user_tz":-480,"elapsed":5,"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 Support Vector Regression model on the Training set"]},{"cell_type":"code","metadata":{"id":"y6R4rt_GRz15","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1628941842589,"user_tz":-480,"elapsed":785,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"145122f5-4a95-4b24-93b2-b38b19eccce2"},"source":["from sklearn.svm import SVR\n","regressor = SVR(kernel = 'rbf')\n","regressor.fit(X_train, y_train)"],"execution_count":13,"outputs":[{"output_type":"stream","text":["/usr/local/lib/python3.7/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":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":1628941842591,"user_tz":-480,"elapsed":27,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"d8220838-20f2-48d9-eb86-7fc341714e46"},"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.61 -0.62]\n"," [ 1.25 1.53]\n"," [-0.64 -0.66]\n"," [-0.29 -0.23]\n"," [-0.66 -0.68]\n"," [-0.68 -0.66]\n"," [ 1.52 1.57]\n"," [-0.3 -0.28]\n"," [-0.64 -0.71]\n"," [ 0.89 2.07]\n"," [-0.4 -0.37]\n"," [ 1.49 1.65]\n"," [-0.28 -0.27]\n"," [-0.52 -0.66]\n"," [ 1.58 1.23]\n"," [-0.69 -0.66]\n"," [-0.54 -0.69]\n"," [-0.34 -0.67]\n"," [ 1.62 1.62]\n"," [-0.73 -0.71]\n"," [-0.69 -0.68]\n"," [-0.55 -0.67]\n"," [-0.72 -0.69]\n"," [ 1.59 1.6 ]\n"," [-0.25 -0.37]\n"," [-0.6 -0.71]\n"," [-0.33 -0.44]\n"," [ 1.57 1.77]\n"," [-0.37 -0.42]\n"," [-0.27 -0.36]\n"," [-0.56 -0.69]\n"," [-0.61 -0.67]]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"ZT52QzFdoVMj","executionInfo":{"status":"ok","timestamp":1628941842591,"user_tz":-480,"elapsed":23,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"9c5852fb-0a73-43fe-faf6-cac57f44e3ea"},"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([13591.75])"]},"metadata":{"tags":[]},"execution_count":15}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Gt_B80VAwnNJ","executionInfo":{"status":"ok","timestamp":1628941842591,"user_tz":-480,"elapsed":20,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"7aa27d59-911c-41e2-939f-a4787d358595"},"source":["sc_y.inverse_transform(y_pred)"],"execution_count":16,"outputs":[{"output_type":"execute_result","data":{"text/plain":["array([ 13591.75, 143717.38, 11396.73, 35978.04, 9554.05, 8192.06,\n"," 162656.48, 35082.39, 11313.34, 118744.91, 28395.3 , 160629.18,\n"," 36281.45, 19363.47, 166864.46, 7990.5 , 18375.18, 32228.41,\n"," 170128.05, 4755.83, 7733.91, 17717.6 , 5522.18, 168110.56,\n"," 38489.65, 13915.15, 32818.02, 166225.99, 30084.08, 36849.67,\n"," 16697.57, 13074.46])"]},"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":1628941842592,"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":1628941842592,"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":1628941842592,"user_tz":-480,"elapsed":18,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"44a0fa08-e0ae-494f-8429-1b8a17d11318"},"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.12490692759669908\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":1628941842593,"user_tz":-480,"elapsed":18,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"4162f3b1-96f6-4f0d-f4a0-3b606539b1de"},"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.059498709028539015\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":1628941842593,"user_tz":-480,"elapsed":16,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"6f2ce6e9-42ba-4556-e0e1-cd3e7a215fee"},"source":["print(\"RMSE\",np.sqrt(mean_squared_error(y_test,y_pred)))"],"execution_count":21,"outputs":[{"output_type":"stream","text":["RMSE 0.24392357210515556\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":1628941842593,"user_tz":-480,"elapsed":14,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"4f760868-4af5-40d8-f4a5-7b5f38f27b58"},"source":["print(\"RMSLE\",np.log(np.sqrt(mean_squared_error(y_test,y_pred))))"],"execution_count":22,"outputs":[{"output_type":"stream","text":["RMSLE -1.4109003318326403\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":1628941842594,"user_tz":-480,"elapsed":13,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"a71f5ee4-c5d3-46da-ef4e-78fe1144c521"},"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.9364258238094701\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":1628941842594,"user_tz":-480,"elapsed":12,"user":{"displayName":"Avinnaash Suresh","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gg43tbnG3E_ve7kI59iMhn1idBD-ZqV8v0bXDut=s64","userId":"14263710632077858620"}},"outputId":"2de6a44e-3877-4838-8808-0d993ee6adb5"},"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.9342924621923383\n"],"name":"stdout"}]}]}