-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoccupationPrediction.py
111 lines (103 loc) · 3.88 KB
/
occupationPrediction.py
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
import pandas as pd
import glob
import numpy as np
from sklearn.linear_model import LinearRegression
regr = LinearRegression()
byOccupation = glob.glob("Data\Final CSV Files\By Occupation\*.csv")
toSort = {}
for path in byOccupation:
df = pd.read_csv(path, usecols=["Characteristic", "fatal_injury_rate"])
toSort[path] = list(df.mean())[0]
verSmall = []
small = []
notLarge = []
medium = []
larger = []
big = []
p = []
chara = []
for key, val in toSort.items():
if (val < 1):
verSmall.append(key)
elif (val > 1 and val <= 5):
small.append(key)
elif (val > 5 and val <= 10):
notLarge.append(key)
elif (val > 10 and val <= 20):
medium.append(key)
elif (val > 20 and val <= 30):
larger.append(key)
else:
big.append(key)
df = pd.concat((pd.read_csv(f, usecols=["Characteristic", "fatal_injury_rate"]) for f in verSmall), ignore_index=True)
charaCur = df.Characteristic.unique()
chara.extend(charaCur)
for i in range(len(charaCur)):
df.loc[df["Characteristic"] == charaCur[i], "Characteristic"] = float(i)
X = np.array(df["Characteristic"]).reshape(-1, 1)
y = np.array(df["fatal_injury_rate"]).reshape(-1, 1)
regr.fit(X, y)
test = np.array([[i] for i in range(len(charaCur))])
pCur = regr.predict(test).flatten()
p.extend(pCur)
df = pd.concat((pd.read_csv(f, usecols=["Characteristic", "fatal_injury_rate"]) for f in small), ignore_index=True)
charaCur = df.Characteristic.unique()
chara.extend(charaCur)
for i in range(len(charaCur)):
df.loc[df["Characteristic"] == charaCur[i], "Characteristic"] = float(i)
X = np.array(df["Characteristic"]).reshape(-1, 1)
y = np.array(df["fatal_injury_rate"]).reshape(-1, 1)
regr.fit(X, y)
test = np.array([[i] for i in range(len(charaCur))])
pCur = regr.predict(test).flatten()
p.extend(pCur)
df = pd.concat((pd.read_csv(f, usecols=["Characteristic", "fatal_injury_rate"]) for f in notLarge), ignore_index=True)
charaCur = df.Characteristic.unique()
chara.extend(charaCur)
for i in range(len(charaCur)):
df.loc[df["Characteristic"] == charaCur[i], "Characteristic"] = float(i)
X = np.array(df["Characteristic"]).reshape(-1, 1)
y = np.array(df["fatal_injury_rate"]).reshape(-1, 1)
regr.fit(X, y)
test = np.array([[i] for i in range(len(charaCur))])
pCur = regr.predict(test).flatten()
p.extend(pCur)
df = pd.concat((pd.read_csv(f, usecols=["Characteristic", "fatal_injury_rate"]) for f in medium), ignore_index=True)
charaCur = df.Characteristic.unique()
chara.extend(charaCur)
for i in range(len(charaCur)):
df.loc[df["Characteristic"] == charaCur[i], "Characteristic"] = float(i)
X = np.array(df["Characteristic"]).reshape(-1, 1)
y = np.array(df["fatal_injury_rate"]).reshape(-1, 1)
regr.fit(X, y)
test = np.array([[i] for i in range(len(charaCur))])
pCur = regr.predict(test).flatten()
p.extend(pCur)
df = pd.concat((pd.read_csv(f, usecols=["Characteristic", "fatal_injury_rate"]) for f in larger), ignore_index=True)
charaCur = df.Characteristic.unique()
chara.extend(charaCur)
for i in range(len(charaCur)):
df.loc[df["Characteristic"] == charaCur[i], "Characteristic"] = float(i)
X = np.array(df["Characteristic"]).reshape(-1, 1)
y = np.array(df["fatal_injury_rate"]).reshape(-1, 1)
regr.fit(X, y)
test = np.array([[i] for i in range(len(charaCur))])
pCur = regr.predict(test).flatten()
p.extend(pCur)
df = pd.concat((pd.read_csv(f, usecols=["Characteristic", "fatal_injury_rate"]) for f in big), ignore_index=True)
charaCur = df.Characteristic.unique()
chara.extend(charaCur)
for i in range(len(charaCur)):
df.loc[df["Characteristic"] == charaCur[i], "Characteristic"] = float(i)
X = np.array(df["Characteristic"]).reshape(-1, 1)
y = np.array(df["fatal_injury_rate"]).reshape(-1, 1)
regr.fit(X, y)
test = np.array([[i] for i in range(len(charaCur))])
pCur = regr.predict(test).flatten()
p.extend(pCur)
predictions = {
"Characteristic": chara,
"Predicted Injury Rate": p
}
pred = pd.DataFrame(predictions)
print(pred)