-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-investigation.py
334 lines (272 loc) · 10.4 KB
/
data-investigation.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
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
# Python Data Plotting for Census Data
import pandas as pd
from pandas.plotting import scatter_matrix
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn import preprocessing
from sklearn.model_selection import GridSearchCV, cross_val_score, cross_val_predict, StratifiedKFold, learning_curve, train_test_split, KFold
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.metrics import classification_report
from sklearn.svm import SVC
from scipy.stats import ttest_ind, ttest_rel
from scipy import stats
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
#redefine missing values
missing_values = [" ?", "n/a","--", "nan"]
data = pd.read_csv("../input/thedata/adult.csv", na_values = missing_values)
data[data == '?'] = np.nan #helps machine realise that ? values are nan
for col in ['work_class', 'occupation', 'nativecountry']:
data[col].fillna(data[col].mode()[0], inplace=True) #replace missing values with mode value
X = data.drop(['under_over'], axis=1)
y = data['under_over']
#separate data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)
#transform any categorical data
categorical = ['work_class', 'education', 'marital_status', 'occupation', 'relationship', 'race', 'sex', 'nativecountry']
for feature in categorical:
encoder = preprocessing.LabelEncoder()
X_train[feature] = encoder.fit_transform(X_train[feature])
X_test[feature] = encoder.transform(X_test[feature])
#scale the features
sk_scaler = StandardScaler()
X_train = pd.DataFrame(sk_scaler.fit_transform(X_train), columns = X.columns)
X_test = pd.DataFrame(sk_scaler.transform(X_test), columns = X.columns)
accuracyList = []
#logistic regression model using all the features
regression = LogisticRegression()
regression.fit(X_train, y_train)
y_predict = regression.predict(X_test)
theAccuracy = accuracy_score(y_test, y_pred)
print('Logistic regression:', theAccuracy)
accuracyList.append(theAccuracy)
#below is the KNN portion
knnClassifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
knnClassifier.fit(X_train, y_train)
y_predict = knnClassifier.predict(X_test)
theAccuracy = accuracy_score(y_test, y_pred)
print('K nearest neighbour:', theAccuracy)
accuracyList.append(theAccuracy)
#below is the decision tree
dtClassifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
dtClassifier.fit(X_train, y_train)
y_predict = dtClassifier.predict(X_test)
theAccuracy = accuracy_score(y_test, y_pred)
print('Decision tree:', theAccuracy)
accuracyList.append(theAccuracy)
rfClassifier = RandomForestClassifier(n_estimators = 300, criterion = 'entropy', random_state = 0)
rfClassifier.fit(X_train, y_train)
y_predict = rfClassifier.predict(X_test)
theAccuracy = accuracy_score(y_test, y_pred)
print('Random forest:', theAccuracy)
accuracyList.append(theAccuracy)
print(confusion_matrix(y_test, y_predict))
print(classification_report(y_test, y_predict))
#evaluating the algorithms
axisY =['Logistic Regression',
'K-Neighbors Classifier',
'Decision Tree Classifier',
'Random Forest Classifier']
axisX=accuracyList
sns.barplot(x=axisX,y=axisY)
plt.xlabel('Accuracy')
data.sample(10)
data.isna().values.any() #check if any values are missing (result returns false as it doesn't recognise ? as missing values)
data.shape #find the shape of the dataset (result: 32651 instances split into 15 features)
data.info() # to check data types and other info; this tells us there are 5 numerical features and 9 categorical features
data["age"].hist(figsize=(10,10))
plt.xlabel('Age')
plt.ylabel('Count')
plt.show()
data["fnlwgt"].hist(figsize=(10,10))
plt.xlabel('Final Weight')
plt.ylabel('Count')
plt.show()
data["education_num"].hist(figsize=(10,10))
plt.xlabel('Educational Number')
plt.ylabel('Count')
plt.show()
data["capital_gain"].hist(figsize=(10,10))
plt.xlabel('Capital Gain')
plt.ylabel('Count')
plt.show()
data["capital_loss"].hist(figsize=(10,10))
plt.xlabel('Capital Loss')
plt.ylabel('Count')
plt.show()
data["hours_pw"].hist(figsize=(10,10))
plt.xlabel('Hours worked (per week)')
plt.ylabel('Count')
plt.show()
plt.figure(figsize=(15,9))
x = sns.countplot(x="work_class", data=data)
for p in x.patches:
height = p.get_height()
x.text(p.get_x()+p.get_width()/2.,
height + 3,
'{:1.2f}'.format((height/total)*100),
ha="center")
plt.show()
x = sns.countplot(x="education", data=data)
for p in x.patches:
height = p.get_height()
x.text(p.get_x()+p.get_width()/2.,
height + 3,
'{:1.2f}'.format((height/total)*100),
ha="center")
plt.show()
x = sns.countplot(x="marital_status", data=data)
for p in x.patches:
height = p.get_height()
x.text(p.get_x()+p.get_width()/2.,
height + 3,
'{:1.2f}'.format((height/total)*100),
ha="center")
plt.show()
x = sns.countplot(x="occupation", data=data)
for p in x.patches:
height = p.get_height()
x.text(p.get_x()+p.get_width()/2.,
height + 3,
'{:1.2f}'.format((height/total)*100),
ha="center")
plt.show()
x = sns.countplot(x="relationship", data=data)
for p in x.patches:
height = p.get_height()
x.text(p.get_x()+p.get_width()/2.,
height + 3,
'{:1.2f}'.format((height/total)*100),
ha="center")
plt.show()
x = sns.countplot(x="sex", data=data)
for p in x.patches:
height = p.get_height()
x.text(p.get_x()+p.get_width()/2.,
height + 3,
'{:1.2f}'.format((height/total)*100),
ha="center")
plt.show()
x = sns.countplot(x="race", data=data)
for p in x.patches:
height = p.get_height()
x.text(p.get_x()+p.get_width()/2.,
height + 3,
'{:1.2f}'.format((height/total)*100),
ha="center")
plt.show()
x = sns.countplot(x="nativecountry", data=data)
for p in x.patches:
height = p.get_height()
x.text(p.get_x()+p.get_width()/2.,
height + 3,
'{:1.2f}'.format((height/total)*100),
ha="center")
plt.show()
x = sns.countplot(x="under_over", data=data)
for p in x.patches:
height = p.get_height()
x.text(p.get_x()+p.get_width()/2.,
height + 3,
'{:1.2f}'.format((height/total)*100),
ha="center")
plt.show()
x = sns.countplot(x="sex", data=data)
for p in x.patches:
height = p.get_height()
x.text(p.get_x()+p.get_width()/2.,
height + 3,
'{:1.2f}'.format((height/total)*100),
ha="center")
plt.show()
fig = plt.figure(figsize=(15,9))
crosstab2=pd.crosstab(data["age"],data["under_over"], normalize='index', margins=False)*100
print(crosstab2)
crosstab2.plot(kind = 'bar', stacked = True)
fig = plt.figure(figsize=(15,9))
crosstab2=pd.crosstab(data["work_class"],data["under_over"], normalize='index', margins=False)*100
print(crosstab2)
crosstab2.plot(kind = 'bar', stacked = True)
fig = plt.figure(figsize=(15,9))
crosstab2=pd.crosstab(data["education"],data["under_over"], normalize='index', margins=False)*100
print(crosstab2)
crosstab2.plot(kind = 'bar', stacked = True)
fig = plt.figure(figsize=(15,9))
crosstab2=pd.crosstab(data["education_num"],data["under_over"], normalize='index', margins=False)*100
print(crosstab2)
crosstab2.plot(kind = 'bar', stacked = True)
fig = plt.figure(figsize=(15,9))
crosstab2=pd.crosstab(data["marital_status"],data["under_over"], normalize='index', margins=False)*100
print(crosstab2)
crosstab2.plot(kind = 'bar', stacked = True)
fig = plt.figure(figsize=(15,9))
crosstab2=pd.crosstab(data["occupation"],data["under_over"], normalize='index', margins=False)*100
print(crosstab2)
crosstab2.plot(kind = 'bar', stacked = True)
fig = plt.figure(figsize=(15,9))
crosstab2=pd.crosstab(data["relationship"],data["under_over"], normalize='index', margins=False)*100
print(crosstab2)
crosstab2.plot(kind = 'bar', stacked = True)
fig = plt.figure(figsize=(15,9))
crosstab2=pd.crosstab(data["race"],data["under_over"], normalize='index', margins=False)*100
print(crosstab2)
crosstab2.plot(kind = 'bar', stacked = True)
fig = plt.figure(figsize=(15,9))
crosstab2=pd.crosstab(data["sex"],data["under_over"], normalize='index', margins=False)*100
print(crosstab2)
crosstab2.plot(kind = 'bar', stacked = True)
plt.figure(figsize=(12, 8))
sns.boxplot(x="fnlwgt", y="under_over", data=data)
plt.show()
plt.figure(figsize=(12, 8))
sns.boxplot(x="capital_gain", y="under_over", data=data)
plt.show()
plt.figure(figsize=(12, 8))
sns.boxplot(x="capital_loss", y="under_over", data=data)
plt.show()
plt.figure(figsize=(12, 8))
sns.boxplot(x="hours_pw", y="under_over", data=data)
plt.show()
box plots to identify outliers
num_feat = data.select_dtypes(include=['int64']).columns
for i in range(6):
plt.subplot(2,3,i+1)
plt.boxplot(data[num_feat[i]])
plt.title(num_feat[i],color="g",fontsize=22)
plt.yticks(fontsize=15)
plt.xticks(fontsize=15)
plt.show()
removing outliers through winsorization
from scipy.stats.mstats import winsorize
data["age"] = winsorize(data["age"],(0,0.15))
data["fnlwgt"] = winsorize(data["fnlwgt"],(0,0.15))
data["capital_gain"] = winsorize(data["capital_gain"],(0,0.099))
data["capital_loss"] = winsorize(data["capital_loss"],(0,0.099))
data["hours_pw"] = winsorize(data["hours_pw"],(0.12,0.18))
plt.rcParams['figure.figsize'] = (25,7)
baslik_font = {'family':'arial','color':'red','weight':'bold','size':25}
col_list=['age',"fnlwgt",'capital_gain', 'capital_loss', 'hours_pw']
for i in range(5):
plt.subplot(1,5,i+1)
plt.boxplot(data[col_list[i]])
plt.title(col_list[i],fontdict=baslik_font)
plt.show()
data.head(10)
numerical = ['int64']
numericData = data.select_dtypes(include=numerical) #organise numerical data to be parsed separately
categorical = ['object']
categoricData = data.select_dtypes(include=categorical) #organise categorical data to be parsed separately
print(numericData.describe()) #describe numerical dataset and return min/max/std etc
print("Mode", categoricData.mode()) #display mode (most frequent attributes)