1
+ {
2
+ "nbformat" : 4 ,
3
+ "nbformat_minor" : 0 ,
4
+ "metadata" : {
5
+ "colab" : {
6
+ "provenance" : [],
7
+ "collapsed_sections" : [],
8
+ "authorship_tag" : " ABX9TyPo1/jVmtDE9X+OTsJBXsvz" ,
9
+ "include_colab_link" : true
10
+ },
11
+ "kernelspec" : {
12
+ "name" : " python3" ,
13
+ "display_name" : " Python 3"
14
+ },
15
+ "language_info" : {
16
+ "name" : " python"
17
+ }
18
+ },
19
+ "cells" : [
20
+ {
21
+ "cell_type" : " markdown" ,
22
+ "metadata" : {
23
+ "id" : " view-in-github" ,
24
+ "colab_type" : " text"
25
+ },
26
+ "source" : [
27
+ " <a href=\" https://colab.research.google.com/github/DeepthiTabithaBennet/Python_AppliedStatistics/blob/main/Multiple_Linear_Regression.ipynb\" target=\" _parent\" ><img src=\" https://colab.research.google.com/assets/colab-badge.svg\" alt=\" Open In Colab\" /></a>"
28
+ ]
29
+ },
30
+ {
31
+ "cell_type" : " markdown" ,
32
+ "metadata" : {
33
+ "id" : " RmGFoV8P22AE"
34
+ },
35
+ "source" : [
36
+ " # **Multiple Linear Regression**"
37
+ ]
38
+ },
39
+ {
40
+ "cell_type" : " code" ,
41
+ "metadata" : {
42
+ "id" : " 75W-ukM9p6LC"
43
+ },
44
+ "source" : [
45
+ " # Written by Deepthi Tabitha Bennet\n " ,
46
+ " \n " ,
47
+ " !pip install matplotlib\n " ,
48
+ " !pip install sklearn\n " ,
49
+ " !pip install LinearRegression\n " ,
50
+ " \n " ,
51
+ " import pandas as pd\n " ,
52
+ " import numpy as np\n " ,
53
+ " import matplotlib.pyplot as plt\n " ,
54
+ " import seaborn as sns\n " ,
55
+ " %matplotlib inline "
56
+ ],
57
+ "execution_count" : null ,
58
+ "outputs" : []
59
+ },
60
+ {
61
+ "cell_type" : " code" ,
62
+ "metadata" : {
63
+ "id" : " YWyZDKvUvt94"
64
+ },
65
+ "source" : [
66
+ " raw_data = pd.read_csv('Housing_Data.csv')"
67
+ ],
68
+ "execution_count" : null ,
69
+ "outputs" : []
70
+ },
71
+ {
72
+ "cell_type" : " code" ,
73
+ "metadata" : {
74
+ "id" : " _kQumzvYsv1Y"
75
+ },
76
+ "source" : [
77
+ " raw_data.info()"
78
+ ],
79
+ "execution_count" : null ,
80
+ "outputs" : []
81
+ },
82
+ {
83
+ "cell_type" : " code" ,
84
+ "metadata" : {
85
+ "id" : " VGmvmAJusv6J"
86
+ },
87
+ "source" : [
88
+ " sns.pairplot(raw_data)"
89
+ ],
90
+ "execution_count" : null ,
91
+ "outputs" : []
92
+ },
93
+ {
94
+ "cell_type" : " code" ,
95
+ "metadata" : {
96
+ "id" : " e-RE0_pmsv95"
97
+ },
98
+ "source" : [
99
+ " x = raw_data[['Avg. Area Income', 'Avg. Area House Age', 'Avg. Area Number of Rooms', 'Avg. Area Number of Bedrooms', 'Area Population']]\n " ,
100
+ " y = raw_data['Price']"
101
+ ],
102
+ "execution_count" : null ,
103
+ "outputs" : []
104
+ },
105
+ {
106
+ "cell_type" : " code" ,
107
+ "metadata" : {
108
+ "id" : " dCTLqMejvtlA"
109
+ },
110
+ "source" : [
111
+ " from sklearn.model_selection import train_test_split\n " ,
112
+ " \n " ,
113
+ " x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3)"
114
+ ],
115
+ "execution_count" : null ,
116
+ "outputs" : []
117
+ },
118
+ {
119
+ "cell_type" : " code" ,
120
+ "metadata" : {
121
+ "id" : " LtlnUPffvtt-"
122
+ },
123
+ "source" : [
124
+ " from sklearn.linear_model import LinearRegression\n " ,
125
+ " \n " ,
126
+ " model = LinearRegression()\n " ,
127
+ " model.fit(x_train, y_train)"
128
+ ],
129
+ "execution_count" : null ,
130
+ "outputs" : []
131
+ },
132
+ {
133
+ "cell_type" : " code" ,
134
+ "metadata" : {
135
+ "id" : " bCbrRU6hvt3O"
136
+ },
137
+ "source" : [
138
+ " print(model.coef_)\n " ,
139
+ " print(model.intercept_)"
140
+ ],
141
+ "execution_count" : null ,
142
+ "outputs" : []
143
+ },
144
+ {
145
+ "cell_type" : " code" ,
146
+ "metadata" : {
147
+ "id" : " -ngs8G_5vt6e"
148
+ },
149
+ "source" : [
150
+ " pd.DataFrame(model.coef_, x.columns, columns = ['Coeff'])"
151
+ ],
152
+ "execution_count" : null ,
153
+ "outputs" : []
154
+ },
155
+ {
156
+ "cell_type" : " code" ,
157
+ "metadata" : {
158
+ "id" : " eVw4lndpC45e"
159
+ },
160
+ "source" : [
161
+ " predictions = model.predict(x_test)"
162
+ ],
163
+ "execution_count" : null ,
164
+ "outputs" : []
165
+ },
166
+ {
167
+ "cell_type" : " code" ,
168
+ "metadata" : {
169
+ "id" : " PblNR_9BC5BQ"
170
+ },
171
+ "source" : [
172
+ " plt.scatter(y_test, predictions)"
173
+ ],
174
+ "execution_count" : null ,
175
+ "outputs" : []
176
+ },
177
+ {
178
+ "cell_type" : " code" ,
179
+ "metadata" : {
180
+ "id" : " -3fxeEceC5Tf"
181
+ },
182
+ "source" : [
183
+ " plt.hist(y_test - predictions)"
184
+ ],
185
+ "execution_count" : null ,
186
+ "outputs" : []
187
+ },
188
+ {
189
+ "cell_type" : " code" ,
190
+ "metadata" : {
191
+ "id" : " EI_gS2JfDFHO"
192
+ },
193
+ "source" : [
194
+ " from sklearn import metrics\n " ,
195
+ " \n " ,
196
+ " metrics.mean_absolute_error(y_test, predictions)\n " ,
197
+ " metrics.mean_squared_error(y_test, predictions)\n " ,
198
+ " np.sqrt(metrics.mean_squared_error(y_test, predictions))"
199
+ ],
200
+ "execution_count" : null ,
201
+ "outputs" : []
202
+ }
203
+ ]
204
+ }
0 commit comments