forked from tkamishima/kamfadm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_lr.py
executable file
·254 lines (200 loc) · 7.55 KB
/
train_lr.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
training logistic regression
SYNOPSIS::
SCRIPT [options]
Description
===========
The last column indicates binary class.
Options
=======
-i <INPUT>, --in <INPUT>
specify <INPUT> file name
-o <OUTPUT>, --out <OUTPUT>
specify <OUTPUT> file name
--ns
ignore sensitive features
-C <REG>, --reg <REG>
regularization parameter (default 1.0)
-q, --quiet
set logging level to ERROR, no messages unless errors
--rseed <RSEED>
random number seed. if None, use /dev/urandom (default None)
--version
show version
Attributes
==========
N_NS : int
the number of non sensitive features
"""
#==============================================================================
# Module metadata variables
#==============================================================================
__author__ = "Toshihiro Kamishima ( http://www.kamishima.net/ )"
__date__ = "2012/08/26"
__version__ = "3.0.0"
__copyright__ = "Copyright (c) 2011 Toshihiro Kamishima all rights reserved."
__license__ = "MIT License: http://www.opensource.org/licenses/mit-license.php"
__docformat__ = "restructuredtext en"
#==============================================================================
# Imports
#==============================================================================
import sys
import argparse
import os
import platform
import commands
import logging
import datetime
import pickle
import numpy as np
# private modeules ------------------------------------------------------------
import site
site.addsitedir('.')
from fadm import __version__ as fadm_version
from fadm.util import fill_missing_with_mean
from sklearn.linear_model import LogisticRegression
#==============================================================================
# Public symbols
#==============================================================================
__all__ = []
#==============================================================================
# Constants
#==============================================================================
N_NS = 1
#==============================================================================
# Module variables
#==============================================================================
#==============================================================================
# Classes
#==============================================================================
#==============================================================================
# Functions
#==============================================================================
#==============================================================================
# Main routine
#==============================================================================
def main(opt):
""" Main routine that exits with status code 0
"""
### pre process
# read data
D = np.loadtxt(opt.infile)
# split data and process missing values
y = np.array(D[:, -1])
if opt.ns:
X = fill_missing_with_mean(D[:, :-(1 + N_NS)])
else:
X = fill_missing_with_mean(D[:, :-1])
### main process
# set starting time
start_time = datetime.datetime.now()
start_utime = os.times()[0]
opt.start_time = start_time.isoformat()
logger.info("start time = " + start_time.isoformat())
# main process
clr = LogisticRegression(C=float(opt.C), penalty='l2', fit_intercept=True)
clr.fit(X, y)
# set end and elapsed time
end_time = datetime.datetime.now()
end_utime = os.times()[0]
logger.info("end time = " + end_time.isoformat())
opt.end_time = end_time.isoformat()
logger.info("elapsed_time = " + str((end_time - start_time)))
opt.elapsed_time = str((end_time - start_time))
logger.info("elapsed_utime = " + str((end_utime - start_utime)))
opt.elapsed_utime = str((end_utime - start_utime))
### output
# add info
opt.nos_samples = X.shape[0]
logger.info('nos_samples = ' + str(opt.nos_samples))
opt.nos_features = X.shape[1]
logger.info('nos_features = ' + str(X.shape[1]))
opt.classifier = clr.__class__.__name__
logger.info('classifier = ' + opt.classifier)
opt.fadm_version = fadm_version
logger.info('fadm_version = ' + opt.fadm_version)
# opt.training_score = clr.score(X, y)
# logger.info('training_score = ' + str(opt.training_score))
# write file
pickle.dump(clr, opt.outfile)
info = {}
for key, key_val in vars(opt).iteritems():
info[key] = str(key_val)
pickle.dump(info, opt.outfile)
### post process
# close file
if opt.infile is not sys.stdin:
opt.infile.close()
if opt.outfile is not sys.stdout:
opt.outfile.close()
sys.exit(0)
### Preliminary processes before executing a main routine
if __name__ == '__main__':
### set script name
script_name = sys.argv[0].split('/')[-1]
### init logging system
logger = logging.getLogger(script_name)
logging.basicConfig(level=logging.INFO,
format='[%(name)s: %(levelname)s'
' @ %(asctime)s] %(message)s')
### command-line option parsing
ap = argparse.ArgumentParser(
description='pydoc is useful for learning the details.')
# common options
ap.add_argument('--version', action='version',
version='%(prog)s ' + __version__)
apg = ap.add_mutually_exclusive_group()
apg.set_defaults(verbose=True)
apg.add_argument('--verbose', action='store_true')
apg.add_argument('-q', '--quiet', action='store_false', dest='verbose')
ap.add_argument("--rseed", type=int, default=None)
# basic file i/o
ap.add_argument('-i', '--in', dest='infile',
default=None, type=argparse.FileType('r'))
ap.add_argument('infilep', nargs='?', metavar='INFILE',
default=sys.stdin, type=argparse.FileType('r'))
ap.add_argument('-o', '--out', dest='outfile',
default=None, type=argparse.FileType('w'))
ap.add_argument('outfilep', nargs='?', metavar='OUTFILE',
default=sys.stdout, type=argparse.FileType('w'))
# script specific options
ap.add_argument('-C', '--reg', dest="C", type=float, default=1.0)
ap.set_defaults(ns=False)
ap.add_argument("--ns", dest="ns", action="store_true")
# parsing
opt = ap.parse_args()
# post-processing for command-line options
# disable logging messages by changing logging level
if not opt.verbose:
logger.setLevel(logging.ERROR)
# set random seed
np.random.seed(opt.rseed)
# basic file i/o
if opt.infile is None:
opt.infile = opt.infilep
del vars(opt)['infilep']
logger.info("input_file = " + opt.infile.name)
if opt.outfile is None:
opt.outfile = opt.outfilep
del vars(opt)['outfilep']
logger.info("output_file = " + opt.outfile.name)
### set meta-data of script and machine
opt.script_name = script_name
opt.script_version = __version__
opt.python_version = platform.python_version()
opt.sys_uname = platform.uname()
if platform.system() == 'Darwin':
opt.sys_info =\
commands.getoutput('system_profiler'
' -detailLevel mini SPHardwareDataType')\
.split('\n')[4:-1]
elif platform.system() == 'FreeBSD':
opt.sys_info = commands.getoutput('sysctl hw').split('\n')
elif platform.system() == 'Linux':
opt.sys_info = commands.getoutput('cat /proc/cpuinfo').split('\n')
### suppress warnings in numerical computation
np.seterr(all='ignore')
### call main routine
main(opt)