/usr/bin/knn-landscape is in python-mlpy 2.2.0~dfsg1-3build3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 | #!/usr/bin/python
from numpy import *
from optparse import OptionParser
from mlpy import *
# Command line parsing
parser = OptionParser()
parser.add_option("-d", "--data", metavar = "FILE", action = "store", type = "string",
dest = "data", help = "data - required")
parser.add_option("-s", "--standardize", action = "store_true", default = False,
dest = "stand", help = "standardize data")
parser.add_option("-n", "--normalize", action = "store_true", default = False,
dest = "norm", help = "normalize data")
parser.add_option("-k", action = "store", type = "int",
dest = "k", help = "k for k-fold cross validation")
parser.add_option("-c", action = "store", type = "int", nargs = 2, metavar = "SETS PAIRS",
dest = "c", help = "sets and pairs for monte carlo cross validation")
parser.add_option("-S", "--stratified", action = "store_true", default = False,
dest = "strat", help = "for stratified cv")
parser.add_option("-K", action = "store", type = "int",
dest = "K", help = "number of nearest neighbors [default %default]", default=1)
parser.add_option("-l", "--distance", action = "store", type = "string",
dest = "dist", help = "type of distance: 'se' (SQUARED EUCLIDEAN) \
or 'e' (EUCLIDEAN) [default %default]", default = "se")
(options, args) = parser.parse_args()
if not options.data:
parser.error("option -d (data) is required")
if not (options.k or options.c):
parser.error("option -k (k-fold) or -c (monte carlo) for resampling is required")
if (options.k and options.c):
parser.error("option -k (k-fold) and -c (monte carlo) are mutually exclusive")
if not options.dist in ["se", "e"]:
parser.error("option -l (type of distance) should be 'se' or 'e")
# Data
x, y = data_fromfile(options.data)
if options.stand:
x = data_standardize(x)
if options.norm:
x = data_normalize(x)
print "samples:", x.shape[0]
print "features:", x.shape[1]
# Resampling
if options.strat:
if options.k:
print "stratified %d-fold cv" % options.k
res = kfoldS(cl = y, sets = options.k)
elif options.c:
print "stratified monte carlo cv (%d sets, %d pairs)" %(options.c[0], options.c[1])
res = montecarloS(cl = y, sets = options.c[0], pairs = options.c[1])
else:
if options.k:
print "%d-fold cv" % options.k
res = kfold(nsamples = y.shape[0], sets = options.k)
elif options.c:
print "monte carlo cv (%d sets, %d pairs)" %(options.c[0], options.c[1])
res = montecarlo(nsamples = y.shape[0], sets = options.c[0], pairs = options.c[1])
# Compute
n = Knn(k = options.K, dist = options.dist) # Initialize nn class
ERR = 0.0 # Initialize error
MCC = 0.0 # Initialize mcc
for r in res:
xtr, ytr, xts, yts = x[r[0]], y[r[0]], x[r[1]], y[r[1]]
n.compute(xtr, ytr)
p = n.predict(xts)
ERR += err(yts, p)
MCC += mcc(yts, p)
ERR /= float(len(res))
MCC /= float(len(res))
print "error %f, mcc %f" % (ERR, MCC)
|