-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcensus_data_exploration.py
More file actions
206 lines (179 loc) · 7.14 KB
/
census_data_exploration.py
File metadata and controls
206 lines (179 loc) · 7.14 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, roc_auc_score
train_path = r"us_census_full/census_income_learn.csv"
test_path = r"us_census_full/census_income_test.csv"
train_data = pd.read_csv(train_path)
test_data = pd.read_csv(test_path)
features = [
'age',
'class of worker',
'detailed industry recode',
'detailed occupation recode',
'education',
'wage per hour',
'enroll in edu inst last wk',
'marital stat',
'major industry code',
'major occupation code',
'race',
'hispanic origin',
'sex',
'member of a labor union',
'reason for unemployment',
'full or part time employment stat',
'capital gains',
'capital losses',
'dividends from stocks',
'tax filer stat',
'region of previous residence',
'state of previous residence',
'detailed household and family stat',
'detailed household summary in household',
'weight',
'migration code-change in msa',
'migration code-change in reg',
'migration code-move within reg',
'live in this house 1 year ago',
'migration prev res in sunbelt',
'num persons worked for employer',
'family members under 18',
'country of birth father',
'country of birth mother',
'country of birth self',
'citizenship',
'own business or self employed',
'fill inc questionnaire for veterans admin',
'veterans benefits',
'weeks worked in year',
'year',
'>50k'
]
train_data.columns = features
test_data.columns = features
print(train_data.head())
# DATA EXPLORATION
# 0 - class balance
print(train_data[">50k"].value_counts())
'''
>50k
-50000 187140
50000+. 12382
The target class is heavily unbalanced in the training set
'''
# 0 Data types
print(train_data.dtypes)
'''
>50k
-50000 187140
50000+. 12382
Name: count, dtype: int64
age int64
class of worker object
detailed industry recode int64
detailed occupation recode int64
education object
wage per hour int64
enroll in edu inst last wk object
marital stat object
major industry code object
major occupation code object
race object
hispanic origin object
sex object
member of a labor union object
reason for unemployment object
full or part time employment stat object
capital gains int64
capital losses int64
dividends from stocks int64
tax filer stat object
region of previous residence object
state of previous residence object
detailed household and family stat object
detailed household summary in household object
weight float64
migration code-change in msa object
migration code-change in reg object
migration code-move within reg object
live in this house 1 year ago object
migration prev res in sunbelt object
num persons worked for employer int64
family members under 18 object
country of birth father object
country of birth mother object
country of birth self object
citizenship object
own business or self employed int64
fill inc questionnaire for veterans admin object
veterans benefits int64
weeks worked in year int64
year int64
>50k object
dtype: object
'''
# combining capital gains and capital losses into a net capital gain feature
train_data['net capital gains'] = train_data['capital gains'] - train_data['capital losses']
test_data['net capital gains'] = test_data['capital gains'] - test_data['capital losses']
# removing features deemed to be redundant / irrelevant
features_to_remove = ["detailed industry recode", "detailed occupation recode", "weight", "year",
"region of previous residence", "enroll in edu inst last wk", "tax filer stat",
"detailed household and family stat", "migration code-change in msa",
"migration code-change in reg",
"migration code-move within reg", "live in this house 1 year ago",
"migration prev res in sunbelt",
"capital gains", "capital losses"]
train_data.drop(columns=features_to_remove)
test_data.drop(columns=features_to_remove)
# 1 - Histogram
hist_features = ['age','wage per hour']
for f in hist_features:
plt.figure(figsize=(8, 6))
sns.histplot(data=train_data, x=f, hue='>50k', kde=True)
plt.title(f'Distribution of {f} by Income')
plt.xlabel(f)
plt.ylabel('Count')
plt.legend(title='Income', labels=['<=50k', '>50k'])
plt.show()
# 2 - Bar plots
bar_features = ['class of worker', 'education', 'marital stat', 'race', 'sex', 'own business or self employed']
plt.figure(figsize=(15, 10))
for i, f in enumerate(bar_features):
plt.subplot(3, 2, i + 1) # 3x2 grid
sns.countplot(data=train_data, y=f, hue='>50k')
plt.title(f'Frequency of {f} by Income')
plt.xlabel("Count")
plt.ylabel(f)
plt.legend(title='Income', labels=['<=50k', '>50k'])
plt.tight_layout()
plt.show()
f = "major occupation code"
plt.figure(figsize=(8, 6))
sns.countplot(data=train_data, y=f, hue='>50k')
plt.title(f'Frequency of {f} by Income')
plt.xlabel("Count")
plt.ylabel(f)
plt.legend(title='Income', labels=['<=50k', '>50k'])
plt.show()
# 3 - violin plots
v_features = ['age', 'wage per hour', 'net capital gains', 'weeks worked in year']
plt.figure(figsize=(15, 10))
for i, f in enumerate(v_features):
plt.subplot(2, 2, i + 1) # 2x2 grid
sns.violinplot(data=train_data, x='>50k', y=f)
plt.title(f'Distribution of {f} by Income')
plt.xlabel('Income')
plt.ylabel(f)
plt.tight_layout()
plt.show()
# 4 - pairplots (of a sample of the training set)
f = ["age", "wage per hour", "weeks worked in year", "net capital gains"]
sns.pairplot(data=train_data[:5000], vars=f,
hue=">50k") # only take data from the first 5000 samples to improve the readability of the plot
plt.show()
f = ["net capital gains", "dividends from stocks"]
sns.pairplot(data=train_data[:5000], vars=f,
hue=">50k") # only take data from the first 5000 samples to improve the readability of the plot
plt.show()