-
Notifications
You must be signed in to change notification settings - Fork 3
/
construct_features.py
69 lines (47 loc) · 1.61 KB
/
construct_features.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
#!/usr/bin/env python2.6
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Written (W) 2011 Christian Widmer
# Copyright (C) 2011 Max-Planck-Society
"""
Created on 02.08.2011
@author: Christian Widmer
@summary: Includes some code to construct shogun feature objects
"""
import numpy
from shogun.Features import RealFeatures
def construct_features(features, transpose=True):
"""
constructs a simple feature object
"""
# sanity check
lengths = set([len(xt) for xt in features])
assert len(lengths) == 1, "unequal feature vector lengths %s" % (str(lengths))
# assume real features
examples = numpy.array(features, dtype=numpy.float64)
if transpose:
examples = numpy.transpose(examples)
print "data format when creating features:", examples.shape
feat = RealFeatures(examples)
return feat
def discretize_labels(labels, cutoff):
"""
discretize labels according to given cutoff
"""
ret_labels = []
num_neg = 0
num_pos = 0
for lab in labels:
if lab < cutoff:
num_neg += 1
ret_labels.append(-1.0)
else:
num_pos += 1
ret_labels.append(1.0)
print "discretized labels, num_neg=%i, num_pos=%i" % (num_neg, num_pos)
assert(len(ret_labels) == len(labels))
assert(num_pos+num_neg == len(labels))
return ret_labels