Spaces:
Runtime error
Runtime error
File size: 9,860 Bytes
d2a8669 |
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 |
#!/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
-C <REG>, --reg <REG>
regularization parameter (default 1.0)
-e <eta>, --eta <eta>
fairness penalty parameter (default 1.0)
-l <LTYPE>, --ltype <LTYPE>
likehood fitting type (default 4)
-t <NTRY>, --try <NTRY>
the number of trials with random restart. if 0, all coefficients are
initialized by zeros, and a model is trained only once. (default 0)
-n <ITYPE>, --itype <ITYPE>
method to initialize coefficients. 0: by zero, 1: at random following
normal distribution, 2: learned by standard LR, 3: separately learned by
standard LR (default 3)
-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
from subprocess import getoutput
import logging
import datetime
import pickle
import numpy as np
# private modeules ------------------------------------------------------------
import site
site.addsitedir('.')
from fadm import __version__ as fadm_version
from sklearn import __version__ as sklearn_version
from fadm.util import fill_missing_with_mean
from fadm.lr.pr import *
#==============================================================================
# Public symbols
#==============================================================================
__all__ = []
#==============================================================================
# Constants
#==============================================================================
N_NS = 1
#==============================================================================
# Module variables
#==============================================================================
#==============================================================================
# Classes
#==============================================================================
#==============================================================================
# Functions
#==============================================================================
def train(X, y, ns, opt):
""" train model
Parameters
----------
X : ary, shape=(n_samples, n_features)
features
y : ary, shape=(n_samples)
classes
ns : int
the number of sensitive features
opt : object
options
Returns
-------
clr : classifier object
trained classifier
"""
if opt.ltype == 4:
clr = LRwPRType4(eta=opt.eta, C=opt.C)
clr.fit(X, y, ns, itype=opt.itype)
else:
sys.exit("Illegal likelihood fitting type")
return clr
#==============================================================================
# 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])
X = fill_missing_with_mean(D[:, :-1])
del D
### 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())
# init constants
ns = 1
# train
if opt.ntry <= 0:
# train only once with zero coefficients
clr = train(X, y, ns, opt)
opt.final_loss = clr.f_loss_
logger.info('final_loss = ' + str(opt.final_loss))
else:
# train multiple times with random restarts
clr = None
best_loss = np.inf
best_trial = 0
for trial in range(opt.ntry):
logger.info("Trial No. " + str(trial + 1))
tmp_clr = train(X, y, ns, opt)
logger.info("loss = " + str(tmp_clr.f_loss_))
if tmp_clr.f_loss_ < best_loss:
clr = tmp_clr
best_loss = clr.f_loss_
best_trial = trial + 1
opt.final_loss = best_loss
logger.info('final_loss = ' + str(opt.final_loss))
opt.best_trial = best_trial
logger.info('best_trial = ' + str(opt.best_trial))
# 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.sklearn_version = sklearn_version
logger.info('sklearn_version = ' + opt.sklearn_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).items():
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('wb'))
ap.add_argument('outfilep', nargs='?', metavar='OUTFILE',
default=sys.stdout, type=argparse.FileType('wb'))
# script specific options
ap.add_argument('-C', '--reg', dest='C', type=float, default=1.0)
ap.set_defaults(ns=False)
ap.add_argument('-e', '--eta', type=float, default=1.0)
ap.add_argument('-l', '--ltype', type=int, default=4)
ap.add_argument('-n', '--itype', type=int, default=3)
ap.set_defaults(ns=False)
ap.add_argument('--ns', dest='ns', action='store_true')
ap.add_argument('-t', '--try', dest='ntry', type=int, default=0)
# 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 =\
getoutput('system_profiler'
' -detailLevel mini SPHardwareDataType')\
.split('\n')[4:-1]
elif platform.system() == 'FreeBSD':
opt.sys_info = getoutput('sysctl hw').split('\n')
elif platform.system() == 'Linux':
opt.sys_info = getoutput('cat /proc/cpuinfo').split('\n')
### suppress warnings in numerical computation
np.seterr(all='ignore')
### call main routine
main(opt)
|