-
Notifications
You must be signed in to change notification settings - Fork 55
/
bactclass.py
830 lines (743 loc) · 46.3 KB
/
bactclass.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
"""!
BactClass: Machine Learning Classifier for Bactome
Date created: 25th July 2020
License: GNU General Public License version 3 for academic or
not-for-profit use only
Reference: Liu, TT, Ling, MHT. 2020. BactClass: Simplifying the Use of
Machine Learning in Biology and Medicine. Acta Scientific Medical
Sciences 4(11): 43-47.
Bactome package is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import pickle
import pandas as pd
from sklearn.model_selection import train_test_split
try:
import fire
except ImportError:
subprocess.check_call([sys.executable, '-m', 'pip',
'install', 'fire'])
import fire
try:
import joblib
except ImportError:
subprocess.check_call([sys.executable, '-m', 'pip',
'install', 'joblib'])
import joblib
def readData(filename, training=True, label="Class"):
"""!
Internal function - Read the data file into a Pandas dataframe.
@param filename String: Path to CSV data file.
@param training Boolean: Flag to indicate whether the file consists of data for training. Default = True
@param label String: Column (field) name in the data file to indicate the class label. Default = Class
"""
data = pd.read_csv(filename)
print("Number of rows in data = " + str(data.shape[0]))
print("Number of columns in data = " + str(data.shape[1]))
if training:
X = data.drop(label, axis=1)
Y = data[label]
return X, Y
else:
return data
def saveModel(filename, filetype, classifier):
"""!
Internal function - Write out the classifier object into a pickle file.
@param filename String: Path to write out the generated classifier.
@param filetype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib".
@param classifier Object: Classifier object
"""
if filetype.lower() == "pickle":
f = open(filename, "wb")
pickle.dump(classifier, f, pickle.HIGHEST_PROTOCOL)
f.close()
elif filetype.lower() == "joblib":
joblib.dump(classifier, filename)
def loadModel(filename, filetype):
"""!
Internal function - Load the classifier from file.
@param filename String: Path to the generated classifier to be loaded.
@param filetype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib".
"""
if filetype.lower() == "pickle":
f = open(filename, "rb")
classifier = pickle.load(f)
f.close()
return classifier
elif filetype.lower() == "joblib":
classifier = joblib.load(filename)
return classifier
def showClassifierParameters(classifier):
"""!
Internal function - Print out parameters of the classifier.
@param classifier Object: Classifier object
"""
param = classifier.get_params()
print("------------- Classifier Parameters -----------------")
for k in param:
print("%s = %s" % (k, param[k]))
print("------------- End of Classifier Parameters ----------")
print("")
def showConfusionMatrix(Y_test, Y_pred):
"""!
Internal function - Print out confusion matrix.
@param Y_test Array: Actual classification from training data in NumPy Array.
@param Y_pred Array: Predicted classification from classifier in NumPy Array.
"""
from sklearn.metrics import confusion_matrix
matrix = confusion_matrix(Y_test, Y_pred)
print("------------- Confusion Matrix ----------------------")
for row in matrix:
print(', '.join([str(x) for x in row]))
print("------------- End of Confusion Matrix ---------------")
print("")
def showClassificationReport(Y_test, Y_pred):
"""!
Internal function - Print out classification report (precision, recall, F1-score, and Accuracy).
@param Y_test Array: Actual classification from training data in NumPy Array.
@param Y_pred Array: Predicted classification from classifier in NumPy Array.
"""
from sklearn.metrics import classification_report
print("------------- Classification Report -----------------")
print(classification_report(Y_test, Y_pred))
print("------------- End of Classification Report ----------")
print("")
def cross_validate(classifier, X, Y, fold):
"""!
Internal function - Cross validation of classifier
@param classifier Object: Generated classifier object.
@param X Array: Array of independent variables.
@param Y Array: Array of dependent variable.
@param fold Integer: Number of folds for cross validation
"""
from sklearn import metrics
from sklearn.model_selection import cross_val_score
fold = int(fold)
print("------------ Cross Validation Report ----------------")
print("%s fold cross validation" % str(fold))
print("")
try:
scores = cross_val_score(classifier, X, Y, cv=fold, scoring="precision")
print("Precision: %0.3f (sigma = %0.4f)" % (scores.mean(), scores.std()))
except ValueError:
pass
try:
scores = cross_val_score(classifier, X, Y, cv=fold, scoring="recall")
print("Recall: %0.3f (sigma = %0.4f)" % (scores.mean(), scores.std()))
except ValueError:
pass
try:
scores = cross_val_score(classifier, X, Y, cv=fold, scoring="f1")
print("F1 Score: %0.3f (sigma = %0.4f)" % (scores.mean(), scores.std()))
except ValueError:
scores = cross_val_score(classifier, X, Y, cv=fold, scoring="f1_weighted")
print("Weighted F1 Score: %0.3f (sigma = %0.4f)" % (scores.mean(), scores.std()))
try:
scores = cross_val_score(classifier, X, Y, cv=fold, scoring="accuracy")
print("Accuracy: %0.3f (sigma = %0.4f)" % (scores.mean(), scores.std()))
except ValueError:
pass
try:
scores = cross_val_score(classifier, X, Y, cv=fold, scoring="roc_auc")
print("Area Under ROC Curve (AUC): %0.3f (sigma = %0.4f)" % (scores.mean(), scores.std()))
except ValueError:
pass
try:
scores = cross_val_score(classifier, X, Y, cv=fold, scoring="jaccard")
print("Jaccard Similarity: %0.3f (sigma = %0.4f)" % (scores.mean(), scores.std()))
except ValueError:
pass
try:
scores = cross_val_score(classifier, X, Y, cv=fold, scoring="normalized_mutual_info_score")
print("Normalized Mutual Information: %0.3f (sigma = %0.4f)" % (scores.mean(), scores.std()))
except ValueError:
pass
try:
scores = cross_val_score(classifier, X, Y, cv=fold, scoring="v_measure_score")
print("V-measure: %0.3f (sigma = %0.4f)" % (scores.mean(), scores.std()))
except ValueError:
pass
try:
scores = cross_val_score(classifier, X, Y, cv=fold, scoring="neg_mean_squared_error")
print("Mean Square Error (MSE): %0.3f (sigma = %0.4f)" % (abs(scores.mean()), scores.std()))
except ValueError:
pass
try:
scores = cross_val_score(classifier, X, Y, cv=fold, scoring="r2")
print("R-square: %0.3f (sigma = %0.4f)" % (abs(scores.mean()), scores.std()))
except ValueError:
pass
print("--------- End of Cross Validation Report ------------")
def process_classifier(datafile, label, classifier, oclass, otype,
classparam, confusion, classreport, cross_validation):
"""!
Internal function - Process (train, test, and save) classifier.
@param datafile String: Path to CSV data file used to generate SVM.
@param label String: Column (field) name in the data file to indicate the class label.
@param oclass String: Path to write out the generated classifier.
@param otype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib".
@param classifier Object: Generated classifier object.
@param classparam Boolean: Flag to indicate whether to print out SVM parameters.
@param confusion Boolean: Flag to indicate whether to print out confusion matrix.
@param classreport Boolean: Flag to indicate whether to print out classification report.
@param cross_validation Integer: Number of cross validation (if any) to perform. If less than 2, cross validation will not be carried out.
"""
X, Y = readData(datafile, True, label)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.20)
classifier.fit(X_train, Y_train)
Y_pred = classifier.predict(X_test)
saveModel(oclass, otype, classifier)
if classparam:
showClassifierParameters(classifier)
if confusion:
showConfusionMatrix(Y_test, Y_pred)
if classreport:
showClassificationReport(Y_test, Y_pred)
if cross_validation > 1:
cross_validate(classifier, X, Y, int(cross_validation))
def useScikitClassifier(classifier_type, datafile, classfile, classtype, resultfile):
"""!
Internal function - Use a previously generated SciKit-Learn classifier
to classify data.
@param classifier_type String: Type of classifier.
@param datafile String: Path to CSV file containing data to be classified.
@param classfile String: Path to the generated classifier.
@param classtype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib".
@param resultfile String: Path to write out the classified results.
"""
taskText = {"ANN": "Classifying using Artificial Neural Network (ANN)",
"BNB":"Classifying using Naive Bayes for Bernoulli Models (BNB)",
"CNB":"Classifying using Complement Naive Bayes (CNB)",
"DT": "Classifying using Decision Tree (DT)",
"GNB":"Classifying using Gaussian Naive Bayes (GNB)",
"MNB":"Classifying using Naive Bayes for Multinomial Models (MNB)",
"SVM": "Classifying using Support Vector Machine (SVM)",
}
print("")
print("Task: %s" % taskText[classifier_type])
print("Parameters:")
print(" Data File = " + str(datafile))
print(" Classifier File = " + str(classfile))
print(" Classifier File Type = " + str(classtype))
print(" Result File = " + str(resultfile))
print("")
classifier = loadModel(classfile, classtype)
data = readData(datafile, False)
data["Prediction"] = classifier.predict(data)
data.to_csv(resultfile, index=False)
def recycle(infile, intype, outfile, outtype):
"""!
Function to read in and write out a classifier. This can be used to update the serialization protocol or to change the change the type of serialization; such as, from pickle to joblib.
Usage:
python bactclass.py recycle --infile=classifier_ANN.pickle --intype=pickle --outfile=classifier_ANN.joblib --outtype=joblib
@param infile String: Path to the input classifier.
@param intype String: Type of file of the input classifier. Allowable types are "pickle" and "joblib".
@param outfile String: Path to the output classifier.
@param outtype String: Type of file of the output classifier. Allowable types are "pickle" and "joblib".
"""
print("")
print("Task: Read and Write (Recycle) a Classifier")
print("Parameters:")
print(" Input Classifier File = " + str(infile))
print(" Input Classifier File Type = " + str(intype))
print(" Output Classifier File = " + str(outfile))
print(" Output Classifier File Type = " + str(outtype))
print("")
classifier = loadModel(infile, intype)
saveModel(outfile, outtype, classifier)
def generateANN(datafile, label,
oclass="classifier_ANN.pickle",
otype="pickle",
hidden_layer_sizes="100",
activation="relu",
solver="adam",
learning_rate="constant",
learning_rate_init=0.001,
power_t=0.5,
max_iteration=200,
shuffle=True,
tolerance=0.001,
momentum=0.9,
nesterovs_momentum=True,
beta_1=0.9,
beta_2=0.999,
epsilon=1e-8,
n_iter_no_change=10,
verbose=False,
classparam=True,
confusion=True,
classreport=True,
cross_validation=5):
"""!
Function to generate an artificial neural network (multi-layer perceptron classifier) from given data.
Usage:
python bactclass.py genANN --datafile=classifier_train.csv --label=Class --oclass=classifier_ANN.pickle --otype=pickle --hidden_layer_sizes=100 --activation=relu --solver=adam --learning_rate=constant --learning_rate_init=0.001 --power_t=0.5 --max_iteration=200 --shuffle=True --tolerance=0.001 --momentum=0.9 --nesterovs_momentum=True --beta_1=0.9 --beta_2=0.999 --epsilon=1e-8 --n_iter_no_change=10 --verbose=False --classparam=True --confusion=True --classreport=True --cross_validation=5
@param datafile String: Path to CSV data file used to generate ANN.
@param label String: Column (field) name in the data file to indicate the class label.
@param oclass String: Path to write out the generated classifier. Default = classifier_ANN.pickle
@param otype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib". Default = pickle
@param hidden_layer_sizes String: Defines the number of hidden layers and the number of nodes per hidden layer in semicolon-delimited format. For example, "100" represents 1 hidden layer of 100 nodes; whereas, "100;30" represents 2 hidden layers where the first hidden layer contains 100 nodes while the second hidden layer contains 30 nodes. Default = 100
@param activation String: Activation function of the hidden layer. Allowable types are "identity", f(x) = x; "logistic" (logistic sigmoid function), f(x) = 1 / (1 + exp(-x)); "tanh" (hyperbolic tangeant function), f(x) = tanh(x); and "relu" (rectified linear unit function),f(x) = max(0, x). Default = relu
@param solver String: Solver for weight optimization. Allowable types are "lbfgs" (is an )optimizer in the family of quasi-Newton methods), "sgd" (stochastic gradient descent), and "adam" (stochastic gradient-based optimizer proposed by Kingma and Ba, 2015. 3rd International Conference on Learning Representations). Default = adam
@param learning_rate String: Learning rate schedule for weight updates, which is used only when solver is "sgd". Allowable types are "constant" (constant learning rate given by "learning_rate_init"), "invscaling" (gradually decreases the learning rate at each time step "t" using an inverse scaling exponent of "power_t" where ffective_learning_rate = learning_rate_init / pow(t, power_t)), and "adaptive" (keeps the learning rate constant to "learning_rate_init" as long as training loss keeps decreasing). Default = constant
@param learning_rate_init Float: Initial learning rate used to control the step-size in updating the weights. This is only used when solver is "sgd or "adam". Default = 0.001
@param power_t Float: Exponent for inverse scaling learning rate, which is used when solver is "sgd" for updating effective learning rate when the learning_rate is set to "invscaling". Default = 0.5
@param max_iteration Integer: Maximum number of iterations where the solver iterates until convergence (determined by "tolerance") or reaching maximum iterations. For stochastic solvers ("sgd" or "adam"), note that this determines the number of epochs (how many times each data point will be used) rather than the number of gradient steps.
@param shuffle Boolean: Determines whether to shuffle samples in each iteration. Only used when solver is "sgd" or "adam". Default = True
@param tolerance Float: Tolerance for the optimization. When the loss or score is not improving by at least tolerance for n_iter_no_change consecutive iterations, unless learning_rate is set to "adaptive", convergence is considered to be reached and training stops. Default = 0.001
@param momentum Float: Momentum, between 0 and 1, for gradient descent update (solver is "sgd"). Default = 0.9
@param nesterovs_momentum Boolean: Flag to whether to use Nesterov's momentum when solver is "sgd" and momentum > 0. Default = True
@param beta_1 float: Exponential decay rate for estimates of first moment vector, between 0 (inclusive) and 1 (exclusive), in "adam" solver. Default = 0.9
@param beta_2 float: Exponential decay rate for estimates of second moment vector, between 0 (inclusive) and 1 (exclusive), in "adam" solver. Default = 0.999
@param epsilon float: Value for numerical stability in "adam". Default = 1e-8
@param n_iter_no_change Integer: Maximum number of epochs to not meet tolerance improvement when solver is "sgd" or "adam". Default = 10
@param verbose Boolean: Flag to indicate whether to print progress messages. Default = False
@param classparam Boolean: Flag to indicate whether to print out ANN parameters. Default = True
@param confusion Boolean: Flag to indicate whether to print out confusion matrix. Default = True
@param classreport Boolean: Flag to indicate whether to print out classification report. Default = True
@param cross_validation Integer: Number of cross validation (if any) to perform. If less than 2, cross validation will not be carried out. Default = 5
"""
from sklearn.neural_network import MLPClassifier
print("")
print("Task: Generate Artificial Neural Network (ANN) Multi-Layer Perceptron Classifier")
print("Parameters:")
print(" Data File = " + str(datafile))
print(" Classification Label = " + str(label))
print(" Hidden Layer Sizes = " + str(hidden_layer_sizes))
print(" Activation Function = " + str(activation))
print(" Solver Function = " + str(solver))
print(" Type of Learning Rate = " + str(learning_rate))
print(" Initial Learning Rate (learning_rate_init) = " + str(learning_rate_init))
print(" Inverse Scaling Learning Rate Exponent (power_t) = " + str(power_t))
print(" Momentum = " + str(momentum))
print(" Nesterov's Momentum = " + str(nesterovs_momentum))
print(" Exponential decay rate of first moment vector (beta_1) = " + str(beta_1))
print(" Exponential decay rate of second moment vector (beta_2) = " + str(beta_2))
print(" Numerical Stability (epsilon) = " + str(beta_2))
print(" Maximum Iteration = " + str(int(max_iteration)))
print(" Maximum Iteration with No Change (n_iter_no_change) = " + str(n_iter_no_change))
print(" Tolerance = " + str(tolerance))
print(" Shuffle = " + str(shuffle))
print(" Verbose = " + str(verbose))
print(" Classifier File = " + str(oclass))
print(" Classifier File Type = " + str(otype))
print("")
hidden_layer_sizes = str(hidden_layer_sizes)
hidden_layer_sizes = hidden_layer_sizes.split(";")
hidden_layer_sizes = [int(x.strip()) for x in hidden_layer_sizes]
classifier = MLPClassifier(hidden_layer_sizes=tuple(hidden_layer_sizes),
activation=str(activation),
solver=str(solver),
learning_rate=str(learning_rate),
learning_rate_init=float(learning_rate_init),
power_t=float(power_t),
random_state=1,
max_iter=int(max_iteration),
shuffle=shuffle,
tol=float(tolerance),
verbose=verbose,
momentum=float(momentum),
nesterovs_momentum=nesterovs_momentum,
beta_1=float(beta_1),
beta_2=float(beta_2),
epsilon=float(epsilon),
n_iter_no_change=int(n_iter_no_change))
process_classifier(datafile, label, classifier, oclass, otype,
classparam, confusion, classreport, cross_validation)
print("===================== ANN Generated =====================")
def useANN(datafile, classfile, classtype, resultfile):
"""!
Function to use a previously generated artificial neural network (ANN)
to classify data.
Usage:
python bactclass.py useANN --datafile=classifier_use.csv --classfile=classifier_ANN.pickle --classtype=pickle --resultfile=classifier_result.csv
@param datafile String: Path to CSV file containing data to be classified.
@param classfile String: Path to the generated classifier.
@param classtype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib".
@param resultfile String: Path to write out the classified results.
"""
useScikitClassifier("ANN", datafile, classfile, classtype, resultfile)
def generateSVM(datafile, label,
oclass="classifier_SVM.pickle",
otype="pickle",
kernel="linear",
degree=3,
gamma="scale",
coef0=0.0,
decision_function_shape="ovr",
tolerance=0.001,
max_iteration=-1,
break_ties=False,
classparam=True,
confusion=True,
classreport=True,
cross_validation=5):
"""!
Function to generate a support vector machine from given data.
Usage:
python bactclass.py genSVM --datafile=classifier_train.csv --label=Class --oclass=classifier_SVM.pickle --otype=pickle --kernel=linear --degree=3 --gamma=scale --coef0=0.0 --decision_function_shape=ovr --tolerance=0.001 --max_iteration=-1 --break_ties=False --classparam=True --confusion=True --classreport=True --cross_validation=5
@param datafile String: Path to CSV data file used to generate SVM.
@param label String: Column (field) name in the data file to indicate the class label.
@param oclass String: Path to write out the generated classifier. Default = classifier_SVM.pickle
@param otype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib". Default = pickle
@param kernel String: Type of SVM kernel. Acceptable values are linear, poly, rbf, sigmoid. Default = linear
@param degree Integer: Polynomial degree, only used in polynomial (poly) kernel. Default = 3
@param gamma String: Coefficient for "rbf", "poly" and "sigmoid" kernels. Allowable types are "scale" and "float". Default = scale
@param coef0 float: Independent term used in "poly" and "sigmoid" kernels. Default = 0.0
@param decision_function_shape String: Uses one-vs-rest (ovr) decision function of shape or one-vs-one ("ovo") decision function. However, one-vs-one ("ovo") is always used as multi-class strategy. The parameter is ignored for binary classification. Default = ovr
@param tolerance float: Tolerance for stopping criterion. Default = 0.001
@param max_iteration Integer: Hard limit on iterations within solver, or -1 for no limit. Default = -1
@param break_ties Boolean: If True, decision_function_shape is "ovr, and number of classes > 2, prediction function will break ties according to the confidence values of decision_function; otherwise the first class among the tied classes is returned. Default = False
@param classparam Boolean: Flag to indicate whether to print out SVM parameters. Default = True
@param confusion Boolean: Flag to indicate whether to print out confusion matrix. Default = True
@param classreport Boolean: Flag to indicate whether to print out classification report. Default = True
@param cross_validation Integer: Number of cross validation (if any) to perform. If less than 2, cross validation will not be carried out. Default = 5
"""
from sklearn.svm import SVC
print("")
print("Task: Generate Support Vector Machine (SVM) Classifier")
print("Parameters:")
print(" Data File = " + str(datafile))
print(" Classification Label = " + str(label))
print(" SVM Kernel Type = " + str(kernel))
print(" Polynomial Degree = " + str(int(degree)))
print(" Kernel Coefficient (gamma) = " + str(gamma))
print(" Independent Coefficient (coef0) = " + str(coef0))
print(" Decision Function Shape = " + str(decision_function_shape))
print(" Tolerance = " + str(tolerance))
print(" Maximum Iteration = " + str(int(max_iteration)))
print(" Breaking Prediction ties (break_ties) = " + str(break_ties))
print(" Classifier File = " + str(oclass))
print(" Classifier File Type = " + str(otype))
print("")
classifier = SVC(kernel=str(kernel),
degree=int(degree),
gamma=str(gamma),
coef0=float(coef0),
decision_function_shape=str(decision_function_shape),
tol=float(tolerance),
max_iter=int(max_iteration),
break_ties=break_ties)
process_classifier(datafile, label, classifier, oclass, otype,
classparam, confusion, classreport, cross_validation)
print("===================== SVM Generated =====================")
def useSVM(datafile, classfile, classtype, resultfile):
"""!
Function to use a previously generated support vector machine (SVM)
to classify data.
Usage:
python bactclass.py useSVM --datafile=classifier_use.csv --classfile=classifier_SVM.pickle --classtype=pickle --resultfile=classifier_result.csv
@param datafile String: Path to CSV file containing data to be classified.
@param classfile String: Path to the generated classifier.
@param classtype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib".
@param resultfile String: Path to write out the classified results.
"""
useScikitClassifier("SVM", datafile, classfile, classtype, resultfile)
def generateDT(datafile, label,
oclass="classifier_DT.pickle",
otype="pickle",
criterion="gini",
splitter="best",
max_depth=0,
min_samples_split=2,
min_samples_leaf=1,
min_weight_fraction_leaf=0.0,
max_leaf_nodes=0,
ccp_alpha=0.0,
classparam=True,
confusion=True,
classreport=True,
cross_validation=5):
"""!
Function to generate a decision tree from given data.
Usage:
python bactclass.py genDT --datafile=classifier_train.csv --label=Class --oclass=classifier_DT.pickle --otype=pickle --criterion=gini --splitter=best --max_depth=0 --min_samples_split=2 --min_samples_leaf=1 --min_weight_fraction_leaf=0.0 --max_leaf_nodes=0 --ccp_alpha=0.0 --classparam=True --confusion=True --classreport=True --cross_validation=5
@param datafile String: Path to CSV data file used to generate DT.
@param label String: Column (field) name in the data file to indicate the class label.
@param oclass String: Path to write out the generated classifier. Default = classifier_DT.pickle
@param otype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib". Default = pickle
@param criterion String: The function to measure the quality of a split. Allowable types are "gini" (Gini impurity) and "entropy" (information gain). Default = gini
@param splitter String: The strategy used to choose the split at each node. Allowable types are "best" (best split) and "random" (best random split). Default = best
@param max_depth Integer: The maximum depth of the tree. If less than 1, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. Default = 0
@param min_samples_split Integer: The minimum number of samples required to split an internal node. Default = 2
@param min_samples_leaf Integer: The minimum number of samples required to be at a leaf node. A split point at any depth will only be considered if it leaves at least min_samples_leaf training samples in each of the left and right branches. This may have the effect of smoothing the model, especially in regression. Default = 1
@param min_weight_fraction_leaf Float: The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Default = 0.0
@param max_leaf_nodes Integer: Grow a tree with max_leaf_nodes in best-first fashion. Best nodes are defined as relative reduction in impurity. If less than 1, then unlimited number of leaf nodes. Default = 0
@param ccp_alpha Float: Complexity parameter used for Minimal Cost-Complexity Pruning. The subtree with the largest cost complexity that is smaller than ccp_alpha will be chosen. If zero, no pruning is performed. Default = 0.0
@param classparam Boolean: Flag to indicate whether to print out SVM parameters. Default = True
@param confusion Boolean: Flag to indicate whether to print out confusion matrix. Default = True
@param classreport Boolean: Flag to indicate whether to print out classification report. Default = True
@param cross_validation Integer: Number of cross validation (if any) to perform. If less than 2, cross validation will not be carried out. Default = 5
"""
from sklearn.tree import DecisionTreeClassifier
print("")
print("Task: Generate Decision Tree (DT) Classifier")
print("Parameters:")
print(" Data File = " + str(datafile))
print(" Classification Label = " + str(label))
print(" Citerion = " + str(criterion))
print(" Splitting Strategy = " + str(splitter))
print(" Maximum Tree Depth = " + str(max_depth))
print(" Minimum Samples to Split = " + str(min_samples_split))
print(" Minimum Samples per Leaf = " + str(min_samples_leaf))
print(" Minimum Weight Fraction per Leaf = " + str(min_weight_fraction_leaf))
print(" Maximum number of Leaf Nodes = " + str(max_leaf_nodes))
print(" Minimal Cost-Complexity Pruning Parameter = " + str(ccp_alpha))
print(" Classifier File = " + str(oclass))
print(" Classifier File Type = " + str(otype))
print("")
max_depth = int(max_depth)
if max_depth == 0: max_depth = None
max_leaf_nodes = int(max_leaf_nodes)
if max_leaf_nodes == 0: max_leaf_nodes = None
classifier = DecisionTreeClassifier(criterion=str(criterion),
splitter=str(splitter),
max_depth=max_depth,
min_samples_split=int(min_samples_split),
min_samples_leaf=int(min_samples_leaf),
min_weight_fraction_leaf=float(min_weight_fraction_leaf),
max_leaf_nodes=max_leaf_nodes,
ccp_alpha=float(ccp_alpha))
process_classifier(datafile, label, classifier, oclass, otype,
classparam, confusion, classreport, cross_validation)
print("===================== DT Generated =====================")
def useDT(datafile, classfile, classtype, resultfile):
"""!
Function to use a previously generated decision tree (DT) to classify data.
Usage:
python bactclass.py useDT --datafile=classifier_use.csv --classfile=classifier_DT.pickle --classtype=pickle --resultfile=classifier_result.csv
@param datafile String: Path to CSV file containing data to be classified.
@param classfile String: Path to the generated classifier.
@param classtype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib".
@param resultfile String: Path to write out the classified results.
"""
useScikitClassifier("DT", datafile, classfile, classtype, resultfile)
def generateBNB(datafile, label,
oclass="classifier_BNB.pickle",
otype="pickle",
alpha=1.0,
binarize=0.0,
fit_prior=True,
classparam=True,
confusion=True,
classreport=True,
cross_validation=5):
"""!
Function to generate a Naive Bayes classifier for Bernoulli Models (BNB) from given data.
Usage:
python bactclass.py genBNB --datafile=classifier_train.csv --label=Class --oclass=classifier_BNB.pickle --otype=pickle --alpha=1.0 --binarize=0.0 --fit_prior=True --classparam=True --confusion=True --classreport=True --cross_validation=5
@param datafile String: Path to CSV data file used to generate BNB.
@param label String: Column (field) name in the data file to indicate the class label.
@param oclass String: Path to write out the generated classifier. Default = classifier_BNB.pickle
@param otype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib". Default = pickle
@param alpha Float: Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing). Default = 1.0
@param binarize Float: Threshold for binarizing (mapping to booleans) of sample features. Default=0.0
@param fit_prior Boolean: Flag to indicate whether to learn class prior probabilities or not. If False, a uniform prior will be used. Default = True
@param classparam Boolean: Flag to indicate whether to print out BNB parameters. Default = True
@param confusion Boolean: Flag to indicate whether to print out confusion matrix. Default = True
@param classreport Boolean: Flag to indicate whether to print out classification report. Default = True
@param cross_validation Integer: Number of cross validation (if any) to perform. If less than 2, cross validation will not be carried out. Default = 5
"""
from sklearn.naive_bayes import BernoulliNB
print("")
print("Task: Generate Naive Bayes Classifier for Bernoulli Models (BNB)")
print("Parameters:")
print(" Data File = " + str(datafile))
print(" Classification Label = " + str(label))
print(" Additive Smoothing Parameter = " + str(alpha))
print(" Binarizing Threshold = " + str(binarize))
print(" Learn Prior Probabilities = " + str(fit_prior))
print(" Classifier File = " + str(oclass))
print(" Classifier File Type = " + str(otype))
print("")
classifier = BernoulliNB(alpha=float(alpha),
binarize=float(binarize),
fit_prior=fit_prior)
process_classifier(datafile, label, classifier, oclass, otype,
classparam, confusion, classreport, cross_validation)
print("================ Bernoulli NB Generated ================")
def useBNB(datafile, classfile, classtype, resultfile):
"""!
Function to use a previously generated Bernoulli Naive Bayes (BNB) classifier to classify data.
Usage:
python bactclass.py useBNB --datafile=classifier_use.csv --classfile=classifier_BNB.pickle --classtype=pickle --resultfile=classifier_result.csv
@param datafile String: Path to CSV file containing data to be classified.
@param classfile String: Path to the generated classifier.
@param classtype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib".
@param resultfile String: Path to write out the classified results.
"""
useScikitClassifier("BNB", datafile, classfile, classtype, resultfile)
def generateCNB(datafile, label,
oclass="classifier_CNB.pickle",
otype="pickle",
alpha=1.0,
fit_prior=True,
classparam=True,
confusion=True,
classreport=True,
cross_validation=5):
"""!
Function to generate a Complement Naive Bayes classifier (CNB) from given data.
Usage:
python bactclass.py genCNB --datafile=classifier_train.csv --label=Class --oclass=classifier_CNB.pickle --otype=pickle --alpha=1.0 --fit_prior=True --classparam=True --confusion=True --classreport=True --cross_validation=5
@param datafile String: Path to CSV data file used to generate CNB.
@param label String: Column (field) name in the data file to indicate the class label.
@param oclass String: Path to write out the generated classifier. Default = classifier_CNB.pickle
@param otype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib". Default = pickle
@param alpha Float: Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing). Default = 1.0
@param fit_prior Boolean: Flag to indicate whether to learn class prior probabilities or not. If False, a uniform prior will be used. Default = True
@param classparam Boolean: Flag to indicate whether to print out CNB parameters. Default = True
@param confusion Boolean: Flag to indicate whether to print out confusion matrix. Default = True
@param classreport Boolean: Flag to indicate whether to print out classification report. Default = True
@param cross_validation Integer: Number of cross validation (if any) to perform. If less than 2, cross validation will not be carried out. Default = 5
"""
from sklearn.naive_bayes import ComplementNB
print("")
print("Task: Generate Complement Naive Bayes (CNB) Classifier")
print("Parameters:")
print(" Data File = " + str(datafile))
print(" Classification Label = " + str(label))
print(" Additive Smoothing Parameter = " + str(alpha))
print(" Learn Prior Probabilities = " + str(fit_prior))
print(" Classifier File = " + str(oclass))
print(" Classifier File Type = " + str(otype))
print("")
classifier = ComplementNB(alpha=float(alpha),
fit_prior=fit_prior)
process_classifier(datafile, label, classifier, oclass, otype,
classparam, confusion, classreport, cross_validation)
print("================ Complement NB Generated ================")
def useCNB(datafile, classfile, classtype, resultfile):
"""!
Function to use a previously generated Complement Naive Bayes (CNB) classifier to classify data.
Usage:
python bactclass.py useCNB --datafile=classifier_use.csv --classfile=classifier_CNB.pickle --classtype=pickle --resultfile=classifier_result.csv
@param datafile String: Path to CSV file containing data to be classified.
@param classfile String: Path to the generated classifier.
@param classtype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib".
@param resultfile String: Path to write out the classified results.
"""
useScikitClassifier("CNB", datafile, classfile, classtype, resultfile)
def generateMNB(datafile, label,
oclass="classifier_MNB.pickle",
otype="pickle",
alpha=1.0,
fit_prior=True,
classparam=True,
confusion=True,
classreport=True,
cross_validation=5):
"""!
Function to generate a Naive Bayes classifier for Multinomial Models (MNB) from given data.
Usage:
python bactclass.py genMNB --datafile=classifier_train.csv --label=Class --oclass=classifier_BNB.pickle --otype=pickle --alpha=1.0 --fit_prior=True --classparam=True --confusion=True --classreport=True --cross_validation=5
@param datafile String: Path to CSV data file used to generate BNB.
@param label String: Column (field) name in the data file to indicate the class label.
@param oclass String: Path to write out the generated classifier. Default = classifier_BNB.pickle
@param otype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib". Default = pickle
@param alpha Float: Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing). Default = 1.0
@param fit_prior Boolean: Flag to indicate whether to learn class prior probabilities or not. If False, a uniform prior will be used. Default = True
@param classparam Boolean: Flag to indicate whether to print out MNB parameters. Default = True
@param confusion Boolean: Flag to indicate whether to print out confusion matrix. Default = True
@param classreport Boolean: Flag to indicate whether to print out classification report. Default = True
@param cross_validation Integer: Number of cross validation (if any) to perform. If less than 2, cross validation will not be carried out. Default = 5
"""
from sklearn.naive_bayes import MultinomialNB
print("")
print("Task: Generate Naive Bayes Classifier for Multinomial Models (MNB)")
print("Parameters:")
print(" Data File = " + str(datafile))
print(" Classification Label = " + str(label))
print(" Additive Smoothing Parameter = " + str(alpha))
print(" Learn Prior Probabilities = " + str(fit_prior))
print(" Classifier File = " + str(oclass))
print(" Classifier File Type = " + str(otype))
print("")
classifier = MultinomialNB(alpha=float(alpha),
fit_prior=fit_prior)
process_classifier(datafile, label, classifier, oclass, otype,
classparam, confusion, classreport, cross_validation)
print("=============== Multinomial NB Generated ===============")
def useMNB(datafile, classfile, classtype, resultfile):
"""!
Function to use a previously generated Multinomial Naive Bayes (MNB) classifier to classify data.
Usage:
python bactclass.py useMNB --datafile=classifier_use.csv --classfile=classifier_MNB.pickle --classtype=pickle --resultfile=classifier_result.csv
@param datafile String: Path to CSV file containing data to be classified.
@param classfile String: Path to the generated classifier.
@param classtype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib".
@param resultfile String: Path to write out the classified results.
"""
useScikitClassifier("MNB", datafile, classfile, classtype, resultfile)
def generateGNB(datafile, label,
oclass="classifier_GNB.pickle",
otype="pickle",
var_smoothing=1e-9,
classparam=True,
confusion=True,
classreport=True,
cross_validation=5):
"""!
Function to generate a Gaussian Naive Bayes classifier (GNB) from given data.
Usage:
python bactclass.py genGNB --datafile=classifier_train.csv --label=Class --oclass=classifier_GNB.pickle --otype=pickle --var_smoothing=1e-9 --classparam=True --confusion=True --classreport=True --cross_validation=5
@param datafile String: Path to CSV data file used to generate GNB.
@param label String: Column (field) name in the data file to indicate the class label.
@param oclass String: Path to write out the generated classifier. Default = classifier_GNB.pickle
@param otype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib". Default = pickle
@param var_smoothing Float: Portion of the largest variance of all features that is added to variances for calculation stability. Default = 1e-9
@param classparam Boolean: Flag to indicate whether to print out GNB parameters. Default = True
@param confusion Boolean: Flag to indicate whether to print out confusion matrix. Default = True
@param classreport Boolean: Flag to indicate whether to print out classification report. Default = True
@param cross_validation Integer: Number of cross validation (if any) to perform. If less than 2, cross validation will not be carried out. Default = 5
"""
from sklearn.naive_bayes import GaussianNB
print("")
print("Task: Generate Gaussian Naive Bayes Classifier")
print("Parameters:")
print(" Data File = " + str(datafile))
print(" Classification Label = " + str(label))
print(" Variance Smoothing = " + str(var_smoothing))
print(" Classifier File = " + str(oclass))
print(" Classifier File Type = " + str(otype))
print("")
classifier = GaussianNB(var_smoothing=float(var_smoothing))
process_classifier(datafile, label, classifier, oclass, otype,
classparam, confusion, classreport, cross_validation)
print("================= Gaussian NB Generated =================")
def useGNB(datafile, classfile, classtype, resultfile):
"""!
Function to use a previously generated Gaussian Naive Bayes classifier (GNB) classifier to classify data.
Usage:
python bactclass.py useGNB --datafile=classifier_use.csv --classfile=classifier_GNB.pickle --classtype=pickle --resultfile=classifier_result.csv
@param datafile String: Path to CSV file containing data to be classified.
@param classfile String: Path to the generated classifier.
@param classtype String: Type of file to write out the generated classifier. Allowable types are "pickle" and "joblib".
@param resultfile String: Path to write out the classified results.
"""
useScikitClassifier("GNB", datafile, classfile, classtype, resultfile)
if __name__ == "__main__":
exposed_functions = {"genANN": generateANN,
"genBNB": generateBNB,
"genCNB": generateCNB,
"genDT": generateDT,
"genGNB": generateGNB,
"genMNB": generateMNB,
"genSVM": generateSVM,
"recycle": recycle,
"useANN": useANN,
"useBNB": useBNB,
"useCNB": useCNB,
"useDT": useDT,
"useGNB": useGNB,
"useMNB": useMNB,
"useSVM": useSVM}
fire.Fire(exposed_functions)