26 namespace LogisticModel{
31 const Pds::Vector &W0);
40 const Pds::Vector &Y);
44 const Pds::Vector &Y);
55 if(W.IsEmpty())
return Pds::Ra::Nan;
56 if(X.IsEmpty())
return Pds::Ra::Nan;
57 if(Y.IsEmpty())
return Pds::Ra::Nan;
59 if(X.Nlin()!=Y.Nlin())
return Pds::Ra::Nan;
60 if(X.Ncol()!=(W.Nlin()-1))
return Pds::Ra::Nan;
62 Pds::Vector Yh=Y.Geq(0.5);
66 unsigned int L=Yh.Nlin();
85 double F=Pds::qHbn(0.5,2.0);
86 double Et=Pds::qHbn(Nt/L,q)/F;
87 double E1=Pds::qHbn(N1/Na,q)/F;
88 double val=(Nt-N1)/(L-Na);
92 double E0=Pds::qHbn(val,q)/F;
96 return Et-(Na/L)*E1-((L-Na)/L)*E0;
102 const Pds::Matrix &X,
103 const Pds::Vector &Y,
106 if(W.IsEmpty())
return Pds::Vector();
107 if(X.IsEmpty())
return Pds::Vector();
108 if(Y.IsEmpty())
return Pds::Vector();
110 if(X.Nlin()!=Y.Nlin())
return Pds::Vector();
111 if(X.Ncol()!=(W.Nlin()-1))
return Pds::Vector();
113 if(h==0.0)
return Pds::Vector();
115 unsigned int N=W.Nel();
125 for(
unsigned int n=0;n<N;n++)
127 Whp.SetRaw(n,W.GetRaw(n)+h);
128 Whm.SetRaw(n,W.GetRaw(n)-h);
135 Whp.SetRaw(n,W.GetRaw(n));
136 Whm.SetRaw(n,W.GetRaw(n));
142 const Pds::Matrix &X,
143 const Pds::Vector &Y)
145 if(W.IsEmpty())
return Pds::Vector();
146 if(X.IsEmpty())
return Pds::Vector();
147 if(Y.IsEmpty())
return Pds::Vector();
149 if(X.Nlin()!=Y.Nlin())
return Pds::Vector();
150 if(X.Ncol()!=(W.Nlin()-1))
return Pds::Vector();
152 Pds::Vector Yt=Y.Geq(0.5);
153 unsigned int N=Yt.Sum();
154 unsigned int L=Yt.Nel();
155 Pds::Matrix R=Pds::RegressorMatrix(X);
156 Pds::Vector z=Pds::Sigmoid(R*W);
157 Pds::Vector Dz=z.Product(1.0-z);
162 double p2=(N-N1)/(L-Na);
164 double h1=Pds::Hb(p1);
165 double h2=Pds::Hb(p2);
166 double Dh1=-Pds::Logit2(p1);
167 double Dh2=-Pds::Logit2(p2);
176 double factor1=(-h1+h2+p1*Dh1-p2*Dh2)/L;
177 double factor2=(-Dh1+Dh2)/L;
178 Pds::Vector DIG= factor1*R.TMul(Dz)+factor2*R.TMul(Dz.Product(Yt));
181 std::cout<<
"N:"<<N<<
"\n";
182 std::cout<<
"L:"<<L<<
"\n";
183 std::cout<<
"N1:"<<N1<<
"\t N1<Na True positive in Na side\n";
184 std::cout<<
"Na:"<<Na<<
"\t elems. classify as 1\n";
185 std::cout<<
"p1:"<<p1<<
"\n";
186 std::cout<<
"p2:"<<p2<<
"\n";
187 std::cout<<
"factor1:"<<factor1<<
"\n";
188 std::cout<<
"factor2:"<<factor2<<
"\n";
191 DIG.T().Print(
"DIG: ");
198 void print_IterationConf_IG_soft_init_data(
Pds::IterationConf Conf,std::string str=
"")
204 std::ios_base::fmtflags f( std::cout.flags() );
206 std::cout.precision(4);
207 std::cout<<
"┌──────────────────────────────┐"<<std::endl;
208 std::cout<<
"│ "<<std::left<<std::setw(22)<<str<<
" │"<<std::endl;
209 std::cout<<
"│ IterationConf init data │"<<std::endl;
210 std::cout<<
"├──────────────────────────────┤"<<std::endl;
211 std::cout<<
"│ Alpha: "<<std::setw(10)<<Alpha <<
" │"<<std::endl;
212 std::cout<<
"│ MinErrorΔ: "<<std::setw(10)<<std::scientific<<MinError<<std::defaultfloat<<
" │"<<std::endl;
213 std::cout<<
"│ MaxIter: "<<std::setw(10)<<MaxIter<<
" │"<<std::endl;
214 std::cout<<
"└──────────────────────────────┘"<<std::endl;
216 std::cout.flags( f );
221 std::ios_base::fmtflags f( std::cout.flags() );
224 std::cout.precision(3);
225 std::cout<<
"┌──────────────────────────────┐"<<std::endl;
226 std::cout<<
"│ "<<std::left<<std::setw(22)<<str<<
" │"<<std::endl;
227 std::cout<<
"│ IterationConf end data │"<<std::endl;
228 std::cout<<
"├──────────────────────────────┤"<<std::endl;
229 std::cout<<
"│ Alpha: "<<std::setw(10)<<Alpha<<
" │"<<std::endl;
230 std::cout<<
"│ ErrorΔ: "<<std::setw(10)<<std::scientific<<Conf.
LastError<<std::defaultfloat<<
" │"<<std::endl;
231 std::cout<<
"│ LastIter: "<<std::setw(10)<<Conf.
LastIter<<
" │"<<std::endl;
232 std::cout<<
"└──────────────────────────────┘"<<std::endl;
234 std::cout.flags( f );
236 void print_iterative_IG_soft_data(
unsigned int iter,
double Alpha,
double last_error,
double cost_error,
bool END)
239 std::ios_base::fmtflags f( std::cout.flags() );
241 std::cout.precision(3);
243 <<
"┌──────────────────────────────┐"<<std::endl
244 <<
"│ Iter: "<<std::setw(10)<<iter<<
" │"<<std::endl
245 <<
"│ Alpha: "<<std::setw(10)<<Alpha<<
" │"<<std::endl
246 <<
"│ ErrorΔ: "<<std::setw(10)<<std::scientific<<last_error<<
" │"<<std::endl
247 <<
"│ IG: "<<std::setw(10)<<cost_error<<std::defaultfloat<<
" │"<<std::endl
248 <<
"└──────────────────────────────┘"<<std::endl;
249 if(END==
false) std::cout<<
"\x1b[A\r"<<
"\x1b[A\r"<<
"\x1b[A\r"<<
"\x1b[A\r"<<
"\x1b[A\r"<<
"\x1b[A\r";
251 std::cout.flags( f );
255 const Pds::Matrix &X,
256 const Pds::Vector &Y,
257 const Pds::Vector &W0)
259 if(W0.IsEmpty())
return Pds::Matrix();
260 if(X.IsEmpty())
return Pds::Matrix();
261 if(Y.IsEmpty())
return Pds::Matrix();
263 if(X.Nlin()!=Y.Nlin())
return Pds::Matrix();
264 if(X.Ncol()!=(W0.Nlin()-1))
return Pds::Matrix();
266 unsigned int YSUM=Y.Geq(0.5).Sum();
276 pds_print_error_message(
"Esta función aun no trabaja bien, es demasiado abrupta");
278 if(Conf.
Show) print_IterationConf_IG_soft_init_data(Conf,
"Gradient IG");
287 Pds::Fir FIR5(Pds::Vector(5,1.0)/5);
292 double last_error=1000, before_error=0,delta_error=1;;
295 Pds::Matrix R=Pds::RegressorMatrix(X);
297 Pds::Matrix I=Pds::Eye(W.Nel());I.SetRaw(0,0,0);
301 std::vector<double> ig;
302 std::vector<double> err;
304 double A=W.Norm()*0.1+0.01;
311 DW.T().Print(
"DW: ");
313 DW.T().Print(
"DW: ");
319 Pds::Octave::Plot::PointsX2DY(X,Pds::Sigmoid(R*W),
"testando.m",
"test_"+std::to_string(iter)+
".png");
323 before_error=last_error;
325 last_error = 1.0-Cost;
328 delta_error=fabs(FIR5.Evaluate(last_error)-before_error);
337 err.push_back(last_error);
343 }
while( (delta_error>MinError)&&(iter<MaxIter) );
345 if(Conf.
Show) print_iterative_IG_soft_data(iter,Alpha,delta_error,Cost,
true);
351 Pds::Ra::SaveStdVector(ig,
"ig.txt");
352 Pds::Ra::SaveStdVector(err,
"err.txt");
354 if(Conf.
Show) print_IterationConf_IG_soft_end_data(Conf,
"Gradient IG");
369 double average_distance_between_samples(
const Pds::Matrix &X)
371 if(X.IsEmpty())
return Pds::Ra::Nan;
373 Pds::Vector Min=X.MinInCols();
374 Pds::Vector Max=X.MaxInCols();
376 unsigned int N=X.Ncol();
377 unsigned int L=X.Nlin();
384 d=fabs(Max[n]-Min[n]);
388 return pow(S/L,1.0/N);
391 #include "extra_newhb.h"
403 Pds::Ra::Randomize();
412 unsigned int L=1000; Pds::Matrix X; Pds::Vector Y;
415 Pds::Octave::XLabel=
"x_1";
416 Pds::Octave::YLabel=
"x_2";
417 Pds::Octave::Plot::PointsX2DY(X,Y,
"testando.m",
"test_working_FittingGradientIGSoft.png");
442 Pds::Vector w1=Pds::LinSpace(-b,b,N);
443 Pds::Vector w2=Pds::LinSpace(-b,b,N);
446 Pds::Matrix IGsoft(N,N);
449 std::cout<<
"avr: "<<average_distance_between_samples(X)<<
"\n";
451 Pds::Meshgrid (w1,w2,W1,W2);
453 for(
unsigned int i=0;i<N;i++)
454 for(
unsigned int j=0;j<N;j++)
456 Pds::Vector w({0.5,w1[i],w2[j]});
460 IGsoft.SetRaw(i,j,val);
466 Pds::Octave::Plot::SurfC(W1,W2,(1-IG),
"testando.m",
"ig_matrix.png");
467 Pds::Octave::Plot::SurfC(W1,W2,IGsoft,
"testando_soft.m",
"ig_soft_matrix.png");
La clase tipo Pds::IterationConf . Esta clase genera una matriz de Nlin lineas y 1 columna....
bool SetLambda(double Lambda)
Coloca el valor lambda.
bool SetAlpha(double Alpha)
Coloca el valor alpha.
double GetMinError(void) const
Devuelve el valor MinError.
double GetAlpha(void) const
Devuelve el valor alpha.
bool SetMaxIter(unsigned int MaxIter)
Coloca el valor MaxIter.
bool SetMinError(double MinError)
Coloca el valor MinError.
bool SetGamma(double Lambda)
Coloca el valor gamma.
unsigned int GetMaxIter(void) const
Devuelve el valor MaxIter.
void LoadDataBand(unsigned int L, Pds::Matrix &X, Pds::Vector &Y)
Clasificacion de datos separados por mas de una curva.
Pds::Vector GetW0CornerMeanMethod(const Pds::Matrix &X, double Delta=0.001)
Obtiene de forma rapida un vector inicial para usar en regresion logistica.
double CostInformationGain(const Pds::Vector &W, const Pds::Matrix &X, const Pds::Vector &Y)
Calculo de costo.
Pds::Vector Classify(const Pds::Vector &W, const Pds::Matrix &X)
Calculo del resultado del clasificador.
double CostInformationGainSoft(const Pds::Vector &W, const Pds::Matrix &X, const Pds::Vector &Y)
Pds::Vector FittingGradientIGSoft(Pds::IterationConf &Conf, const Pds::Matrix &X, const Pds::Vector &Y, const Pds::Vector &W0)
Pds::Vector GradientCostInformationGainSoft2(const Pds::Vector &W, const Pds::Matrix &X, const Pds::Vector &Y)
double CostXqEntropy(const Pds::Vector &W, const Pds::Matrix &X, const Pds::Vector &Y, double q)
Pds::Vector GradientCostInformationGainSoft(const Pds::Vector &W, const Pds::Matrix &X, const Pds::Vector &Y, double h)
Nombre de espacio para Pds (Procesamiento Digital de Senales)