Spaces:
Runtime error
Runtime error
File size: 8,554 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 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Predict classes for logistic regression model
SYNOPSIS::
SCRIPT [options]
Description
===========
Columns of Outputs:
1. true sample class number
2. predicted class number
3. sensitive feature
4. class 0 probability
5. class 1 probability
Delimiters of columns are a single space.
Options
=======
-i <INPUT>, --in <INPUT>
specify <INPUT> file name
-o <OUTPUT>, --out <OUTPUT>
specify <OUTPUT> file name
-m <MODEL>, --model <MODEL>
trained classifier (default "classification.model")
--ns
ignore sensitive features
--hideinfo
suppress output meta information
-q, --quiet
set logging level to ERROR, no messages unless errors
--rseed <RSEED>
random number seed. if None, use /dev/urandom (default None)
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) 2012 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 ------------------------------------------------------------
from fadm.util import fill_missing_with_mean
#==============================================================================
# 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
# load model file
clr = pickle.load(opt.model)
clr_info = pickle.load(opt.model)
# 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])
S = np.atleast_2d(D[:, -(1 + N_NS):-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())
# prediction and write results
p = clr.predict_proba(X)
# output prediction
n = 0
m = 0
for i in range(p.shape[0]):
c = np.argmax(p[i, :])
opt.outfile.write("%d %d " % (y[i], c))
opt.outfile.write(" ".join(S[i, :].astype(str)) + " ")
opt.outfile.write(str(p[i, 0]) + " " + str(p[i, 1]) + "\n")
n += 1
m += 1 if c == y[i] else 0
# 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 meta info
opt.nos_samples = n
logger.info('nos_samples = ' + str(opt.nos_samples))
opt.nos_correct_samples = m
logger.info('nos_correct_samples = ' + str(opt.nos_correct_samples))
opt.accuracy = m / float(n)
logger.info('accuracy = ' + str(opt.accuracy))
opt.negative_mean_prob = np.mean(p[:, 0])
logger.info('negative_mean_prob = ' + str(opt.negative_mean_prob))
opt.positive_mean_prob = np.mean(p[:, 1])
logger.info('positive_mean_prob = ' + str(opt.positive_mean_prob))
# output meta information
if opt.info:
for key in clr_info.keys():
opt.outfile.write("#classifier_%s=%s\n" %
(key, str(clr_info[key])))
for key, key_val in vars(opt).items():
opt.outfile.write("#%s=%s\n" % (key, str(key_val)))
### post process
# close file
if opt.infile != sys.stdin:
opt.infile.close()
if opt.outfile != sys.stdout:
opt.outfile.close()
if opt.model != sys.stdout:
opt.model.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('-m', '--model', type=argparse.FileType('rb'),
required=True)
ap.set_defaults(ns=False)
ap.add_argument("--ns", dest="ns", action="store_true")
ap.set_defaults(info=True)
ap.add_argument('--hideinfo', dest='info', action='store_false')
# 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)
|