Last active
December 3, 2020 18:43
-
-
Save RobotOptimist/7003f85731e1fa3916c521729581eff8 to your computer and use it in GitHub Desktop.
Python Multiple Linear Regression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
R&D Spend | Administration | Marketing Spend | State | Profit | |
---|---|---|---|---|---|
165349.2 | 136897.8 | 471784.1 | New York | 192261.83 | |
162597.7 | 151377.59 | 443898.53 | California | 191792.06 | |
153441.51 | 101145.55 | 407934.54 | Florida | 191050.39 | |
144372.41 | 118671.85 | 383199.62 | New York | 182901.99 | |
142107.34 | 91391.77 | 366168.42 | Florida | 166187.94 | |
131876.9 | 99814.71 | 362861.36 | New York | 156991.12 | |
134615.46 | 147198.87 | 127716.82 | California | 156122.51 | |
130298.13 | 145530.06 | 323876.68 | Florida | 155752.6 | |
120542.52 | 148718.95 | 311613.29 | New York | 152211.77 | |
123334.88 | 108679.17 | 304981.62 | California | 149759.96 | |
101913.08 | 110594.11 | 229160.95 | Florida | 146121.95 | |
100671.96 | 91790.61 | 249744.55 | California | 144259.4 | |
93863.75 | 127320.38 | 249839.44 | Florida | 141585.52 | |
91992.39 | 135495.07 | 252664.93 | California | 134307.35 | |
119943.24 | 156547.42 | 256512.92 | Florida | 132602.65 | |
114523.61 | 122616.84 | 261776.23 | New York | 129917.04 | |
78013.11 | 121597.55 | 264346.06 | California | 126992.93 | |
94657.16 | 145077.58 | 282574.31 | New York | 125370.37 | |
91749.16 | 114175.79 | 294919.57 | Florida | 124266.9 | |
86419.7 | 153514.11 | 0 | New York | 122776.86 | |
76253.86 | 113867.3 | 298664.47 | California | 118474.03 | |
78389.47 | 153773.43 | 299737.29 | New York | 111313.02 | |
73994.56 | 122782.75 | 303319.26 | Florida | 110352.25 | |
67532.53 | 105751.03 | 304768.73 | Florida | 108733.99 | |
77044.01 | 99281.34 | 140574.81 | New York | 108552.04 | |
64664.71 | 139553.16 | 137962.62 | California | 107404.34 | |
75328.87 | 144135.98 | 134050.07 | Florida | 105733.54 | |
72107.6 | 127864.55 | 353183.81 | New York | 105008.31 | |
66051.52 | 182645.56 | 118148.2 | Florida | 103282.38 | |
65605.48 | 153032.06 | 107138.38 | New York | 101004.64 | |
61994.48 | 115641.28 | 91131.24 | Florida | 99937.59 | |
61136.38 | 152701.92 | 88218.23 | New York | 97483.56 | |
63408.86 | 129219.61 | 46085.25 | California | 97427.84 | |
55493.95 | 103057.49 | 214634.81 | Florida | 96778.92 | |
46426.07 | 157693.92 | 210797.67 | California | 96712.8 | |
46014.02 | 85047.44 | 205517.64 | New York | 96479.51 | |
28663.76 | 127056.21 | 201126.82 | Florida | 90708.19 | |
44069.95 | 51283.14 | 197029.42 | California | 89949.14 | |
20229.59 | 65947.93 | 185265.1 | New York | 81229.06 | |
38558.51 | 82982.09 | 174999.3 | California | 81005.76 | |
28754.33 | 118546.05 | 172795.67 | California | 78239.91 | |
27892.92 | 84710.77 | 164470.71 | Florida | 77798.83 | |
23640.93 | 96189.63 | 148001.11 | California | 71498.49 | |
15505.73 | 127382.3 | 35534.17 | New York | 69758.98 | |
22177.74 | 154806.14 | 28334.72 | California | 65200.33 | |
1000.23 | 124153.04 | 1903.93 | New York | 64926.08 | |
1315.46 | 115816.21 | 297114.46 | Florida | 49490.75 | |
0 | 135426.92 | 0 | California | 42559.73 | |
542.05 | 51743.15 | 0 | New York | 35673.41 | |
0 | 116983.8 | 45173.06 | California | 14681.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Multiple Linear Regression","provenance":[],"toc_visible":true,"authorship_tag":"ABX9TyPhYhte6t7H4wEK4xPpDWT7"},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"CazISR8X_HUG","colab_type":"text"},"source":["# Multiple Linear Regression"]},{"cell_type":"markdown","metadata":{"id":"pOyqYHTk_Q57","colab_type":"text"},"source":["## Importing the libraries"]},{"cell_type":"code","metadata":{"id":"T_YHJjnD_Tja","colab_type":"code","colab":{}},"source":["import numpy as np\n","import matplotlib.pyplot as plt\n","import pandas as pd"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"vgC61-ah_WIz","colab_type":"text"},"source":["## Importing the dataset"]},{"cell_type":"code","metadata":{"id":"UrxyEKGn_ez7","colab_type":"code","colab":{}},"source":["dataset = pd.read_csv('50_Startups.csv')\n","X = dataset.iloc[:, :-1].values\n","y = dataset.iloc[:, -1].values"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"GOB3QhV9B5kD","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":857},"outputId":"4a05377a-2db2-43fc-b824-a0710448baee","executionInfo":{"status":"ok","timestamp":1586353652778,"user_tz":-240,"elapsed":552,"user":{"displayName":"Hadelin de Ponteves","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GhEuXdT7eQweUmRPW8_laJuPggSK6hfvpl5a6WBaA=s64","userId":"15047218817161520419"}}},"source":["print(X)"],"execution_count":3,"outputs":[{"output_type":"stream","text":["[[165349.2 136897.8 471784.1 'New York']\n"," [162597.7 151377.59 443898.53 'California']\n"," [153441.51 101145.55 407934.54 'Florida']\n"," [144372.41 118671.85 383199.62 'New York']\n"," [142107.34 91391.77 366168.42 'Florida']\n"," [131876.9 99814.71 362861.36 'New York']\n"," [134615.46 147198.87 127716.82 'California']\n"," [130298.13 145530.06 323876.68 'Florida']\n"," [120542.52 148718.95 311613.29 'New York']\n"," [123334.88 108679.17 304981.62 'California']\n"," [101913.08 110594.11 229160.95 'Florida']\n"," [100671.96 91790.61 249744.55 'California']\n"," [93863.75 127320.38 249839.44 'Florida']\n"," [91992.39 135495.07 252664.93 'California']\n"," [119943.24 156547.42 256512.92 'Florida']\n"," [114523.61 122616.84 261776.23 'New York']\n"," [78013.11 121597.55 264346.06 'California']\n"," [94657.16 145077.58 282574.31 'New York']\n"," [91749.16 114175.79 294919.57 'Florida']\n"," [86419.7 153514.11 0.0 'New York']\n"," [76253.86 113867.3 298664.47 'California']\n"," [78389.47 153773.43 299737.29 'New York']\n"," [73994.56 122782.75 303319.26 'Florida']\n"," [67532.53 105751.03 304768.73 'Florida']\n"," [77044.01 99281.34 140574.81 'New York']\n"," [64664.71 139553.16 137962.62 'California']\n"," [75328.87 144135.98 134050.07 'Florida']\n"," [72107.6 127864.55 353183.81 'New York']\n"," [66051.52 182645.56 118148.2 'Florida']\n"," [65605.48 153032.06 107138.38 'New York']\n"," [61994.48 115641.28 91131.24 'Florida']\n"," [61136.38 152701.92 88218.23 'New York']\n"," [63408.86 129219.61 46085.25 'California']\n"," [55493.95 103057.49 214634.81 'Florida']\n"," [46426.07 157693.92 210797.67 'California']\n"," [46014.02 85047.44 205517.64 'New York']\n"," [28663.76 127056.21 201126.82 'Florida']\n"," [44069.95 51283.14 197029.42 'California']\n"," [20229.59 65947.93 185265.1 'New York']\n"," [38558.51 82982.09 174999.3 'California']\n"," [28754.33 118546.05 172795.67 'California']\n"," [27892.92 84710.77 164470.71 'Florida']\n"," [23640.93 96189.63 148001.11 'California']\n"," [15505.73 127382.3 35534.17 'New York']\n"," [22177.74 154806.14 28334.72 'California']\n"," [1000.23 124153.04 1903.93 'New York']\n"," [1315.46 115816.21 297114.46 'Florida']\n"," [0.0 135426.92 0.0 'California']\n"," [542.05 51743.15 0.0 'New York']\n"," [0.0 116983.8 45173.06 'California']]\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"VadrvE7s_lS9","colab_type":"text"},"source":["## Encoding categorical data"]},{"cell_type":"code","metadata":{"id":"wV3fD1mbAvsh","colab_type":"code","colab":{}},"source":["from sklearn.compose import ColumnTransformer\n","from sklearn.preprocessing import OneHotEncoder\n","ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [3])], remainder='passthrough')\n","X = np.array(ct.fit_transform(X))"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"4ym3HdYeCGYG","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":857},"outputId":"ce09e670-cf06-4a1c-f5b0-89422aae0496","executionInfo":{"status":"ok","timestamp":1586353657759,"user_tz":-240,"elapsed":616,"user":{"displayName":"Hadelin de Ponteves","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GhEuXdT7eQweUmRPW8_laJuPggSK6hfvpl5a6WBaA=s64","userId":"15047218817161520419"}}},"source":["print(X)"],"execution_count":5,"outputs":[{"output_type":"stream","text":["[[0.0 0.0 1.0 165349.2 136897.8 471784.1]\n"," [1.0 0.0 0.0 162597.7 151377.59 443898.53]\n"," [0.0 1.0 0.0 153441.51 101145.55 407934.54]\n"," [0.0 0.0 1.0 144372.41 118671.85 383199.62]\n"," [0.0 1.0 0.0 142107.34 91391.77 366168.42]\n"," [0.0 0.0 1.0 131876.9 99814.71 362861.36]\n"," [1.0 0.0 0.0 134615.46 147198.87 127716.82]\n"," [0.0 1.0 0.0 130298.13 145530.06 323876.68]\n"," [0.0 0.0 1.0 120542.52 148718.95 311613.29]\n"," [1.0 0.0 0.0 123334.88 108679.17 304981.62]\n"," [0.0 1.0 0.0 101913.08 110594.11 229160.95]\n"," [1.0 0.0 0.0 100671.96 91790.61 249744.55]\n"," [0.0 1.0 0.0 93863.75 127320.38 249839.44]\n"," [1.0 0.0 0.0 91992.39 135495.07 252664.93]\n"," [0.0 1.0 0.0 119943.24 156547.42 256512.92]\n"," [0.0 0.0 1.0 114523.61 122616.84 261776.23]\n"," [1.0 0.0 0.0 78013.11 121597.55 264346.06]\n"," [0.0 0.0 1.0 94657.16 145077.58 282574.31]\n"," [0.0 1.0 0.0 91749.16 114175.79 294919.57]\n"," [0.0 0.0 1.0 86419.7 153514.11 0.0]\n"," [1.0 0.0 0.0 76253.86 113867.3 298664.47]\n"," [0.0 0.0 1.0 78389.47 153773.43 299737.29]\n"," [0.0 1.0 0.0 73994.56 122782.75 303319.26]\n"," [0.0 1.0 0.0 67532.53 105751.03 304768.73]\n"," [0.0 0.0 1.0 77044.01 99281.34 140574.81]\n"," [1.0 0.0 0.0 64664.71 139553.16 137962.62]\n"," [0.0 1.0 0.0 75328.87 144135.98 134050.07]\n"," [0.0 0.0 1.0 72107.6 127864.55 353183.81]\n"," [0.0 1.0 0.0 66051.52 182645.56 118148.2]\n"," [0.0 0.0 1.0 65605.48 153032.06 107138.38]\n"," [0.0 1.0 0.0 61994.48 115641.28 91131.24]\n"," [0.0 0.0 1.0 61136.38 152701.92 88218.23]\n"," [1.0 0.0 0.0 63408.86 129219.61 46085.25]\n"," [0.0 1.0 0.0 55493.95 103057.49 214634.81]\n"," [1.0 0.0 0.0 46426.07 157693.92 210797.67]\n"," [0.0 0.0 1.0 46014.02 85047.44 205517.64]\n"," [0.0 1.0 0.0 28663.76 127056.21 201126.82]\n"," [1.0 0.0 0.0 44069.95 51283.14 197029.42]\n"," [0.0 0.0 1.0 20229.59 65947.93 185265.1]\n"," [1.0 0.0 0.0 38558.51 82982.09 174999.3]\n"," [1.0 0.0 0.0 28754.33 118546.05 172795.67]\n"," [0.0 1.0 0.0 27892.92 84710.77 164470.71]\n"," [1.0 0.0 0.0 23640.93 96189.63 148001.11]\n"," [0.0 0.0 1.0 15505.73 127382.3 35534.17]\n"," [1.0 0.0 0.0 22177.74 154806.14 28334.72]\n"," [0.0 0.0 1.0 1000.23 124153.04 1903.93]\n"," [0.0 1.0 0.0 1315.46 115816.21 297114.46]\n"," [1.0 0.0 0.0 0.0 135426.92 0.0]\n"," [0.0 0.0 1.0 542.05 51743.15 0.0]\n"," [1.0 0.0 0.0 0.0 116983.8 45173.06]]\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"WemVnqgeA70k","colab_type":"text"},"source":["## Splitting the dataset into the Training set and Test set"]},{"cell_type":"code","metadata":{"id":"Kb_v_ae-A-20","colab_type":"code","colab":{}},"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":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"k-McZVsQBINc","colab_type":"text"},"source":["## Training the Multiple Linear Regression model on the Training set"]},{"cell_type":"code","metadata":{"id":"ywPjx0L1BMiD","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"099836bc-4d85-4b4f-a488-093faf02e8cb","executionInfo":{"status":"ok","timestamp":1586353664008,"user_tz":-240,"elapsed":757,"user":{"displayName":"Hadelin de Ponteves","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GhEuXdT7eQweUmRPW8_laJuPggSK6hfvpl5a6WBaA=s64","userId":"15047218817161520419"}}},"source":["from sklearn.linear_model import LinearRegression\n","regressor = LinearRegression()\n","regressor.fit(X_train, y_train)"],"execution_count":7,"outputs":[{"output_type":"execute_result","data":{"text/plain":["LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)"]},"metadata":{"tags":[]},"execution_count":7}]},{"cell_type":"markdown","metadata":{"id":"xNkXL1YQBiBT","colab_type":"text"},"source":["## Predicting the Test set results"]},{"cell_type":"code","metadata":{"id":"TQKmwvtdBkyb","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":185},"outputId":"493436bf-a4ae-4374-ca16-0b0c25d19457","executionInfo":{"status":"ok","timestamp":1586353666678,"user_tz":-240,"elapsed":951,"user":{"displayName":"Hadelin de Ponteves","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GhEuXdT7eQweUmRPW8_laJuPggSK6hfvpl5a6WBaA=s64","userId":"15047218817161520419"}}},"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":8,"outputs":[{"output_type":"stream","text":["[[103015.2 103282.38]\n"," [132582.28 144259.4 ]\n"," [132447.74 146121.95]\n"," [ 71976.1 77798.83]\n"," [178537.48 191050.39]\n"," [116161.24 105008.31]\n"," [ 67851.69 81229.06]\n"," [ 98791.73 97483.56]\n"," [113969.44 110352.25]\n"," [167921.07 166187.94]]\n"],"name":"stdout"}]}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This file is autogenerated by pip-compile | |
# To update, run: | |
# | |
# pip-compile | |
# | |
click==7.1.2 | |
cycler==0.10.0 | |
joblib==0.17.0 | |
kiwisolver==1.3.1 | |
matplotlib==3.3.3 | |
nltk==3.5 | |
numpy==1.19.2 | |
pandas==1.1.4 | |
Pillow==8.0.1 | |
pip==20.2.4 | |
pip-tools==5.3.1 | |
pyparsing==2.4.7 | |
python-dateutil==2.8.1 | |
pytz==2020.4 | |
regex==2020.10.15 | |
scikit-learn==0.23.2 | |
scipy==1.5.4 | |
six==1.15.0 | |
sklearn==0.0 | |
threadpoolctl==2.1.0 | |
tqdm==4.50.2 | |
wheel==0.35.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment