-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFeature_selection_Impute.py
269 lines (223 loc) · 9.1 KB
/
Feature_selection_Impute.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
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 29 22:59:15 2020
@author: apurva sharma
"""
import pandas as pd
''' Importing the data '''
df = pd.read_csv("survey_results_public.csv")
from tqdm import tqdm
''' Converting some categorical features into Integers '''
def Convert_col_Cat_to_int(df):
first_15_col_excl_yearcode = [ 'MainBranch' ,'Hobbyist', 'OpenSourcer', 'OpenSource', 'Employment', 'Country' ,
'Student' ,'EdLevel', 'UndergradMajor', 'EduOther', 'OrgSize', 'CareerSat']
for i in tqdm(first_15_col_excl_yearcode):
print('Converting '+i +'\n')
print(list(df[i].unique()))
df[i].replace(to_replace = list(df[i].unique()), value = range(1,len(list(df[i].unique()))+1) , inplace = True)
print(df[i].unique())
second_15_col_excl_yearcode = ['WorkPlan', 'WorkChallenge', 'WorkRemote', 'WorkLoc', 'ImpSyn' ,
'CodeRev' ,'UnitTests', 'PurchaseHow', 'PurchaseWhat']
for i in tqdm(second_15_col_excl_yearcode):
print('Converting '+i +'\n')
print(list(df[i].unique()))
df[i].replace(to_replace = list(df[i].unique()), value = range(1,len(list(df[i].unique()))+1) , inplace = True)
print(df[i].unique())
third_15_col_excl_yearcode = ['OpSys' ,'BlockchainOrg', 'BlockchainIs', 'ITperson']
for i in tqdm(third_15_col_excl_yearcode):
print('Converting '+i +'\n')
print(list(df[i].unique()))
df[i].replace(to_replace = list(df[i].unique()), value = range(1,len(list(df[i].unique()))+1) , inplace = True)
print(df[i].unique())
fourth_15_col_excl_yearcode = ['OffOn','SocialMedia', 'SOFindAnswer', 'SOVisitFreq','SOTimeSaved' ,
'SOHowMuchTime' ,'SOAccount', 'SOPartFreq', 'SOJobs', 'WelcomeChange', 'SONewContent', 'Gender','Trans','Sexuality','Dependents']
for i in tqdm(fourth_15_col_excl_yearcode):
print('Converting '+i)
print(list(df[i].unique()))
df[i].replace(to_replace = list(df[i].unique()), value = range(1,len(list(df[i].unique()))+1) , inplace = True)
print(df[i].unique())
last_15_col_excl_yearcode = ['JobSat','MgrIdiot','MgrMoney','MgrWant','JobSeek','LastHireDate','JobFactors','CurrencySymbol']
for i in tqdm(last_15_col_excl_yearcode):
print( 'Converting '+ i )
print(list(df[i].unique()))
df[i].replace(to_replace = list(df[i].unique()), value = range(1,len(list(df[i].unique()))+1) , inplace = True)
print(df[i].unique())
return df
print("Converting Categorical data to integers , mapping the categories \n\n")
Convert_col_Cat_to_int(df)
'''Dropped non - useful columns '''
def Dropping_Non_useful_data(df):
df = df.drop(axis=1 ,columns=["Respondent", "Age1stCode", "FizzBuzz", "ResumeUpdate", "BetterLife", "ScreenName", "SOVisit1st",
"EntTeams","SOComm","Extraversion","SurveyLength","SurveyEase","LastInt",'CurrencyDesc',
"Ethnicity", "DevEnviron", "LanguageDesireNextYear","DatabaseDesireNextYear","PlatformDesireNextYear",
"WebFrameDesireNextYear","MiscTechDesireNextYear","SOVisitTo", "WelcomeChange",
"CurrencySymbol","CompTotal","CompFreq"])
return df
Dropping_Non_useful_data(df)
def Convert_YearsCodePro_and_YearsCode ( df ):
df["YearsCode"].replace(to_replace = ['Less than 1 year', 'More than 50 years'],
value = [1,50] , inplace = True)
df["YearsCode"].fillna(value= '-1', inplace = True )
df["YearsCode"] = df["YearsCode"].apply(pd.to_numeric)
df["YearsCodePro"].replace(to_replace = ['Less than 1 year', 'More than 50 years'],
value = [1,50] , inplace = True)
df["YearsCodePro"].fillna(value= '-1', inplace = True )
df["YearsCodePro"] = df["YearsCodePro"].apply(pd.to_numeric)
return df
Convert_YearsCodePro_and_YearsCode(df)
''' Multiclass Categorical feature'''
DevType = pd.DataFrame(rep_devtype)
df["DevType"] = DevType
LanguageWorkedWith = df["LanguageWorkedWith"]
LanguageWorkedWith.fillna(value= '-1', inplace = True )
ls = []
for i in LanguageWorkedWith :
if i != '-1':
cat = i.split(';')
for j in cat:
if j not in ls:
ls.append(j)
d = dict(zip(set(ls), range(len(ls))))
rep_LanguageWorkedWith = []
for i in LanguageWorkedWith :
if i == '-1':
rep_LanguageWorkedWith.append(-1)
if i != '-1':
sum = 0
cat = i.split(';')
for j in cat:
sum = d[ j ] + sum
rep_LanguageWorkedWith.append(sum)
LanguageWorkedWith = pd.DataFrame(rep_LanguageWorkedWith)
df["LanguageWorkedWith"] = LanguageWorkedWith
DatabaseWorkedWith = df["DatabaseWorkedWith"]
DatabaseWorkedWith.fillna(value= '-1', inplace = True )
ls = []
for i in DatabaseWorkedWith :
if i != '-1':
cat = i.split(';')
for j in cat:
if j not in ls:
ls.append(j)
d = dict(zip(set(ls), range(len(ls))))
rep_DatabaseWorkedWith = []
for i in DatabaseWorkedWith :
if i == '-1':
rep_DatabaseWorkedWith.append(-1)
if i != '-1':
sum = 0
cat = i.split(';')
for j in cat:
sum = d[ j ] + sum
rep_DatabaseWorkedWith.append(sum)
DatabaseWorkedWith = pd.DataFrame(rep_DatabaseWorkedWith)
df["DatabaseWorkedWith"] = DatabaseWorkedWith
PlatformWorkedWith = df["PlatformWorkedWith"]
PlatformWorkedWith.fillna(value= '-1', inplace = True )
ls = []
for i in PlatformWorkedWith :
if i != '-1':
cat = i.split(';')
for j in cat:
if j not in ls:
ls.append(j)
d = dict(zip(set(ls), range(len(ls))))
rep_PlatformWorkedWith = []
for i in PlatformWorkedWith :
if i == '-1':
rep_PlatformWorkedWith.append(-1)
if i != '-1':
sum = 0
cat = i.split(';')
for j in cat:
sum = d[ j ] + sum
rep_PlatformWorkedWith.append(sum)
PlatformWorkedWith = pd.DataFrame(rep_PlatformWorkedWith)
df["PlatformWorkedWith"] = PlatformWorkedWith
WebFrameWorkedWith = df["WebFrameWorkedWith"]
WebFrameWorkedWith.fillna(value= '-1', inplace = True )
ls = []
for i in WebFrameWorkedWith :
if i != '-1':
cat = i.split(';')
for j in cat:
if j not in ls:
ls.append(j)
d = dict(zip(set(ls), range(len(ls))))
rep_WebFrameWorkedWith = []
for i in WebFrameWorkedWith :
if i == '-1':
rep_WebFrameWorkedWith.append(-1)
if i != '-1':
sum = 0
cat = i.split(';')
for j in cat:
sum = d[ j ] + sum
rep_WebFrameWorkedWith.append(sum)
WebFrameWorkedWith = pd.DataFrame(rep_WebFrameWorkedWith)
df["WebFrameWorkedWith"] = DatabaseWorkedWith
MiscTechWorkedWith = df["MiscTechWorkedWith"]
MiscTechWorkedWith.fillna(value= '-1', inplace = True )
ls = []
for i in MiscTechWorkedWith :
if i != '-1':
cat = i.split(';')
for j in cat:
if j not in ls:
ls.append(j)
d = dict(zip(set(ls), range(len(ls))))
rep_MiscTechWorkedWith = []
for i in MiscTechWorkedWith :
if i == '-1':
rep_MiscTechWorkedWith.append(-1)
if i != '-1':
sum = 0
cat = i.split(';')
for j in cat:
sum = d[ j ] + sum
rep_MiscTechWorkedWith.append(sum)
MiscTechWorkedWith = pd.DataFrame(rep_MiscTechWorkedWith)
df["MiscTechWorkedWith"] = MiscTechWorkedWith
Containers = df["Containers"]
Containers.fillna(value= '-1', inplace = True )
ls = []
for i in Containers :
if i != '-1':
cat = i.split(';')
for j in cat:
if j not in ls:
ls.append(j)
d = dict(zip(set(ls), range(len(ls))))
rep_Containers = []
for i in Containers :
if i == '-1':
rep_Containers.append(-1)
if i != '-1':
sum = 0
cat = i.split(';')
for j in cat:
sum = d[ j ] + sum
rep_Containers.append(sum)
Containers = pd.DataFrame(rep_Containers)
df["Containers"] = Containers
''' Imputting data'''
df['WorkWeekHrs'] = df['WorkWeekHrs'].fillna((df['WorkWeekHrs'].mean()))
df['CodeRevHrs'] = df['CodeRevHrs'].fillna((df['CodeRevHrs'].mean()))
df['Age'] = df['Age'].fillna((df['Age'].median()))
ls = []
for i in range(len(df["MainBranch"])):
if df["MainBranch"][i] == 1:
ls.append(i)
len(ls)
for i in range(len(df["ConvertedComp"])):
if i in ls:
df["ConvertedComp"][i] = 0
df["ConvertedComp"] = df["ConvertedComp"].fillna(value = df["ConvertedComp"].mean())
'''Normalizing the data'''
from sklearn import preprocessing
# Create a minimum and maximum processor object
min_max_scaler = preprocessing.MinMaxScaler( feature_range= (0,1))
# Create an object to transform the data to fit minmax processor
x_scaled = min_max_scaler.fit_transform(df.drop(axis = 1, columns= "ConvertedComp"))3
# Run the normalizer on the dataframe
df_normalized = pd.DataFrame(x_scaled)