-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_classify.py
73 lines (48 loc) · 2.08 KB
/
train_classify.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
#!/usr/bin/env python3
import pandas as pd
import numpy as np
import warnings
import argparse
import matplotlib.pyplot as plt
from func.preprocess import *
from func.balance import *
from func.classifier import *
from func.feature_selection import *
pd.options.mode.chained_assignment = None
warnings.filterwarnings("ignore", category=DeprecationWarning)
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", help="input file.",required=True)
parser.add_argument("-c", "--classifier",default='boot', help="classifier methods: kfold or boot")
parser.add_argument("-f", "--model", default='forest', help="feature selection \
model: logistic, forest, boosting")
parser.add_argument("-o", "--output", default='classify_model.sav', help="output model")
args = parser.parse_args()
# read in data
try:
data = pd.read_csv(args.input, header=None)
print("Data loaded")
except:
print("Dataset could not be loaded. Is the dataset missing? Please use -i inputfile")
data.columns = ['AAGE', 'ACLSWKR', 'ADTIND', 'ADTOCC', 'AHGA', 'AHRSPAY',
'AHSCOL', 'AMARITL', 'AMJIND', 'AMJOCC', 'ARACE', 'AREORGN',
'ASEX', 'AUNMEM', 'AUNTYPE', 'AWKSTAT', 'CAPGAIN', 'CAPLOSS',
'DIVVAL', 'FILESTAT', 'GRINREG', 'GRINST', 'HHDFMX', 'HHDREL',
'MARSUPWT', 'MIGMTR1', 'MIGMTR3', 'MIGMTR4', 'MIGSAME', 'MIGSUN',
'NOEMP', 'PARENT', 'PEFNTVTY', 'PEMNTVTY', 'PENATVTY', 'PRCITSHP',
'SEOTR', 'VETQVA', 'VETYN', 'WKSWORK', 'YEAR', 'TARGET']
# preprocess data
data = preprocess(data)
# balance data by SMOTE
X, y, data = handle_imbalanced_data(data)
# drop instance weight from data
instance_weight = data.MARSUPWT
data = data.drop('MARSUPWT',axis=1)
# drop label from data
X = data.drop('TARGET',axis=1)
# methods of classifier
'''
method: Kfold_cross_validation, Bootstrap
'''
if args.classifier == 'kfold':
warnings.warn("Kfold cross validation is only used to evaluate models, does not generate model file")
classifier_method(X,y,data,instance_weight,args.classifier,args.output,args.model)