-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
278 lines (237 loc) · 11.2 KB
/
Copy pathmain.py
File metadata and controls
278 lines (237 loc) · 11.2 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
276
277
import pandas as pd
import warnings
from sklearn import preprocessing
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn.ensemble import ExtraTreesClassifier, BaggingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.ensemble import AdaBoostClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
warnings.filterwarnings('ignore')
# *** 1. Loading dataset ***
dataset = pd.read_csv("survey.csv")
print("Shape of dataset:", dataset.shape)
# *** 2. Cleaning our dataset ***
# Deleting some of the features that we don't want to use.
dataset = dataset.drop(["Timestamp"], axis=1)
dataset = dataset.drop(["state"], axis=1)
dataset = dataset.drop(["comments"], axis=1)
dataset = dataset.drop(["Country"], axis=1)
dataset = dataset.drop(["tech_company"], axis=1)
dataset = dataset.drop(["obs_consequence"], axis=1)
# Changing missing (NA) values with TEMP.
dataset = dataset.fillna("TEMP")
'''print(dataset.iloc[0])''' # we checked the first values of each features.
# Changing 'self_employed' values with 'No'.
dataset["self_employed"].replace(["TEMP"], "No", inplace=True)
print("\nUnique values of self_employed after changing NA values:", dataset["self_employed"].unique()) # we checked the unique values of self_employed.
# Changing 'work_interfere' with 'Don't know'
dataset["work_interfere"].replace(["TEMP"], "Don't know", inplace=True)
print("\nUnique values of work_interfere after changing NA values:", dataset["work_interfere"].unique()) # we checked the unique values of work_interfere.
# Changing Age values with median if age < 18 or > 100.
median = int(dataset["Age"].median())
print("\nMedian of age:", median)
for i in range(dataset.shape[0]):
if dataset["Age"][i] < 18 or dataset["Age"][i] > 100:
dataset["Age"][i] = median
# Creating age_range feature.
dataset['age_range'] = pd.cut(dataset['Age'], [0, 20, 30, 65, 100], labels=["0-20", "21-30", "31-65", "66-100"],
include_lowest=True)
# Creating 3 arrays for genders (male, female, trans).
male = ["msle", "mail", "malr",
"cis man", "cis male", "p","male", "m", "male-ish", "maile", "mal", "male (cis)", "make", "male ", "man"]
female = ["female", "woman", "femake", "female ", "cis female", "f", "cis-female/femme", "female (cis)", "femail",
"a little about you"]
trans = ["non-binary", "nah", "all", "enby", "fluid", "genderqueer", "trans-female", "something kinda male?",
"queer/she/they",
"androgyne", "agender", "male leaning androgynous", "guy (-ish) ^_^", "trans woman", "neuter",
"female (trans)", "queer", "ostensibly male, unsure what that really means"]
# We lowered gender values for easier comparison
dataset["Gender"] = dataset["Gender"].str.lower()
# Decreasing gender values to male, female, trans.
for i in range(dataset.shape[0]):
if dataset["Gender"][i] in male:
dataset["Gender"][i] = "male"
elif dataset["Gender"][i] in female:
dataset["Gender"][i] = "female"
elif dataset["Gender"][i] in trans:
dataset["Gender"][i] = "trans"
# *** 3. Encoding dataset ***
# Backup dataset before encoding for graphs.
backupDataset = dataset.copy()
# Encoding dataset with LabelEncoder.
for i in dataset:
label_encoder = preprocessing.LabelEncoder()
label_encoder.fit(dataset[i])
dataset[i] = label_encoder.transform(dataset[i])
# Scaling age.
ageScaler = MinMaxScaler()
dataset['Age'] = ageScaler.fit_transform(dataset[['Age']])
# Checking whether there are any missing values or not.
print("\nChecking the number of null values of each feature after data pre-processing:")
print(dataset.isnull().sum())
# *** 4. Plotting charts for dataset visualization ***
# Creating a chart for distribution by age.
mu = backupDataset["Age"].mean()
sigma = backupDataset["Age"].std()
num_bins = backupDataset["Age"].unique().size
fig, ax = plt.subplots()
n, bins, patches = ax.hist(backupDataset["Age"], num_bins, density=True)
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
np.exp(-0.5 * (1 / sigma * (bins - mu)) ** 2))
ax.plot(bins, y, '--')
ax.set_xlabel('Age')
ax.set_title("Distribution by Age")
fig.tight_layout()
plt.savefig("figures_pdf/Distribution_by_Age.pdf")
# Creating a chart for distribution by gender.
trans = 0
female = 0
male = 0
for i in backupDataset["Gender"]:
if i == "trans":
trans = trans + 1
elif i == "female":
female = female + 1
else:
male = male + 1
fig1, ax1 = plt.subplots()
ax1.pie([male, female, trans], labels=["male", "female", "trans"], autopct='%1.1f%%')
plt.title("Distribution by gender")
plt.savefig("figures_pdf/Distribution_by_gender.pdf")
# Creating a chart for mental health condition by family history and gender.
sns.catplot(x="family_history", y="treatment", hue="Gender", data=dataset, kind="bar").set_xticklabels(["NO", "YES"])
plt.title('Mental Health Condition by Family History And Gender')
plt.xlabel('Family History')
plt.savefig("figures_pdf/Mental_Health_Condition_by_Family_History_And_Gender.pdf")
# Creating a chart for mental health condition by age range.
sns.catplot(x="age_range", y="treatment", hue="Gender", data=dataset, kind="bar", ci=None).set_xticklabels(
["0-20", "21-30", "31-65", "66-100"])
plt.title('Mental Health Condition by Age Range')
plt.xlabel('Age range')
plt.savefig("figures_pdf/Mental_Health_Condition_by_Age_Range.pdf")
# *** 5. Determining feature importance ***
# We made this part because instead of using every feature we wanted to use the most important ones.
# Defining X and y for feature importance.
X_importance = dataset.drop(["treatment"], axis=1)
y_importance = dataset["treatment"]
# ExtraTreesClassifier for computing feature importance.
ETC = ExtraTreesClassifier(n_estimators=300, random_state=0).fit(X_importance, y_importance)
importance = ETC.feature_importances_
std = np.std([tree.feature_importances_ for tree in ETC.estimators_], axis=0)
indices = np.argsort(importance)[::-1]
labels = []
for col in dataset.columns:
if col == "treatment":
continue
labels.append(col)
plt.figure(figsize=(12, 8))
plt.title("Feature importance's")
plt.bar(range(21), importance[indices], color="blue", yerr=std[indices], align="center")
plt.xticks(range(21), labels, rotation='vertical')
plt.xlim([-1, X_importance.shape[1]])
plt.savefig("figures_pdf/Feature_importance's.pdf")
# *** 6. Applying algorithms ***
# Function for plotting confusion matrix.
def confusionMatrix(y_test, prediction, modelName):
plt.figure(figsize=(12, 8))
mat = confusion_matrix(y_test, prediction)
sns.heatmap(mat, square=True, annot=True, cbar=True, fmt="d")
plt.xlabel("Pred")
plt.ylabel("Real Value")
plt.title("Confusion Matrix of " + modelName)
plt.savefig("figures_pdf/Confusion_matrix_of %s.pdf" % modelName)
# Defining X and y.
dataset_features = ["Age", "Gender", "self_employed", "family_history", "work_interfere", "no_employees", "remote_work",
"benefits", "care_options"]
X = dataset[dataset_features]
y = dataset["treatment"] # Ground truth vector.
# Split X and y for training and testing.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=0)
# Logistic Regression part
print("\n********* Logistic Regression *********")
log = LogisticRegression().fit(X_train, y_train)
log_pred = log.predict(X_test)
log_accuracy = accuracy_score(y_test, log_pred)
print("Accuracy score: ", log_accuracy)
print("Real value: ", y_test.values[:30], "\nPred value: ", log_pred[:30])
confusionMatrix(y_test, log_pred, "Logistic Regression")
# AdaBoost Classifier part
print("\n********* AdaBoost Classifier *********")
ABC = AdaBoostClassifier(n_estimators=50).fit(X_train, y_train)
ABC_pred = ABC.predict(X_test)
ABC_accuracy = accuracy_score(y_test, ABC_pred)
print("Accuracy score: ", ABC_accuracy)
print("Real value: ", y_test.values[:30], "\nPred value: ", ABC_pred[:30])
confusionMatrix(y_test, ABC_pred, "AdaBoost Classifier")
# KNeighbors Classifier part
print("\n********* KNeighbors Classifier *********")
# To create the best prediction value, we wanted to determine the best parameters.
n_values = {}
for i in range(1, 53):
KNC = KNeighborsClassifier(n_neighbors=i).fit(X_train, y_train)
KNC_pred = KNC.predict(X_test)
KNC_accuracy = accuracy_score(y_test, KNC_pred)
n_values[i] = KNC_accuracy
best_n_value = max(n_values, key=n_values.get)
KNC = KNeighborsClassifier(n_neighbors=best_n_value).fit(X_train, y_train)
KNC_pred = KNC.predict(X_test)
KNC_accuracy = accuracy_score(y_test, KNC_pred)
print("Accuracy score: ", KNC_accuracy)
print("Real value: ", y_test.values[:30], "\nPred value: ", KNC_pred[:30])
confusionMatrix(y_test, KNC_pred, "KNeighbors Classifier")
# Random Forest Classifier part
print("\n********* Random Forest Classifier *********")
# To create the best prediction value, we wanted to determine the best parameters.
depth_values = {}
for i in range(1, 11):
RFC = RandomForestClassifier(max_depth=i).fit(X_train, y_train)
RFC_pred = RFC.predict(X_test)
RFC_accuracy = accuracy_score(y_test, RFC_pred)
depth_values[i] = RFC_accuracy
best_depth_value = max(depth_values, key=depth_values.get)
RFC = RandomForestClassifier(max_depth=best_depth_value).fit(X_train, y_train)
RFC_pred = RFC.predict(X_test)
RFC_accuracy = accuracy_score(y_test, RFC_pred)
print("Accuracy score: ", RFC_accuracy)
print("Real value: ", y_test.values[:30], "\nPred value: ", RFC_pred[:30])
confusionMatrix(y_test, RFC_pred, "Random Forest Classifier")
# Bagging Classifier part
print("\n********* Bagging Classifier *********")
# To create the best prediction value, we wanted to determine the best parameters.
node_values = {}
for i in [500, 2000, 8000, 99999]:
DTC = DecisionTreeClassifier(max_leaf_nodes=i)
bag = BaggingClassifier(base_estimator=DTC).fit(X_train, y_train)
bag_pred = bag.predict(X_test)
bag_accuracy = accuracy_score(y_test, bag_pred)
node_values[i] = bag_accuracy
best_node_value = max(node_values, key=node_values.get)
DTC = DecisionTreeClassifier(max_leaf_nodes=best_node_value)
bag = BaggingClassifier(base_estimator=DTC).fit(X_train, y_train)
bag_pred = bag.predict(X_test)
bag_accuracy = accuracy_score(y_test, bag_pred)
print("Accuracy score: ", bag_accuracy)
print("Real value: ", y_test.values[:30], "\nPred value: ", bag_pred[:30])
confusionMatrix(y_test, bag_pred, "Bagging Classifier")
# *** 7. Comparison graph of algorithms by accuracy score ***
fig, ax = plt.subplots(figsize=(16, 9))
print(" ")
models = ["Logistic Regression", "AdaBoost Classifier", "KNeighbors Classifier", "Random Forest Classifier"
, "Bagging Classifier"]
plt.ylabel("Accuracy Score")
plt.title("Comparison graph of algorithms by accuracy score")
success = [log_accuracy * 100, ABC_accuracy * 100, KNC_accuracy * 100, RFC_accuracy * 100, bag_accuracy * 100]
ax.bar(models, success)
ax.grid(b=True, color='grey', linestyle='-.', linewidth=0.5, alpha=0.2)
print("\nAccuracy scores of each algorithm:")
for i in range(0, 5):
print(models[i] + ":", success[i])
plt.savefig("figures_pdf/Comparison_graph.pdf")
print("\nAll figures have been saved into the figures_pdf file.")