-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
387 lines (278 loc) · 14.2 KB
/
model.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import numpy as np
import pandas as pd
from matplotlib import pyplot
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.utils import resample
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
import pickle
import seaborn as sns
from sklearn.tree import DecisionTreeRegressor
def gender():
pd.options.display.max_columns = None
people = pd.read_csv('people.csv', low_memory=False)
name_gender = pd.read_csv('firstNameGender.csv', index_col=0)
relationships = pd.read_csv('relationships.csv', low_memory=False)
people["first_name"] = people["first_name"].str.replace("Dr.", "")
people["first_name"] = people["first_name"].str.split("-").str[0]
people["first_name"] = people["first_name"].str.split(" ").str[0]
# Merge Gender, People and Relationsships
people_gender = pd.merge(people, name_gender, on="first_name", how="left")
people_gender.columns = ["id", "person_object_id", 'first_name', 'last_name', "birthplace", "affiliation_name",
"Gender"]
result = pd.merge(people_gender, relationships, on="person_object_id", how="left")
result = result.drop(
columns=["person_object_id", "id_x", "id_y", "relationship_id", "start_at", "end_at", "is_past", "sequence",
"title", "created_at", "updated_at", 'first_name', 'last_name', "birthplace", "affiliation_name"])
result.columns = ["gender", "object_id"]
# Groupby Company ID
result = result.groupby("object_id")["gender"].value_counts().unstack().fillna(0)
# Calculation of female/male ratio
result['sum'] = result['female'] + result['male']
result['female'] = result['female'] / result['sum']
result['male'] = result['male'] / result['sum']
result = result.round({"female": 2, "male": 2})
final_result = result.drop(columns=["sum"])
return final_result
def prepare_data():
companies_y = pd.read_csv('companies_y.csv', low_memory=False)
companies_y = companies_y.drop('Unnamed: 0', 1)
companies_y['funded_or_acquired'] = companies_y['funded_or_acquired'].astype(int)
companies_races = pd.read_csv('racecompany.csv', low_memory=False)
company_founding = pd.read_csv('company_founding.csv', low_memory=False)
company_domain = pd.read_csv('company_domain.csv', low_memory=False)
company_domain = company_domain.drop('Unnamed: 0', 1)
company_domain = company_domain.drop('domain_name', 1)
company_domain = company_domain.drop('domain', 1)
company_domain = company_domain.drop('domain_ending', 1)
company_domain.rename({'id': 'company_id'}, axis='columns', inplace=True)
company_domain = company_domain.set_index("company_id")
companies_y = companies_y.set_index("company_id")
offices_cities_dummy = pd.read_csv('offices_cities_dummy.csv', low_memory=False)
offices_cities_dummy.rename({'object_id': 'company_id'}, axis='columns', inplace=True)
offices_cities_dummy = offices_cities_dummy.set_index("company_id")
offices_cities_dummy.fillna(0)
offices_cities_dummy.replace(np.nan, 0)
offices_cities_dummy.replace('nan', 0)
offices_cities_dummy = offices_cities_dummy.groupby('company_id').sum()
offices_countries_dummy = pd.read_csv('offices_countries_dummy.csv', low_memory=False)
offices_countries_dummy.rename({'object_id': 'company_id'}, axis='columns', inplace=True)
offices_countries_dummy = offices_countries_dummy.set_index("company_id")
offices_countries_dummy.fillna(0)
offices_countries_dummy.replace(np.nan, 0)
offices_countries_dummy.replace('nan', 0)
offices_countries_dummy = offices_countries_dummy.groupby('company_id').sum()
train = companies_y.join(company_domain, on="company_id", how='left')
train = train.join(offices_cities_dummy, on="company_id", how='left')
train = train.drop('Unnamed: 0', 1)
#train = train.drop('city', 1)
train = train.join(offices_countries_dummy, on="company_id", rsuffix = "_country", how='left')
#train = train.drop('Unnamed: 0', 1)
#train = train.drop('country_code', 1)
train=train.fillna(0)
#train.replace(np.nan, 0)
gender_per_company = gender()
train = train.join(gender_per_company, on="company_id", how='left')
train = train.fillna(0.5)
companies_races.rename({'object_id': 'company_id'}, axis='columns', inplace=True)
companies_races = companies_races.set_index("company_id")
train = train.join(companies_races, on="company_id", how='left')
train=train.fillna(0)
train = train.drop('Unnamed: 0', 1)
domain_endings = pd.read_csv('domain_ending_dummy.csv', low_memory=False)
domain_endings.rename({'id': 'company_id'}, axis='columns', inplace=True)
domain_endings = domain_endings.set_index("company_id")
train = train.join(domain_endings, on="company_id", how='left')
train=train.fillna(0)
train = train.drop('Unnamed: 0', 1)
train = train.drop('male', 1)
train = train.drop('Others', 1)
train = train.drop('Others_country', 1)
return train
def model_dt(data):
X = data.loc[:, data.columns != 'funded_or_acquired']
y = data.loc[:, 'funded_or_acquired']
X_train, X_validation, Y_train, Y_validation = train_test_split(X, y, test_size=0.3, random_state=1, shuffle=True)
model = DecisionTreeClassifier()
model.fit(X_train, Y_train)
predictions = model.predict(X_validation)
# Evaluate predictions
print("accuracy_score: "+ str(accuracy_score(Y_validation, predictions)))
print("confusion_matrix:")
print(confusion_matrix(Y_validation, predictions))
print(classification_report(Y_validation, predictions))
# Saving the Model
pickle_out = open("decision_tree_model.pkl", "wb")
pickle.dump(model, pickle_out)
pickle_out.close()
importance = model.feature_importances_
# summarize feature importance
#for i, v in enumerate(importance):
#print('Feature: %0d, Score: %.5f' % (i, v))
# plot feature importance
pyplot.rcParams["figure.figsize"] = (15, 10)
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
feat_importances = pd.Series(model.feature_importances_, index=X_train.columns)
ax = feat_importances.nlargest(10).plot(kind='barh')
ax.set(ylabel="feature (top 10)", xlabel="feature importance (decision tree classifier)")
ax.set_xlabel("feature importance (decision tree classifier)", fontsize=20)
ax.set_ylabel("feature (top 10)", fontsize=20)
ax.tick_params(labelsize=10)
pyplot.savefig("decision_tree_feature_importances.png")
pyplot.show()
print("checking on training data:")
model2 = DecisionTreeClassifier()
model2.fit(X_train, Y_train)
predictions2 = model2.predict(X_train)
print("accuracy_score (training): "+ str(accuracy_score(Y_train, predictions2)))
print("confusion matrix")
print(confusion_matrix(Y_train, predictions2))
return accuracy_score(Y_validation, predictions)
def model_gauss(data):
X = data.loc[:, data.columns != 'funded_or_acquired']
y = data.loc[:, 'funded_or_acquired']
X_train, X_validation, Y_train, Y_validation = train_test_split(X, y, test_size=0.3, random_state=1, shuffle=True)
model = GaussianNB()
model.fit(X_train, Y_train)
predictions = model.predict(X_validation)
# Evaluate predictions
print("accuracy_score: "+ str(accuracy_score(Y_validation, predictions)))
print("confusion_matrix:")
print(confusion_matrix(Y_validation, predictions))
print(classification_report(Y_validation, predictions))
print("checking on training data:")
model2 = GaussianNB()
model2.fit(X_train, Y_train)
predictions2 = model2.predict(X_train)
print("accuracy_score (training): "+ str(accuracy_score(Y_train, predictions2)))
print("confusion matrix")
print(confusion_matrix(Y_train, predictions2))
return accuracy_score(Y_validation, predictions)
def model_linear(data):
X = data.loc[:, data.columns != 'funded_or_acquired']
y = data.loc[:, 'funded_or_acquired']
X_train, X_validation, Y_train, Y_validation = train_test_split(X, y, test_size=0.3, random_state=1, shuffle=True)
model = LogisticRegression(solver='liblinear', multi_class='ovr')
model.fit(X_train, Y_train)
predictions = model.predict(X_validation)
# Evaluate predictions
print("accuracy_score: "+ str(accuracy_score(Y_validation, predictions)))
print("confusion_matrix:")
print(confusion_matrix(Y_validation, predictions))
print(classification_report(Y_validation, predictions))
print("checking on training data:")
model2 = LogisticRegression(solver='liblinear', multi_class='ovr')
model2.fit(X_train, Y_train)
predictions2 = model2.predict(X_train)
print("accuracy_score (training): "+ str(accuracy_score(Y_train, predictions2)))
print("confusion matrix")
print(confusion_matrix(Y_train, predictions2))
return accuracy_score(Y_validation, predictions)
def model_SVC(data):
X = data.loc[:, data.columns != 'funded_or_acquired']
y = data.loc[:, 'funded_or_acquired']
X_train, X_validation, Y_train, Y_validation = train_test_split(X, y, test_size=0.3, random_state=1, shuffle=True)
model = SVC(gamma='auto')
model.fit(X_train, Y_train)
predictions = model.predict(X_validation)
# Evaluate predictions
print("accuracy_score: "+ str(accuracy_score(Y_validation, predictions)))
print("confusion_matrix:")
print(confusion_matrix(Y_validation, predictions))
print(classification_report(Y_validation, predictions))
print("checking on training data:")
predictions2 = model.predict(X_train)
print("accuracy_score (training): "+ str(accuracy_score(Y_train, predictions2)))
print("confusion matrix")
print(confusion_matrix(Y_train, predictions2))
return accuracy_score(Y_validation, predictions)
def upsample_data(data):
#from: https://elitedatascience.com/imbalanced-classes
print("------")
print("trying to balance the dataset using upsampling = duplicate funded startups until here are as many companies as non funded ones")
print("summary of dataset: "+str(data.funded_or_acquired.value_counts()))
# Separate majority and minority classes
df_majority = data[data.funded_or_acquired == 0]
df_minority = data[data.funded_or_acquired == 1]
print("summary of majority: "+str(df_majority.funded_or_acquired.value_counts()))
print("summary of minority: "+str(df_minority.funded_or_acquired.value_counts()))
# Upsample minority class
df_minority_upsampled = resample(df_minority,
replace=True, # sample with replacement
n_samples=158045, # to match majority class
random_state=123) # reproducible results
# Combine majority class with upsampled minority class
df_upsampled = pd.concat([df_majority, df_minority_upsampled])
# Display new class counts
print("after upsampling:")
print(df_upsampled.funded_or_acquired.value_counts())
return df_upsampled
def downsample_data(data):
#from: https://elitedatascience.com/imbalanced-classes
#print("------")
#print("trying to balance the dataset using downsampling = delete un-funded startups until here are as many companies as non funded ones")
#print("summary of dataset: "+str(data.funded_or_acquired.value_counts()))
# Separate majority and minority classes
df_majority = data[data.funded_or_acquired == 0]
df_minority = data[data.funded_or_acquired == 1]
#print("summary of majority: "+str(df_majority.funded_or_acquired.value_counts()))
#print("summary of minority: "+str(df_minority.funded_or_acquired.value_counts()))
# Upsample minority class
df_majority_downsampled = resample(df_majority,
replace=False, # sample with replacement
n_samples=38508, # to match majority class
random_state=123) # reproducible results
# Combine majority class with upsampled minority class
df_downsampled = pd.concat([df_majority_downsampled, df_minority])
# Display new class counts
#print("after downsampling:")
#print(df_downsampled.funded_or_acquired.value_counts())
return df_downsampled
def model_dt_regressor(data):
#from: https://machinelearningmastery.com/machine-learning-in-python-step-by-step/
X = data.loc[:, data.columns != 'funded_or_acquired']
y = data.loc[:, 'funded_or_acquired']
X_train, X_validation, Y_train, Y_validation = train_test_split(X, y, test_size=0.3, random_state=1, shuffle=True)
model = DecisionTreeRegressor(random_state=0)
model.fit(X_train, Y_train)
predictions = model.predict(X_validation)
# Saving the Model
pickle_out = open("decision_tree_regressor_model.pkl", "wb")
pickle.dump(model, pickle_out)
pickle_out.close()
return predictions
data = prepare_data()
data.rename(columns={'domain_name_length': 'domain_length'}, inplace=True)
data_downsampled = downsample_data(data)
def all_models():
print("---")
print("Decision Tree:")
acc_dt = model_dt(data_downsampled)
print("---")
print("GaussianNB:")
acc_gauss = model_gauss(data_downsampled)
print("---")
print("LogisticRegression:")
acc_linear = model_linear(data_downsampled)
print("---")
print("SVC:")
acc_SVC = model_SVC(data_downsampled)
d = {'algorithms': ["Decision Tree", "GaussianNB", "LogisticRegression", "SVC"], 'accuracy': [acc_dt, acc_gauss, acc_linear, acc_SVC]}
accuracies = pd.DataFrame(data=d)
ax = sns.barplot(x=accuracies['algorithms'], y=accuracies['accuracy'], color='tab:blue')
ax.set_xlabel("algorithms", fontsize=20)
ax.set_ylabel("accuracy", fontsize=20)
ax.tick_params(labelsize=15)
pyplot.axhline(y=0.5, color='k', linestyle='--')
#accuracies.plot.bar()
pyplot.show()
#data.to_csv('data.csv')
#all_models()
regression_dt_predictions = model_dt_regressor(data_downsampled)