This file is indexed.

/usr/share/pyshared/mlpy/_fda.py is in python-mlpy 2.2.0~dfsg1-2.

This file is owned by root:root, with mode 0o644.

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
 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
## This file is part of MLPY.
## Fisher Discriminant Analysis.

## This is an implementation of Fisher Discriminant Analysis described in:
## 'An Improved Training Algorithm for Kernel Fisher Discriminants' S. Mika,
## A. Smola, B Scholkopf. 2001.
    
## This code is written by Roberto Visintainer, <visintainer@fbk.eu> and
## Davide Albanese <albanese@fbk.eu>.
## (C) 2008 Fondazione Bruno Kessler - Via Santa Croce 77, 38100 Trento, ITALY.

## 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 3 of the License, or
## (at your option) any later version.

## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.

__all__ = ['Fda']

from numpy import *
from numpy.linalg import inv
import random as rnd


def dot3(a1, M, a2):
    """Compute a1 * M * a2T
    """
    
    a1M = dot(a1, M)
    res = inner(a1M, a2)
    return res



class Fda:
    """Fisher Discriminant Analysis.

    Example:
    
    >>> import numpy as np
    >>> import mlpy
    >>> xtr = np.array([[1.0, 2.0, 3.1, 1.0],  # first sample
    ...                 [1.0, 2.0, 3.0, 2.0],  # second sample
    ...                 [1.0, 2.0, 3.1, 1.0]]) # third sample
    >>> ytr = np.array([1, -1, 1])             # classes
    >>> myfda = mlpy.Fda()                   # initialize fda class
    >>> myfda.compute(xtr, ytr)              # compute fda
    1
    >>> myfda.predict(xtr)                   # predict fda model on training data
    array([ 1, -1,  1])
    >>> xts = np.array([4.0, 5.0, 6.0, 7.0]) # test point
    >>> myfda.predict(xts)                   # predict fda model on test point
    -1
    >>> myfda.realpred                       # real-valued prediction
    -42.51475717037367
    >>> myfda.weights(xtr, ytr)              # compute weights on training data
    array([  9.60629896,   9.77148463,   9.82027615,  11.58765243])
    """
    
    def __init__(self, C = 1):
        """
        Initialize Fda class.
        
        :Parameters:
          C : float
            regularization parameter
        """
                      
        self.__C = C
        self.__w = 'cr'
                
        self.__x     = None
        self.__y     = None
        self.__xpred = None
        
        self.__a  = None
        self.__b  = None
        self.__K  = None
        
                
    def __stdinvH(self, x, C):
        """Build matrix H and invert it.
        
        See eq. 4 at page 2.
        
        Matrix H:
        |-------------------------|
        |l(Val)       | oneK(Vet) | 
        |-------------------------|
        |oneKT(Vet)   |  M(Mat)   |                       
        |-------------------------|
        """       
        
        # Compute kernel matrix
        xT = x.transpose()
        K  = dot(x, xT)
        KT = K # (symmetric matrix)
        
        # Alloc H
        H = empty((K.shape[0] + 1, K.shape[0] + 1), dtype = float)
        
        # Compute oneK = 1T * K
        oneK = K.sum(axis = 0)
        
        # Build H
        # Compute M = (KT * K) + (C * P)
        H[1:, 1:] = dot(KT, K) + identity(K.shape[1]) * C  
        H[0, 1:] = oneK
        H[1:, 0] = oneK
        H[0, 0]  = x.shape[0]
        
        invH = inv(H)
        
        return (K, KT, invH)
    

    def __compute_a(self, x, y, KT, invH):
        """Compute a

        See eq. 8, 9 at page 3.
        """
        
        lp = y[y ==  1].shape[0]
        ln = y[y == -1].shape[0]
        
        # Compute c, A+ and A-.
        # See eq. 4 at page 2. 
        
        c = append((lp - ln), dot(KT, y))

        onep = zeros_like(y)
        onen = zeros_like(y)
        onep[y == 1 ] = 1
        onen[y == -1] = 1
                
        Ap = append(lp, dot(KT, onep))
        An = append(ln, dot(KT, onen))

        # Compute lambda
        # See eq. 9 at page 3.

        A = dot3(Ap, invH, Ap)
        B = dot3(Ap, invH, An)
        C = dot3(An, invH, Ap)
        D = dot3(An, invH, An)
        E = -(lp) + dot3(c, invH, Ap)
        F = ln + dot3(c, invH, An)
        G = -0.5 * dot3(c, invH, c)
                    
        lambdan = ( -F + ((C + B) * E / (2 * A)) ) / \
                  ( -D + ((C + B)**2  / (4 * A)) )
        lambdap = ( -E + (0.5 * (C + B) * lambdan) ) / -A

        # Compute a
        # See eq. 8 at page 3.

        lambdaAp = dot(lambdap, Ap)
        lambdaAn = dot(lambdan, An)

        a = dot(invH, (c - (lambdaAp + lambdaAn)))
                
        return a

    
    def __standard(self):
        self.__K, KT, invH = self.__stdinvH(self.__x, self.__C)
        a                  = self.__compute_a(self.__x, self.__y, KT, invH)
        
        self.__xpred = self.__x
        
        # Return b, a
        return a[0], a[1:] 
    
    
    def compute(self, x, y):
        """Compute fda model.

        :Parameters:
          x : 2d numpy array float (sample x feature)
            training data
          y : 1d numpy array integer (two classes, 1 or -1)
            classes

        :Returns:
          1
        """

        self.__x = x
        self.__y = y
               
        self.__b, self.__a = self.__standard()           
        
        return 1
        
    def predict(self, p):
        """Predict fda model on test point(s).

        :Parameters:
          p : 1d or 2d ndarray float (sample(s) x feats)
            test sample(s)

        :Returns:
          cl : integer or 1d numpy array integer
            class(es) predicted

        :Attributes:
          self.realpred : float or 1d numpy array float
            real valued prediction
        """
        
        
        if p.ndim == 2:
            
            # Real prediction
            pT = p.transpose()
            K  = dot(self.__xpred, pT)
            self.realpred = dot(self.__a, K) + self.__b
            
            # Prediction
            pred = zeros(p.shape[0], dtype = int)
            pred[self.realpred > 0.0] = 1
            pred[self.realpred < 0.0] = -1
            
        elif p.ndim == 1:
            
            # Real prediction
            pT = p.reshape(-1, 1)
            K  = dot(self.__xpred, pT)
            self.realpred = (dot(self.__a, K) + self.__b)[0]               
            
            # Prediction
            pred = 0.0
            if self.realpred > 0.0:
                pred = 1
            elif self.realpred < 0.0:
                pred = -1
                
        return pred
    

    def weights (self, x, y):
        """
        Return feature weights.
        
        :Parameters:
          x : 2d ndarray float (samples x feats)
            training data
          y : 1d ndarray integer (-1 or 1)
            classes
        
        :Returns:
          fw :  1d ndarray float
            feature weights
        """

        self.compute(x,y)

     
        if self.__w == 'cr':

            n1idx = where(y ==  1)[0]
            n2idx = where(y == -1)[0]
            idx   = append(n1idx, n2idx)

            y = self.__y[idx]

            K = self.__K[idx][:, idx]
            
            target = ones((y.shape[0], y.shape[0]), dtype = int)
            target[:n1idx.shape[0], n1idx.shape[0]:] = -1
            target[n1idx.shape[0]:, :n1idx.shape[0]] = -1

            yy = trace(dot(target, target))

            w = empty(x.shape[1], dtype = float)
            
            for i in range(x.shape[1]):
                mask = dot(x[:, i].reshape(-1, 1), x[:, i].reshape(1, -1))
                newK = K - mask

                w[i] = sqrt( trace(dot(newK, newK)) * yy) / trace(dot(newK, target))


            return w