-
Notifications
You must be signed in to change notification settings - Fork 0
/
ML_Sample_Dan_Amare.py
344 lines (277 loc) · 12.5 KB
/
ML_Sample_Dan_Amare.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
from datetime import datetime
from logging import getLogger
import csv
import os
import numpy as np
import pandas as pd
import re as re
import subprocess as sb
import sys
import cx_Oracle
import platform
import shutil
from datetime import date
import time
import dateutil
from dateutil import parser
import openpyxl
from sklearn.preprocessing import LabelBinarizer, MultiLabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
import matplotlib.pyplot as plt
from xgboost import XGBClassifier
import tensorflow as tf
#This is machine learning sample script to predict risk levels based on relevant features.
#THIS HAS NOT BEEN IMPLMENTED, only used for demo purposes.
#v1.1 Random Forest, Visualize feature importance
#v1.2 Change features to ordinal categories, with priority levels
#v1.3 Try with XGBOOST --> Better
#v1.4 Try with Neural Networks Regression --> Not good
#v1.5 NN Classifier -->Not better
logger = getLogger('return')
def print_log(LOG_FILE, print_txt, target):
if target == 1:
print(print_txt)
with open(LOG_FILE, 'a') as f:
f.write(print_txt)
f.write('\n')
def train_model(data_file_data, rf_priority):
# Create mapper
scale_mapper = {"Low":0,
"Medium":1,
"High":2, "Very High":3}
# Replace feature values with scale
#dataframe["Score"].replace(scale_mapper)
data_file_data['RiskLevel'] = data_file_data['RiskLevel'].replace(scale_mapper)
rf_priority['PRIORITY'] = rf_priority['PRIORITY'].replace(scale_mapper)
rf_priority = rf_priority.drop([0,1,2],axis=0)
#print(rf_priority.head(7))
#print(rf_priority.iloc[7,0])
#exit(0)
#replace boolean with Y/N
#replace_txt = {'Yes':'Y','No':'N'}
#data_file_data = data_file_data.replace(replace_txt,regex=False)
#Replace None Yes/No with NA
replace_txt = {'Yes': 1,
'No': -0.1,
'Not Available':0,
'Not Applicable':0,
'N/A': 0}
data_file_data = data_file_data.replace(replace_txt,regex=False)
#print(data_file_data.head())
df_awarded = data_file_data[data_file_data['StatusCd']=='AWARDED']
df_preaward = data_file_data[data_file_data['StatusCd']!='AWARDED']
# Drop the 'Id' column as it's just an identifier
df_awarded=df_awarded.drop(['GrntId','StatusCd'], axis=1)
df_preaward=df_preaward.drop(['GrntId','StatusCd'], axis=1)
#df_awarded.drop(['GrntId','StatusCd'], axis=1, inplace=True)
#df_preaward.drop(['GrntId','StatusCd'], axis=1, inplace=True)
"""
# One-hot encode the categorical features
encoder = OneHotEncoder(sparse_output=False, handle_unknown='ignore')
encoded_features_a = encoder.fit_transform(df_awarded.drop('RiskLevel', axis=1))
encoded_features_p = encoder.fit_transform(df_preaward.drop('RiskLevel', axis=1))
# Create a new DataFrame with the encoded features
feature_names = encoder.get_feature_names_out(input_features=df_awarded.columns[1:])
df_encoded_a = pd.DataFrame(encoded_features_a, columns=feature_names)
df_encoded_p = pd.DataFrame(encoded_features_p, columns=feature_names)
# Add the target column back to the encoded DataFrame
df_encoded_a['RiskLevel'] = df_awarded['RiskLevel'].values
df_encoded_p['RiskLevel'] = df_preaward['RiskLevel'].values
#print(df_encoded_a.head())
#exit(0)
# Split the dataset into training and testing sets
X_train = df_encoded_a.drop('RiskLevel', axis=1)
y_train = df_encoded_a['RiskLevel']
X_test = df_encoded_p.drop('RiskLevel', axis=1)
y_test = df_encoded_p['RiskLevel']
"""
X_train = df_awarded.drop('RiskLevel', axis=1)
y_train = df_awarded['RiskLevel']
X_test = df_preaward.drop('RiskLevel', axis=1)
y_test = df_preaward['RiskLevel']
row_index = 0
for columns in X_train.columns:
#print(columns)
X_train[columns]=X_train[columns]*rf_priority.iloc[row_index,0]
X_test[columns]=X_test[columns]*rf_priority.iloc[row_index,0]
row_index += 1
"""
# Neural network model for regression
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(128, activation='relu', input_shape=(X_train.shape[1],)),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(1) # Single output neuron without activation for regression
])
model.compile(optimizer='adam',
loss='mean_squared_error', # MSE is commonly used for regression tasks
metrics=['mean_absolute_error']) # MAE provides an intuitive error metric
"""
# Neural network model Classifier
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(128, activation='relu', input_shape=(X_train.shape[1],)),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(y_train.nunique(), activation='softmax') # Assuming 'Risk' is a categorical variable
#tf.keras.layers.Dense([1,2,3,4], activation='softmax') # Assuming 'Risk' is a categorical variable
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train the model
history = model.fit(X_train, y_train, epochs=100, batch_size=32, validation_split=0.2, verbose=1)
# Evaluate the model
val_loss, val_acc = model.evaluate(X_test, y_test, verbose=2)
print(f"Test accuracy: {val_acc}")
# Predict and calculate accuracy (optional)
# Convert predictions to labels
y_pred = model.predict(X_test)
y_pred_labels = np.argmax(y_pred, axis=1)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred_labels)
print(f'Neural Network accuracy: {accuracy}')
#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize and fit the Random Forest classifier
#Best estimators using GridSearch
#'max_depth': None, 'min_samples_leaf': 1, 'min_samples_split': 2, 'n_estimators': 200}
#rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)
#rf_classifier = RandomForestClassifier(max_depth=None, min_samples_leaf=1, min_samples_split=2, n_estimators=200, random_state=42)
#xgboost
""""
rf_classifier = XGBClassifier(use_label_encoder=False,eval_metric='mlogloss')
rf_classifier.fit(X_train, y_train)
# Make predictions and calculate the accuracy
y_pred = rf_classifier.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Model accuracy on the test set:", accuracy)
# Assuming best_rf is your best estimator from GridSearchCV
# Get feature importances
best_rf = rf_classifier
feature_importances = best_rf.feature_importances_
# Sort the feature importances in descending order and get the indices
sorted_indices = np.argsort(feature_importances)[::-1]
# Number of top features to select, you can adjust this number
top_n = 20
# Select the top n feature names and their importance scores
#top_feature_names = df_encoded_a.columns[sorted_indices][:top_n]
top_feature_names = df_awarded.columns[sorted_indices][:top_n]
top_feature_importances = feature_importances[sorted_indices][:top_n]
# Create the bar chart
plt.figure(figsize=(10, 6))
plt.barh(range(top_n), top_feature_importances[::-1], align='center')
plt.yticks(range(top_n), top_feature_names[::-1])
plt.xlabel('Feature Importance')
plt.ylabel('Feature')
plt.title('Top {} Most Important Features'.format(top_n))
plt.show()
# Create features and target
features = data_file_data.iloc[:,2:]
print(features.head())
# Create one-hot encoder
one_hot = LabelBinarizer()
#one_hot = MultiLabelBinarizer()
# One-hot encode feature
one_hot.fit_transform(features[:,0])
print(one_hot.classes_)
target = data_file_data.iloc[:,1]
print(target.head())
"""
return
def main( v_data_file):
#def main():
#examp
DEMO_MAX = 2000
num_rows = 0
VERSION_CODE = 1.0
#VERSION_CONFIG = 20
WORKING_DIRECTORY = os.getcwd()
#print('Imported Index directory is '+ INDEX_DIRECTORY)
#print('Imported CACHE directory is '+ IRSX_CACHE_DIRECTORY)
#print('Imported XML directory is '+ WORKING_DIRECTORY)
today = datetime.now().strftime("%m_%d_%Y_%H_%M")
data_file_curr = v_data_file
data_file_ext = os.path.splitext(data_file_curr)[-1].lower()
data_file_log = 'ML_log_'+today+'.txt'
#CONFIG_FILE = os.path.join(WORKING_DIRECTORY, 'Config','Data_file_structs.xls')
CONFIG_FILE = os.path.join(WORKING_DIRECTORY, 'Config','ML_data_struct.xls')
DATA_FILE = os.path.join(WORKING_DIRECTORY, 'Data',data_file_curr)
LOG_FILE = os.path.join(WORKING_DIRECTORY, 'Logs',data_file_log)
print_txt='Data load script version: %s\n'%(VERSION_CODE)
#print_txt=print_txt + 'Config file version: %s'%(VERSION_CONFIG)
print_log(LOG_FILE, print_txt,1)
if not os.path.exists(DATA_FILE):
print_txt='Error: Input file was not found: %s '%(DATA_FILE)
print_log(LOG_FILE, print_txt,1)
sys.exit(1)
else:
print_txt = "Input file: %s" %(DATA_FILE)
print_log(LOG_FILE, print_txt,1)
#data_name_curr = v_data_name
try:
data_file_headers = pd.read_excel(CONFIG_FILE, names=['FIELDS','PRIORITY'],sheet_name = 'ML',header=None, index_col=False)
except Exception as err:
print_txt = 'Error reading config file. Expected column missing or not in expected location.\n'
print_txt = print_txt+'%s'%(err)
print_log(LOG_FILE, print_txt,1)
sys.exit(1)
data_file_fields = data_file_headers['FIELDS'].T.tolist()
#print(data_file_fields)
#return
rf_priority = data_file_headers[['PRIORITY']]
#print(data_file_cols)
#return
#print (file_headers.loc['COLS'].tolist())
#usecols=lambda c: c in set(data_file_index) for changing index to column names
#USAS file no longer being used, API version implemented in API script
data_file_sheet = {'REVIEW': 'New and Recompete',
'MONREF': 'NC_OCRO',
'FIELDPRINT': 0
}
data_file_cols ={'IPERA':'A:Z',
'REVIEW': 'C,N,DN'}
#If TrueScreen, skip 4 report title rows
#skip_num_rows = 4 if data_name_curr == 'TRUESCREEN' else 0
try:
data_file_data = pd.read_excel(DATA_FILE,
names=data_file_fields,
sheet_name = 0,
header=0,
#skiprows= skip_num_rows,
usecols='G,L,AH,AM:DR',
dtype=str,
keep_default_na=False,
engine='openpyxl',index_col=False)
except Exception as err:
print_txt = 'Error reading file. Expected column missing or not in expected location.\n'
print_txt = print_txt+'%s'%(err)
print_log(LOG_FILE, print_txt,1)
sys.exit(1)
#data_file_data.to_csv(LOG_FILE, index=False)
#print (data_file_data.columns)
#return
#Remove null records
data_file_data.columns = data_file_fields
data_file_data = data_file_data.replace(r'^\s*$', np.nan, regex=True)
data_file_data = data_file_data.dropna(axis=0, how='all')
#print(data_file_data.columns)
#print(data_file_data.head())
train_model(data_file_data, rf_priority)
#load_data(v_db_env,data_name_curr, data_file_fields, data_file_data, LOG_FILE)
if __name__ == '__main__':
if len(sys.argv) != 2:
#raise ValueError('FAILED -- Invalid job execution format.')
print('Error: Invalid job execution format.\n'+
'Please using following format to execute the model.\n'+
'ML_Pilot.py [Data file name].\n'+
'Example: ML_Pilot.py '+"'"+'Applications Pivot Report.xlsx'+"'")
sys.exit(1)
v_data_file = sys.argv[1]
#v_credentials = sys.argv[2]
main(v_data_file)