-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonProject.py
More file actions
276 lines (207 loc) · 9.1 KB
/
PythonProject.py
File metadata and controls
276 lines (207 loc) · 9.1 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 6 10:15:53 2019
@author: eleanorezimah
"""
import csv
import pandas as pd
import numpy
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split # Import train_test_split function
from sklearn.metrics import accuracy_score
from sklearn.naive_bayes import BernoulliNB
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from tabulate import tabulate
import matplotlib.pyplot as plt
import re
data = pd.read_csv(r"/Users/eleanorezimah/Desktop/AIT 590/Experimental Project/training_data.csv")
df = pd.DataFrame(data)
# print(df)
df.replace(' ?', numpy.nan, regex=False, inplace=True)
print(df.columns[df.isna().any()])
#print(tabulate(df, tablefmt='psql'))
# print(df.mean())
df=df.fillna(df.mean())
# print(tabulate(df, tablefmt='psql'))
print(df['workclass'].describe())
common_value = 'Private'
df['workclass'] = df['workclass'].fillna(common_value)
# print(tabulate(df, tablefmt='psql'))
print(df['occupation'].describe())
common_value = 'Prof-speciality'
df['occupation'] = df['occupation'].fillna(common_value)
# print(tabulate(df, tablefmt='psql'))
print(df['native-country'].describe())
common_value = 'United-States'
df['native-country'] = df['native-country'].fillna(common_value)
# print(tabulate(df, tablefmt='psql'))
print(type(df['salary']))
# numpy.isnan(df)
df['sex'].replace([' Male', ' Female'], [1, 0], regex=False, inplace=True)
print(df['sex'])
#df.salary.replace([' <=50K', ' >50K'], [1, 0], inplace=True)
# print(tabulate(df, tablefmt='psql'))
# ....data clean: education num........
# data can be train or test
# var name is variable name: should be passed as strings within ('')
# bins is list of numeric values like [0,6,10,11]
# group names is list of groups you want to create in list form
def bin_var(data, var, bins, group_names):
bin_value = bins
group = group_names
data[var + 'Cat'] = pd.cut(df[var], bin_value, labels=group)
bin_var(df, 'education-num', [0, 6, 11, 16], [0, 1, 2])
# bin_var(test, 'Education Num', [0,6,11,16], ['Low', 'Medium', 'High'])
print(pd.crosstab(df['education-numCat'], df['salary']))
df['education-numCat'] = df['education-numCat'].astype('int64')
# ..............data clean:hours/week.......
bin_var(df, 'hours-per-week', [0, 35, 40, 60, 100], [0, 1, 2, 3])
# bin_var(test, 'Hours/Week', [0,35,40,60,100], ['Low', 'Medium', 'High','VeryHigh'])
print(pd.crosstab(df['hours-per-weekCat'], df['salary'], margins=True))
df['hours-per-weekCat'] = df['hours-per-weekCat'].astype('int64')
# ...........data clean:occupation..........
def occup(x):
if re.search('managerial', x):
return 1
elif re.search('specialty', x):
return 1
else:
return 0
df['occupationCat'] = df.occupation.apply(lambda x: x.strip()).apply(lambda x: occup(x))
# df['occupation']=df.occupation.apply(lambda x: x.strip()).apply(lambda x: occup(x))
print(df['occupationCat'].value_counts())
# ................age............
bin_var(df, 'age', [17, 30, 55, 100], [0, 1, 2])
df['ageCat'] = df['ageCat'].astype('int64')
# ...............marital status.........
df['marital-statusCat'] = df['marital-status'].apply(lambda x: 1 if x.startswith('Married', 1) else 0)
# ....................race................
print(pd.crosstab(df['race'], df['salary'], margins=True))
df['Race_cat'] = df['race'].apply(lambda x: x.strip())
df['Race_cat'] = df['Race_cat'].apply(lambda x: 1 if x == 'White' else 0)
# ...............
def workclas(x):
if re.search('Private', x):
return 0
elif re.search('Self', x):
return 1
elif re.search('gov', x):
return 2
else:
return 3
df['WorfClass_cat'] = df.workclass.apply(lambda x: x.strip()).apply(lambda x: workclas(x))
df['WorfClass_cat'] = df.workclass.apply(lambda x: x.strip()).apply(lambda x: workclas(x))
df['WorfClass_cat'].value_counts()
print(tabulate(df.head(15), tablefmt='psql', headers='keys'))
#......................................test data........................................................
data_test = pd.read_csv(r"C:\Users\nikita\Desktop\data_analytics_material\AIT590\python presentation\ML_test_data_without_output.csv")
df_test = pd.DataFrame(data_test)
# print(df)
df_test.replace(' ?', numpy.nan, regex=False, inplace=True)
print(df_test.columns[df_test.isna().any()])
#print(tabulate(df, tablefmt='psql'))
# print(df.mean())
df_test=df_test.fillna(df.mean())
# print(tabulate(df, tablefmt='psql'))
print(df_test['workclass'].describe())
common_value = 'Private'
df_test['workclass'] = df_test['workclass'].fillna(common_value)
# print(tabulate(df, tablefmt='psql'))
print(df_test['occupation'].describe())
common_value = 'Prof-speciality'
df_test['occupation'] = df_test['occupation'].fillna(common_value)
# print(tabulate(df, tablefmt='psql'))
print(df_test['native-country'].describe())
common_value = 'United-States'
df_test['native-country'] = df_test['native-country'].fillna(common_value)
# print(tabulate(df, tablefmt='psql'))
#print(type(df_['salary']))
# numpy.isnan(df)
df_test['sex'].replace([' Male', ' Female'], [1, 0], regex=False, inplace=True)
print(df_test['sex'])
#df.salary.replace([' <=50K', ' >50K'], [1, 0], inplace=True)
# print(tabulate(df, tablefmt='psql'))
# ....data clean: education num........
# data can be train or test
# var name is variable name: should be passed as strings within ('')
# bins is list of numeric values like [0,6,10,11]
# group names is list of groups you want to create in list form
def bin_var(data, var, bins, group_names):
bin_value = bins
group = group_names
data[var + 'Cat'] = pd.cut(df[var], bin_value, labels=group)
bin_var(df_test, 'education-num', [0, 6, 11, 16], [0, 1, 2])
# bin_var(test, 'Education Num', [0,6,11,16], ['Low', 'Medium', 'High'])
#print(pd.crosstab(df['education-numCat'], df['salary']))
df['education-numCat'] = df['education-numCat'].astype('int64')
# ..............data clean:hours/week.......
bin_var(df_test, 'hours-per-week', [0, 35, 40, 60, 100], [0, 1, 2, 3])
# bin_var(test, 'Hours/Week', [0,35,40,60,100], ['Low', 'Medium', 'High','VeryHigh'])
#print(pd.crosstab(df['hours-per-weekCat'], df['salary'], margins=True))
df_test['hours-per-weekCat'] = df_test['hours-per-weekCat'].astype('int64')
# ...........data clean:occupation..........
def occup(x):
if re.search('managerial', x):
return 1
elif re.search('specialty', x):
return 1
else:
return 0
df_test['occupationCat'] = df.occupation.apply(lambda x: x.strip()).apply(lambda x: occup(x))
# df['occupation']=df.occupation.apply(lambda x: x.strip()).apply(lambda x: occup(x))
print(df_test['occupationCat'].value_counts())
# ................age............
bin_var(df_test, 'age', [17, 30, 55, 100], [0, 1, 2])
df_test['ageCat'] = df_test['ageCat'].astype('int64')
# ...............marital status.........
df_test['marital-statusCat'] = df_test['marital-status'].apply(lambda x: 1 if x.startswith('Married', 1) else 0)
# ....................race................
#print(pd.crosstab(df_test['race'], df['salary'], margins=True))
df_test['Race_cat'] = df_test['race'].apply(lambda x: x.strip())
df_test['Race_cat'] = df_test['Race_cat'].apply(lambda x: 1 if x == 'White' else 0)
# ...............
def workclas(x):
if re.search('Private', x):
return 0
elif re.search('Self', x):
return 1
elif re.search('gov', x):
return 2
else:
return 3
df_test['WorfClass_cat'] = df_test.workclass.apply(lambda x: x.strip()).apply(lambda x: workclas(x))
df_test['WorfClass_cat'] = df_test.workclass.apply(lambda x: x.strip()).apply(lambda x: workclas(x))
df_test['WorfClass_cat'].value_counts()
print(tabulate(df.head(15), tablefmt='psql', headers='keys'))
#..........................................................................................................
features = df[['WorfClass_cat','education-numCat', 'ageCat', 'Race_cat',
'hours-per-weekCat',
'marital-statusCat',
'occupationCat',
'sex']]
targetVariables = df.salary
#featureTrain, featureTest, targetTrain, targetTest = train_test_split(features, targetVariables, test_size=0.20)
featureTrain, featureTest, targetTrain, targetTest = train_test_split(features, targetVariables)
#
featureTest=df_test[['WorfClass_cat','education-numCat', 'ageCat', 'Race_cat',
'hours-per-weekCat',
'marital-statusCat',
'occupationCat',
'sex']]
#................................................................................................................
from openpyxl.workbook import Workbook
model= DecisionTreeClassifier(criterion="entropy",max_depth=15)
clf=model.fit(featureTrain, targetTrain)
predictions = clf.predict(featureTest)
prediction=pd.DataFrame(predictions)
print("Printing csv dataframe")
print(tabulate(prediction, tablefmt='psql'))
#prediction.to_csv(open("prediction.csv", "w"), sep=",")
#prediction.to_excel("prediction_new.xlsx")
#prediction = pd.DataFrame(predictions, columns=['predictions']).to_csv('prediction.csv')
#print(predictions).to_csv('predictions.csv')
# print(accuracy_score(targetTest, predictions))
# print(confusion_matrix(targetTest, predictions))
# print(classification_report(targetTest, predictions))