-
Notifications
You must be signed in to change notification settings - Fork 0
/
Updated Linear Regression Code
382 lines (335 loc) · 14.6 KB
/
Updated Linear Regression Code
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import pandas as pd
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from scipy import stats
chunk = pd.read_csv('NY DATAFRAME FINAL.csv',chunksize = 10000000)
df_NY = pd.concat(chunk)
chunk = pd.read_csv('LA DATAFRAME FINAL.csv',chunksize = 10000000)
df_LA = pd.concat(chunk)
chunk = pd.read_csv('CHICAGO DATAFRAME FINAL.csv',chunksize = 10000000)
df_CHI = pd.concat(chunk)
#Test whether the datasets are statistically different
#Perform a linear regression:
#More precisely, linear regression is used to determine the character and strength
#of the association between a dependent variable and a series of other independent
#variables
#Do victims have an effect as a dependent variable?
#Normal graph to show trends over time
contingency_NY = pd.crosstab(df_NY['CRIME_DESCRIPTION'], df_NY['YEAR'])
contingency_LA = pd.crosstab(df_LA['CRIME_DESCRIPTION'], df_LA['YEAR'])
contingency_CHI = pd.crosstab(df_CHI['CRIME_DESCRIPTION'], df_CHI['YEAR'])
print(contingency_CHI)
# contingency_CHI.info()
#CHI Linear regression
##CHANGE contigency_NY to contigency_LA
NYarson = contingency_NY.iloc[0, :].values.reshape((-1, 1))
NYbattery = contingency_NY.iloc[1, :].values.reshape((-1, 1))
NYburglary = contingency_NY.iloc[2, :].values.reshape((-1, 1))
NYdangerousWeapons= contingency_NY.iloc[3, :].values.reshape((-1, 1))
NYhomicide = contingency_NY.iloc[4, :].values.reshape((-1, 1))
NYnarcotics = contingency_NY.iloc[5, :].values.reshape((-1, 1))
NYpetitLarceny = contingency_NY.iloc[6, :].values.reshape((-1, 1))
NYrobbery = contingency_NY.iloc[7, :].values.reshape((-1, 1))
NYsexualAssault = contingency_NY.iloc[8, :].values.reshape((-1, 1))
LAarson = contingency_LA.iloc[0, :].values.reshape((-1, 1))
LAbattery = contingency_LA.iloc[1, :].values.reshape((-1, 1))
LAburglary = contingency_LA.iloc[2, :].values.reshape((-1, 1))
LAdangerousWeapons= contingency_LA.iloc[3, :].values.reshape((-1, 1))
LAhomicide = contingency_LA.iloc[4, :].values.reshape((-1, 1))
LAnarcotics = contingency_LA.iloc[5, :].values.reshape((-1, 1))
LApetitLarceny = contingency_LA.iloc[6, :].values.reshape((-1, 1))
LArobbery = contingency_LA.iloc[7, :].values.reshape((-1, 1))
LAsexualAssault = contingency_LA.iloc[8, :].values.reshape((-1, 1))
arson = contingency_CHI.iloc[0, :].values.reshape((-1,1))
battery = contingency_CHI.iloc[1, :].values.reshape((-1,1))
burglary = contingency_CHI.iloc[2, :].values.reshape((-1,1))
dangerousWeapons= contingency_CHI.iloc[3, :].values.reshape((-1,1))
homicide = contingency_CHI.iloc[4, :].values.reshape((-1,1))
narcotics = contingency_CHI.iloc[5, :].values.reshape((-1,1))
petitLarceny = contingency_CHI.iloc[6, :].values.reshape((-1,1))
robbery = contingency_CHI.iloc[7, :].values.reshape((-1,1))
sexualAssault = contingency_CHI.iloc[8, :].values.reshape((-1,1))
Year = np.array([1,2,3,4,5]).reshape((-1,1))
##NY to LA
NYmodel1 = LinearRegression().fit(Year, NYarson)
NYmodel2 = LinearRegression().fit(Year, NYbattery)
NYmodel3 = LinearRegression().fit(Year, NYburglary)
NYmodel4 = LinearRegression().fit(Year, NYdangerousWeapons)
NYmodel5 = LinearRegression().fit(Year, NYhomicide)
NYmodel6 = LinearRegression().fit(Year, NYnarcotics)
NYmodel7 = LinearRegression().fit(Year, NYpetitLarceny)
NYmodel8 = LinearRegression().fit(Year, NYrobbery)
NYmodel9 = LinearRegression().fit(Year, NYsexualAssault)
LAmodel1 = LinearRegression().fit(Year, LAarson)
LAmodel2 = LinearRegression().fit(Year, LAbattery)
LAmodel3 = LinearRegression().fit(Year, LAburglary)
LAmodel4 = LinearRegression().fit(Year, LAdangerousWeapons)
LAmodel5 = LinearRegression().fit(Year, LAhomicide)
LAmodel6 = LinearRegression().fit(Year, LAnarcotics)
LAmodel7 = LinearRegression().fit(Year, LApetitLarceny)
LAmodel8 = LinearRegression().fit(Year, LArobbery)
LAmodel9 = LinearRegression().fit(Year, LAsexualAssault)
CHImodel1 = LinearRegression().fit(Year, arson)
CHImodel2 = LinearRegression().fit(Year, battery)
CHImodel3 = LinearRegression().fit(Year, burglary)
CHImodel4 = LinearRegression().fit(Year, dangerousWeapons)
CHImodel5 = LinearRegression().fit(Year, homicide)
CHImodel6 = LinearRegression().fit(Year, narcotics)
CHImodel7 = LinearRegression().fit(Year, petitLarceny)
CHImodel8 = LinearRegression().fit(Year, robbery)
CHImodel9 = LinearRegression().fit(Year, sexualAssault)
print(plt.scatter(Year, NYarson))
print(plt.title("Arson"))
plt.plot(Year, NYmodel1.coef_ * Year + NYmodel1.intercept_)
r_sq = NYmodel1.score(Year, NYarson)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {NYmodel1.intercept_}")
print(f"The slope is : {NYmodel1.coef_}")
y_pred1 = NYmodel1.predict(Year)
print(f"The predicted responses are: \n{y_pred1}")
print()
r_sq = LAmodel1.score(Year, LAarson)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {LAmodel1.intercept_}")
print(f"The slope is : {LAmodel1.coef_}")
y_pred1 = LAmodel1.predict(Year)
print(f"The predicted responses are: \n{y_pred1}")
print(plt.scatter(Year, LAarson))
plt.plot(Year, LAmodel1.coef_ * Year + LAmodel1.intercept_)
print()
print(plt.scatter(Year, arson))
plt.plot(Year, CHImodel1.coef_ * Year + CHImodel1.intercept_)
r_sq = CHImodel2.score(Year, battery)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {CHImodel2.intercept_}")
print(f"The slope is : {CHImodel2.coef_}")
y_pred2 = CHImodel2.predict(Year)
print(f"The predicted responses are: \n{y_pred2}")
print()
print(plt.scatter(Year, NYbattery))
print(plt.title("Battery"))
plt.plot(Year, NYmodel2.coef_ * Year + NYmodel2.intercept_)
r_sq = NYmodel2.score(Year, NYbattery)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {NYmodel2.intercept_}")
print(f"The slope is : {NYmodel2.coef_}")
y_pred2 = NYmodel2.predict(Year)
print(f"The predicted responses are: \n{y_pred2}")
print()
print(plt.scatter(Year, LAbattery))
plt.plot(Year, LAmodel2.coef_ * Year + LAmodel2.intercept_)
r_sq = LAmodel2.score(Year, LAbattery)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {LAmodel2.intercept_}")
print(f"The slope is : {LAmodel2.coef_}")
y_pred2 = LAmodel2.predict(Year)
print(f"The predicted responses are: \n{y_pred2}")
print()
print(plt.scatter(Year, battery))
plt.plot(Year, CHImodel2.coef_ * Year + CHImodel2.intercept_)
r_sq = CHImodel2.score(Year, battery)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {CHImodel2.intercept_}")
print(f"The slope is : {CHImodel2.coef_}")
y_pred2 = CHImodel2.predict(Year)
print(f"The predicted responses are: \n{y_pred2}")
print()
print(plt.scatter(Year, NYburglary))
print(plt.title("Burglary"))
plt.plot(Year, NYmodel3.coef_ * Year + NYmodel3.intercept_)
r_sq = NYmodel3.score(Year, NYburglary)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {NYmodel3.intercept_}")
print(f"The slope is : {NYmodel3.coef_}")
y_pred3 = NYmodel3.predict(Year)
print(f"The predicted responses are: \n{y_pred3}")
print()
print(plt.scatter(Year, LAburglary))
plt.plot(Year, LAmodel3.coef_ * Year + LAmodel3.intercept_)
r_sq = LAmodel3.score(Year, LAburglary)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {LAmodel3.intercept_}")
print(f"The slope is : {LAmodel3.coef_}")
y_pred3 = LAmodel3.predict(Year)
print(f"The predicted responses are: \n{y_pred3}")
print()
print(plt.scatter(Year, burglary))
plt.plot(Year, CHImodel3.coef_ * Year + CHImodel3.intercept_)
r_sq = CHImodel3.score(Year, burglary)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {CHImodel3.intercept_}")
print(f"The slope is : {CHImodel3.coef_}")
y_pred3 = CHImodel3.predict(Year)
print(f"The predicted responses are: \n{y_pred3}")
print()
print(plt.scatter(Year, NYdangerousWeapons))
print(plt.title("Dangerous Weapons"))
plt.plot(Year, NYmodel4.coef_ * Year + NYmodel4.intercept_)
r_sq = NYmodel4.score(Year, NYdangerousWeapons)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {NYmodel4.intercept_}")
print(f"The slope is : {NYmodel4.coef_}")
y_pred4 = NYmodel4.predict(Year)
print(f"The predicted responses are: \n{y_pred4}")
print()
print(plt.scatter(Year, LAdangerousWeapons))
plt.plot(Year, LAmodel4.coef_ * Year + LAmodel4.intercept_)
r_sq = LAmodel4.score(Year, LAdangerousWeapons)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {LAmodel4.intercept_}")
print(f"The slope is : {LAmodel4.coef_}")
y_pred4 = LAmodel4.predict(Year)
print(f"The predicted responses are: \n{y_pred4}")
print()
print(plt.scatter(Year, dangerousWeapons))
plt.plot(Year, CHImodel4.coef_ * Year + CHImodel4.intercept_)
r_sq = CHImodel4.score(Year, dangerousWeapons)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {CHImodel4.intercept_}")
print(f"The slope is : {CHImodel4.coef_}")
y_pred4 = CHImodel4.predict(Year)
print(f"The predicted responses are: \n{y_pred4}")
print()
print(plt.scatter(Year, NYhomicide))
print(plt.title("Homicide"))
plt.plot(Year, NYmodel5.coef_ * Year + NYmodel5.intercept_)
r_sq = NYmodel5.score(Year, NYhomicide)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {NYmodel5.intercept_}")
print(f"The slope is : {NYmodel5.coef_}")
y_pred5 = NYmodel5.predict(Year)
print(f"The predicted responses are: \n{y_pred5}")
print()
print(plt.scatter(Year, LAhomicide))
plt.plot(Year, LAmodel5.coef_ * Year + LAmodel5.intercept_)
r_sq = LAmodel5.score(Year, LAhomicide)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {LAmodel5.intercept_}")
print(f"The slope is : {LAmodel5.coef_}")
y_pred5 = LAmodel5.predict(Year)
print(f"The predicted responses are: \n{y_pred5}")
print()
print(plt.scatter(Year, homicide))
plt.plot(Year, CHImodel5.coef_ * Year + CHImodel5.intercept_)
r_sq = CHImodel5.score(Year, homicide)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {CHImodel5.intercept_}")
print(f"The slope is : {CHImodel5.coef_}")
y_pred5 = CHImodel5.predict(Year)
print(f"The predicted responses are: \n{y_pred5}")
print()
print(plt.scatter(Year, NYnarcotics))
print(plt.title("Narcotics"))
plt.plot(Year, NYmodel6.coef_ * Year + NYmodel6.intercept_)
r_sq = NYmodel6.score(Year, NYnarcotics)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {NYmodel6.intercept_}")
print(f"The slope is : {NYmodel6.coef_}")
y_pred6 = NYmodel6.predict(Year)
print(f"The predicted responses are: \n{y_pred6}")
print()
print(plt.scatter(Year, LAnarcotics))
plt.plot(Year, LAmodel6.coef_ * Year + LAmodel6.intercept_)
r_sq = LAmodel6.score(Year, LAnarcotics)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {LAmodel6.intercept_}")
print(f"The slope is : {LAmodel6.coef_}")
y_pred6 = LAmodel6.predict(Year)
print(f"The predicted responses are: \n{y_pred6}")
print()
print(plt.scatter(Year, narcotics))
plt.plot(Year, CHImodel6.coef_ * Year + CHImodel6.intercept_)
r_sq = CHImodel6.score(Year, narcotics)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {CHImodel6.intercept_}")
print(f"The slope is : {CHImodel6.coef_}")
y_pred6 = CHImodel6.predict(Year)
print(f"The predicted responses are: \n{y_pred6}")
print()
print(plt.scatter(Year, NYpetitLarceny))
print(plt.title("Petit Larceny"))
plt.plot(Year, NYmodel7.coef_ * Year + NYmodel7.intercept_)
r_sq = NYmodel7.score(Year, NYpetitLarceny)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {NYmodel7.intercept_}")
print(f"The slope is : {NYmodel7.coef_}")
y_pred7 = NYmodel7.predict(Year)
print(f"The predicted responses are: \n{y_pred7}")
print()
print(plt.scatter(Year, LApetitLarceny))
plt.plot(Year, LAmodel7.coef_ * Year + LAmodel7.intercept_)
r_sq = LAmodel7.score(Year, LApetitLarceny)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {LAmodel7.intercept_}")
print(f"The slope is : {LAmodel7.coef_}")
y_pred7 = LAmodel7.predict(Year)
print(f"The predicted responses are: \n{y_pred7}")
print()
print(plt.scatter(Year, petitLarceny))
plt.plot(Year, CHImodel7.coef_ * Year + CHImodel7.intercept_)
r_sq = CHImodel7.score(Year, petitLarceny)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {CHImodel7.intercept_}")
print(f"The slope is : {CHImodel7.coef_}")
y_pred7 = CHImodel7.predict(Year)
print(f"The predicted responses are: \n{y_pred7}")
print()
print(plt.scatter(Year, NYrobbery))
print(plt.title("Robbery"))
plt.plot(Year, NYmodel8.coef_ * Year + NYmodel8.intercept_)
r_sq = NYmodel8.score(Year, NYrobbery)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {NYmodel8.intercept_}")
print(f"The slope is : {NYmodel8.coef_}")
y_pred8 = NYmodel8.predict(Year)
print(f"The predicted responses are: \n{y_pred8}")
print()
print(plt.scatter(Year, LArobbery))
plt.plot(Year, LAmodel8.coef_ * Year + LAmodel8.intercept_)
r_sq = LAmodel8.score(Year, LArobbery)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {LAmodel8.intercept_}")
print(f"The slope is : {LAmodel8.coef_}")
y_pred8 = LAmodel8.predict(Year)
print(f"The predicted responses are: \n{y_pred8}")
print()
print(plt.scatter(Year, robbery))
plt.plot(Year, CHImodel8.coef_ * Year + CHImodel8.intercept_)
r_sq = CHImodel8.score(Year, robbery)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {CHImodel8.intercept_}")
print(f"The slope is : {CHImodel8.coef_}")
y_pred8 = CHImodel8.predict(Year)
print(f"The predicted responses are: \n{y_pred8}")
print()
print(plt.scatter(Year, NYsexualAssault))
print(plt.title("Sexual Assault"))
plt.plot(Year, NYmodel9.coef_ * Year + NYmodel9.intercept_)
r_sq = NYmodel9.score(Year, NYsexualAssault)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {NYmodel9.intercept_}")
print(f"The slope is : {NYmodel9.coef_}")
y_pred9 = NYmodel9.predict(Year)
print(f"The predicted responses are: \n{y_pred9}")
print()
print(plt.scatter(Year, LAsexualAssault))
plt.plot(Year, LAmodel9.coef_ * Year + LAmodel9.intercept_)
r_sq = LAmodel9.score(Year, LAsexualAssault)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {LAmodel9.intercept_}")
print(f"The slope is : {LAmodel9.coef_}")
y_pred9 = LAmodel9.predict(Year)
print(f"The predicted responses are: \n{y_pred9}")
print()
print(plt.scatter(Year, sexualAssault))
plt.plot(Year, CHImodel9.coef_ * Year + CHImodel9.intercept_)
r_sq = CHImodel9.score(Year, sexualAssault)
print(f"The coefficient of determination is: {r_sq}")
print(f"The intercept is: {CHImodel9.intercept_}")
print(f"The slope is : {CHImodel9.coef_}")
y_pred9 = CHImodel9.predict(Year)
print(f"The predicted responses are: \n{y_pred9}")
print()