-
Notifications
You must be signed in to change notification settings - Fork 0
/
IncomePrediction.py
225 lines (152 loc) · 5.1 KB
/
IncomePrediction.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 1 17:34:56 2019
@author: ezi
"""
import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
import matplotlib.pyplot as plt
import pandas
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from keras.models import Model
from keras.layers import LSTM, Activation, Dense, Dropout, Input, Embedding
from keras.optimizers import RMSprop
from keras.preprocessing.text import Tokenizer
from keras.preprocessing import sequence
from keras.utils import to_categorical
from keras.callbacks import EarlyStopping
%matplotlib inline
# ------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------
# Loading dataset
data = pd.read_csv('tcd ml 2019-20 income prediction training (with labels).csv')
data
# ------------------------------------------------------------------------------------------------------
# Dataset analysis
data[data.isnull().any(axis=1)].head()
import numpy as np
np.sum(data.isnull().any(axis=1))
# Filling the values of NaN with 0
data=data.dropna(axis = 0, how ='any')
import numpy as np
np.sum(data.isnull().any(axis=1))
data.isnull().any(axis=0)
data.info()
data.describe()
# Looking at the all columns
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import pandas
neg = data
neg_string = []
for t in neg:
neg_string.append(t)
neg_string = pandas.Series(neg_string).str.cat(sep=' ')
wordcloud = WordCloud(width=1600, height=800,max_font_size=200).generate(neg_string)
plt.figure(figsize=(12,10))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
data.head(2)
# Encoding Gender, Country, Profession, University Degree and Hair Color
from sklearn import preprocessing
ge= preprocessing.LabelEncoder()
encge=ge.fit_transform(data['Gender'])
data['Gender'] = encge
co= preprocessing.LabelEncoder()
encco=co.fit_transform(data['Country'])
data['Country'] = encco
pr= preprocessing.LabelEncoder()
encpr=pr.fit_transform(data['Profession'])
data['Profession'] = encpr
uni= preprocessing.LabelEncoder()
encuni=uni.fit_transform(data['University Degree'])
data['University Degree'] = encuni
ha= preprocessing.LabelEncoder()
encha=ha.fit_transform(data['Hair Color'])
data['Hair Color'] = encha
# Heatmap for looking at the values of all columns
plt.figure(figsize = (15,15))
sns.heatmap(data = data.corr(), annot=True, linewidths=.3, cmap='RdBu')
plt.show()
data.describe()
# Features Distribution graphs
data.hist(figsize=(15,12),bins = 20, color="#107009AA")
plt.title("Features Distribution")
plt.show()
# By getting features and Class
y=data['Income in EUR']
X=data.drop(columns=['Income in EUR','Instance'])
# LinearRegression
#from sklearn import linear_model
#LR=linear_model.LinearRegression()
#LR= LR.fit(X , y)
#LR
# Fitting Decision Tree Regression to the dataset
#from sklearn.tree import DecisionTreeRegressor
#LR = DecisionTreeRegressor(random_state = 0)
#LR=LR.fit(X, y)
#LR
# Fitting SupportVectorRegression to the dataset
#from sklearn.svm import SVR
#LR = SVR(kernel = 'rbf')
#LR=LR.fit(X, y)
#LR
# Fitting Kernel SVM to the Training set
#from sklearn.svm import SVC
#classifier = SVC(kernel = 'rbf', random_state = 0)
#classifier.fit(X_train, y_train)
from sklearn.ensemble import RandomForestRegressor
LR = RandomForestRegressor(n_estimators = 10, random_state = 0)
LR= LR.fit(X, y)
LR
# getting the data for predictions
import pandas as pd
DataS=pd.read_csv("tcd ml 2019-20 income prediction test (without labels).csv")
DataS=DataS.drop(columns=['Instance','Income'])
DataS=DataS.dropna(axis = 0, how ='any')
DataS
from sklearn import preprocessing
ge= preprocessing.LabelEncoder()
encge=ge.fit_transform(DataS['Gender'])
DataS['Gender'] = encge
co= preprocessing.LabelEncoder()
encco=co.fit_transform(DataS['Country'])
DataS['Country'] = encco
pr= preprocessing.LabelEncoder()
encpr=pr.fit_transform(DataS['Profession'])
DataS['Profession'] = encpr
uni= preprocessing.LabelEncoder()
encuni=uni.fit_transform(DataS['University Degree'])
DataS['University Degree'] = encuni
ha= preprocessing.LabelEncoder()
encha=ha.fit_transform(DataS['Hair Color'])
DataS['Hair Color'] = encha
# Getting Predictions with Trained model
#pred=classifier.predict(DataS)
pred=LR.predict(DataS)
# Predictions
pred
# Saving Predictions in pandas
submission = pd.DataFrame()
submission["Action"]=pred
import pandas as pd
DataS=pd.read_csv("tcd ml 2019-20 income prediction test (without labels).csv")
DataS['Income']=submission["Action"]
DataS
# Saving with labels
DataS.to_csv('Predictions_tcd ml 2019-20 income prediction test (without labels)')
submission = pd.DataFrame()
submission["Income"]=pred
submission.index += 1
submission["Instance"] = submission.index
submission=submission[['Instance','Income']]
# Saving the Submission File
filename = 'tcd ml 2019-20 income prediction submission file.csv'
submission.to_csv(filename,index=False)
print('Saved file: ' + filename)