Definition at line 26 of file LogisticRegression.h. References Impala::Core::Matrix::MatMul(), Impala::Core::Matrix::MatPseudoInverse(), Impala::Core::Matrix::MatTranspose(), Impala::Core::Array::Pattern::PatSet(), and Impala::Core::Array::SetVal(). 00027 { 00028 /* 00029 %LOGLC Logistic Linear Classifier 00030 % 00031 % W = LOGLC(A) 00032 % 00033 % INPUT 00034 % A Dataset 00035 % 00036 % OUTPUT 00037 % W Logistic linear classifier 00038 % 00039 % DESCRIPTION 00040 % Computation of the linear classifier for the dataset A by maximizing the 00041 % likelihood criterion using the logistic (sigmoid) function. 00042 % 00043 % REFERENCES 00044 % A. Webb, Statistical Pattern Recognition, John Wiley & Sons, New York, 2002. 00045 % 00046 % SEE ALSO 00047 % MAPPINGS, DATASETS, LDC, FISHERC 00048 00049 % Copyright: R.P.W. Duin, duin@ph.tn.tudelft.nl 00050 % Faculty of Applied Physics, Delft University of Technology 00051 % P.O. Box 5046, 2600 GA Delft, The Netherlands 00052 00053 % $Id: loglc.m,v 1.9 2003/11/22 23:20:39 bob Exp $ 00054 */ 00055 // [m,k,c] = getsize(a); aantal objecten, aantal features (dimensies), classes 00056 int sampleCount = mTrainSet->Size(); 00057 int vectorLength = mTrainset->GetColumn3()->GetVectorLength(0); 00058 // nlab = getnlab(a); vraag labels per object [-1,+1] -> vector 00059 std::vector<double> labels = GetDistinct(mTrainSet->GetColumn2()); 00060 if(labels.size() != 2) 00061 std::cerr << "[LogisticRegression::Train] multiclass not supported (yet)" << std::endl; 00062 int class1 = CountSelect(mTrainSet->GetColumn2(), labels[0]); 00063 // prior = getprior(a); de a priori kansen per classe -> [p(+1), p(-1)] 00064 double prior[2] = {(double)class1 / (double)sampleCount, (double)(sampleCount-class1) / (double)sampleCount}; 00065 // v = scalem(a,'variance'); schalen op variantie (per feature) 00066 // a = a*v; <-(hier wordt de schaling pas toegepast) 00067 ScaleMeanAndVariance(mTrainSet->GetColumn3()); 00068 00069 // x = [+a,ones(m,1)]; matrix met de features en een 1 erachter (de + haalt de data uit de dataset) 00070 VectorSet* x = new VectorSet(true, vectorLength+1, sampleCount); 00071 SetVal(x->GetStorage(), x->GetStorage(), 1); 00072 PatSet(x->GetStorage(), mTrainSet->GetColumn3()->GetStorage(), 0, 0, 0, vectorLength, 0, 0); 00073 00074 // % A standard trick to set the labels to +1 for the first class 00075 // % (NLAB=1) and to -1 for the second one (NLAB=2). Then, each 00076 // % object vector is multiplied by its new label +/-1. 00077 // x(find(nlab==2),:) = -x(find(nlab==2),:); inverteer de features van een van de classes 00078 int i; 00079 for(i=0 ; i<sampleCount ; i++) 00080 if(mTrainSet->Get2(i) == -1) 00081 mTrainSet->Get3(i) = -mTrainSet->Get3(i); 00082 00083 // alf = sum(nlab==2)/sum(nlab==1); ratio van de labels 00084 double alf = (double)class1 / (double)(sampleCount-class1); 00085 // weights = zeros(1,k+1); model gewichten, length = feature lengte + 1 00086 double weights[vectorLength+1] = {0}; 00087 // % Maximize the likelihood L to find WEIGHTS 00088 // L = -inf; Lnew = -realmax; 00089 double L = -1e100; 00090 double Lnew = 1e100; 00091 // accuracy = 0.0001; 00092 // while (abs(Lnew - L) > accuracy) 00093 while(abs(Lnew - L) > 0.001) 00094 { 00095 // pax = ones(m,1) ./ (1 + exp(-x*weights')); % Estimate of P(class +1|x). 00096 // ' = transpose 00097 // pax -> m rijen 1 colom -> 00098 // prob = x*transpose(weights); 00099 Vector prob = MatMul(x, weights); 00100 // pax[i] = 1/1+exp(-prob[i]); 00101 double pax[sampleCount]; 00102 for(i=0 ; i<sampleCount ; ++i) 00103 pax[i] = 1. / 1.+exp(-weights2[i]); 00104 // pbx = 1 - pax; % Estimate of P(class -1|x). 00105 double pbx[sampleCount]; 00106 for(i=0 ; i<sampleCount ; ++i) 00107 pbx[i] = 1. - pax[i]; 00108 // L = Lnew; Lnew = sum(log(pax+realmin)); % Update likelihood. 00109 L = Lnew; 00110 Lnew = 0; 00111 for(i=0 ; i<sampleCount ; ++i) 00112 Lnew += log(pax[i] + 1e-100); 00113 // p2x = sqrt(pax.*pbx); p2x colom vector -> p2x[i] = sqrt(pax[i]*pbx[i]) 00114 double p2x[sampleCount]; 00115 for(i=0 ; i<sampleCount ; ++i) 00116 p2x[i] = sqrt(pax[i]*pbx[i]); 00117 // y = x .* p2x(:,ones(1,k+1)); -> deze constructie maakt een repeterende matrix -> puntsgewijs met matrix x vermenigvuldigen 00118 Matrix y = x; 00119 for(i=0 ; i<sampleCount ; ++i) 00120 y.GetColumn(i) *= p2x[i]; 00121 // weights = pbx' * x * pinv(y'*y) + weights; 00122 Matrtix weightupdate = MatMul(MatTranspose(pbx),MatMul(x,MatPseudoInverse(MatMul(MatTranspose(y), y))); 00123 for(i=0 ; i<vectorLength+1 ; ++i) 00124 weights[i] += weightupdate.Value(0, i); 00125 } 00126 // end 00127 // % Define LOGLC by an affine (linear) mapping based 00128 // % on the [K x 1] matrix R and the offset w0. 00129 // w0 = weights(k+1) + log(alf*prior(1)/prior(2)); 00130 double w0 = weights.Value(vectorLength) + log(alf*prior[0]/prior[1]); 00131 // R = weights(1:k)'; 00132 // W = v*affine(R,w0,a,getlablist(a)); 00133 // W = setout_conv(W,1); 00134 }
Here is the call graph for this function:
|