-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinear Regression with Python.py
More file actions
186 lines (87 loc) · 3.15 KB
/
Linear Regression with Python.py
File metadata and controls
186 lines (87 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')
# ### Check out the Data
# In[256]:
USAhousing = pd.read_csv('USA_Housing.csv')
# In[257]:
USAhousing.head()
# In[258]:
USAhousing.info()
# In[259]:
USAhousing.describe()
# In[260]:
USAhousing.columns
# # EDA
#
# Let's create some simple plots to check out the data!
# In[261]:
sns.pairplot(USAhousing)
# In[262]:
sns.distplot(USAhousing['Price'])
# In[263]:
sns.heatmap(USAhousing.corr())
# ## Training a Linear Regression Model
#
# Let's now begin to train out regression model! We will need to first split up our data into an X array that contains the features to train on, and a y array with the target variable, in this case the Price column. We will toss out the Address column because it only has text info that the linear regression model can't use.
#
# ### X and y arrays
# In[264]:
X = USAhousing[['Avg. Area Income', 'Avg. Area House Age', 'Avg. Area Number of Rooms',
'Avg. Area Number of Bedrooms', 'Area Population']]
y = USAhousing['Price']
# ## Train Test Split
#
# Now let's split the data into a training set and a testing set. We will train out model on the training set and then use the test set to evaluate the model.
# In[265]:
from sklearn.model_selection import train_test_split
# In[266]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=101)
# ## Creating and Training the Model
# In[267]:
from sklearn.linear_model import LinearRegression
# In[268]:
lm = LinearRegression()
# In[269]:
lm.fit(X_train,y_train)
# ## Model Evaluation
#
# Let's evaluate the model by checking out it's coefficients and how we can interpret them.
# In[270]:
# print the intercept
print(lm.intercept_)
# In[277]:
coeff_df = pd.DataFrame(lm.coef_,X.columns,columns=['Coefficient'])
coeff_df
#
# ## Predictions from our Model
#
# Let's grab predictions off our test set and see how well it did!
# In[279]:
predictions = lm.predict(X_test)
# In[282]:
plt.scatter(y_test,predictions)
# **Residual Histogram**
# In[281]:
sns.distplot((y_test-predictions),bins=50);
#
# Comparing these metrics:
#
# - **MAE** is the easiest to understand, because it's the average error.
# - **MSE** is more popular than MAE, because MSE "punishes" larger errors, which tends to be useful in the real world.
# - **RMSE** is even more popular than MSE, because RMSE is interpretable in the "y" units.
#
# All of these are **loss functions**, because we want to minimize them.
# In[275]:
from sklearn import metrics
# In[276]:
print('MAE:', metrics.mean_absolute_error(y_test, predictions))
print('MSE:', metrics.mean_squared_error(y_test, predictions))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, predictions)))
# This was your first real Machine Learning Project! Congrats on helping your neighbor out! We'll let this end here for now, but go ahead and explore the Boston Dataset mentioned earlier if this particular data set was interesting to you!
#
# Up next is your own Machine Learning Project!
#
# ## Great Job!