本文整理汇总了C#中Encog.MathUtil.LIBSVM.svm_problem类的典型用法代码示例。如果您正苦于以下问题:C# svm_problem类的具体用法?C# svm_problem怎么用?C# svm_problem使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
svm_problem类属于Encog.MathUtil.LIBSVM命名空间,在下文中一共展示了svm_problem类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Encode
/// <summary>
/// Encode the Encog dataset.
/// </summary>
/// <param name="training">The training data.</param>
/// <param name="outputIndex">The ideal element to use, this is necessary becase SVM's have
/// only a single output.</param>
/// <returns>The SVM problem.</returns>
public static svm_problem Encode(INeuralDataSet training, int outputIndex)
{
svm_problem result = new svm_problem();
result.l = (int)ObtainTrainingLength(training);
result.y = new double[result.l];
result.x = new svm_node[result.l][];
int elementIndex = 0;
foreach (INeuralDataPair pair in training)
{
INeuralData input = pair.Input;
INeuralData output = pair.Ideal;
result.x[elementIndex] = new svm_node[input.Count];
for (int i = 0; i < input.Count; i++)
{
result.x[elementIndex][i] = new svm_node();
result.x[elementIndex][i].index = i + 1;
result.x[elementIndex][i].value_Renamed = input[i];
}
result.y[elementIndex] = output[outputIndex];
elementIndex++;
}
return result;
}
示例2: xa68541a68dd6f460
internal xa68541a68dd6f460(svm_problem prob, svm_parameter param, sbyte[] y_)
: base(prob.l, prob.x, param)
{
this.x1e218ceaee1bb583 = new sbyte[y_.Length];
y_.CopyTo(this.x1e218ceaee1bb583, 0);
this.x1f31bf6ca58166a1 = new xb730a77005d16cc1(prob.l, (int) (param.cache_size * 1048576.0));
}
示例3: xfbfe48e5ee40f893
internal xfbfe48e5ee40f893(svm_problem prob, svm_parameter param)
: base(prob.l, prob.x, param)
{
int num;
int num2;
if (((uint) num) > uint.MaxValue)
{
goto Label_005C;
}
this.x9fc3ee03a439f6f0 = prob.l;
if ((((uint) num) - ((uint) num2)) > uint.MaxValue)
{
if ((((uint) num2) + ((uint) num)) > uint.MaxValue)
{
goto Label_0115;
}
}
else
{
goto Label_0115;
}
Label_0024:
this.x5cafa8d49ea71ea1[num2] = new float[2 * this.x9fc3ee03a439f6f0];
num2++;
Label_003D:
if (num2 < 2)
{
goto Label_0024;
}
this.xafb0a999075e2e6a = 0;
if (0 == 0)
{
return;
}
goto Label_00C0;
Label_005C:
num2 = 0;
goto Label_003D;
Label_00C0:
this.xc0c4c459c6ccbd00 = new int[2 * this.x9fc3ee03a439f6f0];
for (num = 0; num < this.x9fc3ee03a439f6f0; num++)
{
this.x32dce50116aa0f1e[num] = 1;
this.x32dce50116aa0f1e[num + this.x9fc3ee03a439f6f0] = -1;
this.xc0c4c459c6ccbd00[num] = num;
this.xc0c4c459c6ccbd00[num + this.x9fc3ee03a439f6f0] = num;
}
this.x5cafa8d49ea71ea1 = new float[2][];
goto Label_005C;
Label_0115:
this.x1f31bf6ca58166a1 = new xb730a77005d16cc1(this.x9fc3ee03a439f6f0, (int) (param.cache_size * 1048576.0));
this.x32dce50116aa0f1e = new sbyte[2 * this.x9fc3ee03a439f6f0];
goto Label_00C0;
}
示例4: Encode
/// <summary>
/// Encode the Encog dataset.
/// </summary>
///
/// <param name="training">The training data.</param>
/// <param name="outputIndex"></param>
/// <returns>The SVM problem.</returns>
public static svm_problem Encode(IMLDataSet training,
int outputIndex)
{
try
{
var result = new svm_problem {l = (int) training.Count};
result.y = new double[result.l];
result.x = new svm_node[result.l][];
for (int i = 0; i < result.l; i++)
{
result.x[i] = new svm_node[training.InputSize];
}
int elementIndex = 0;
foreach (IMLDataPair pair in training)
{
IMLData input = pair.Input;
IMLData output = pair.Ideal;
result.x[elementIndex] = new svm_node[input.Count];
for (int i = 0; i < input.Count; i++)
{
result.x[elementIndex][i] = new svm_node {index = i + 1, value_Renamed = input[i]};
}
result.y[elementIndex] = output[outputIndex];
elementIndex++;
}
return result;
}
catch (OutOfMemoryException )
{
throw new EncogError("SVM Model - Out of Memory");
}
}
示例5: solve_one_class
private static void solve_one_class(svm_problem prob, svm_parameter param, double[] alpha,
Solver.SolutionInfo si)
{
int l = prob.l;
var zeros = new double[l];
var ones = new sbyte[l];
int i;
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042_3"'
var n = (int) (param.nu*prob.l); // # of alpha's at upper bound
for (i = 0; i < n; i++)
alpha[i] = 1;
alpha[n] = param.nu*prob.l - n;
for (i = n + 1; i < l; i++)
alpha[i] = 0;
for (i = 0; i < l; i++)
{
zeros[i] = 0;
ones[i] = 1;
}
var s = new Solver();
s.Solve(l, new ONE_CLASS_Q(prob, param), zeros, ones, alpha, 1.0, 1.0, param.eps, si, param.shrinking);
}
示例6: solve_nu_svc
private static void solve_nu_svc(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si)
{
int i;
int l = prob.l;
double nu = param.nu;
var y = new sbyte[l];
for (i = 0; i < l; i++)
if (prob.y[i] > 0)
y[i] = (+ 1);
else
y[i] = - 1;
double sum_pos = nu*l/2;
double sum_neg = nu*l/2;
for (i = 0; i < l; i++)
if (y[i] == + 1)
{
alpha[i] = Math.Min(1.0, sum_pos);
sum_pos -= alpha[i];
}
else
{
alpha[i] = Math.Min(1.0, sum_neg);
sum_neg -= alpha[i];
}
var zeros = new double[l];
for (i = 0; i < l; i++)
zeros[i] = 0;
var s = new Solver_NU();
s.Solve(l, new SVC_Q(prob, param, y), zeros, y, alpha, 1.0, 1.0, param.eps, si, param.shrinking);
double r = si.r;
Console.Out.Write("C = " + 1/r + "\n");
for (i = 0; i < l; i++)
alpha[i] *= y[i]/r;
si.rho /= r;
si.obj /= (r*r);
si.upper_bound_p = 1/r;
si.upper_bound_n = 1/r;
}
示例7: solve_c_svc
//
// construct and solve various formulations
//
private static void solve_c_svc(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si,
double Cp, double Cn)
{
int l = prob.l;
var minus_ones = new double[l];
var y = new sbyte[l];
int i;
for (i = 0; i < l; i++)
{
alpha[i] = 0;
minus_ones[i] = - 1;
if (prob.y[i] > 0)
y[i] = (+ 1);
else
y[i] = - 1;
}
var s = new Solver();
s.Solve(l, new SVC_Q(prob, param, y), minus_ones, y, alpha, Cp, Cn, param.eps, si, param.shrinking);
double sum_alpha = 0;
for (i = 0; i < l; i++)
sum_alpha += alpha[i];
if (Cp == Cn)
Console.Out.Write("nu = " + sum_alpha/(Cp*prob.l) + "\n");
for (i = 0; i < l; i++)
alpha[i] *= y[i];
}
示例8: SVR_Q
internal SVR_Q(svm_problem prob, svm_parameter param) : base(prob.l, prob.x, param)
{
l = prob.l;
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042_3"'
cache = new Cache(l, (int) (param.cache_size*(1 << 20)));
sign = new sbyte[2*l];
index = new int[2*l];
for (int k = 0; k < l; k++)
{
sign[k] = 1;
sign[k + l] = - 1;
index[k] = k;
index[k + l] = k;
}
buffer = new float[2][];
for (int i = 0; i < 2; i++)
{
buffer[i] = new float[2*l];
}
next_buffer = 0;
}
示例9: Encode
public static svm_problem Encode(IMLDataSet training, int outputIndex)
{
svm_problem _problem3;
try
{
svm_problem _problem;
int num;
int num2;
svm_problem _problem2 = new svm_problem();
goto Label_0158;
Label_000C:
if (1 == 0)
{
return _problem3;
}
if ((((uint) num2) + ((uint) num2)) < 0)
{
goto Label_018B;
}
Label_0031:
if (num >= _problem.l)
{
num2 = 0;
using (IEnumerator<IMLDataPair> enumerator = training.GetEnumerator())
{
IMLDataPair pair;
IMLData input;
IMLData data2;
int num3;
svm_node _node;
goto Label_0083;
Label_0049:
num3++;
Label_004F:
if (num3 < input.Count)
{
goto Label_00CA;
}
_problem.y[num2] = data2[outputIndex];
if (((uint) outputIndex) < 0)
{
return _problem;
}
num2++;
Label_0083:
if (enumerator.MoveNext())
{
goto Label_0100;
}
return _problem;
Label_008E:
data2 = pair.Ideal;
if ((((uint) num3) + ((uint) num2)) >= 0)
{
_problem.x[num2] = new svm_node[input.Count];
num3 = 0;
goto Label_004F;
}
Label_00CA:
_node = new svm_node();
_node.index = num3 + 1;
_node.value_Renamed = input[num3];
_problem.x[num2][num3] = _node;
goto Label_0049;
Label_0100:
pair = enumerator.Current;
input = pair.Input;
goto Label_008E;
}
}
_problem.x[num] = new svm_node[training.InputSize];
num++;
if ((((uint) outputIndex) & 0) == 0)
{
goto Label_000C;
}
return _problem3;
Label_0158:
_problem2.l = (int) training.Count;
_problem = _problem2;
_problem.y = new double[_problem.l];
_problem.x = new svm_node[_problem.l][];
Label_018B:
num = 0;
goto Label_0031;
}
catch (OutOfMemoryException)
{
throw new EncogError("SVM Model - Out of Memory");
}
return _problem3;
}
示例10: svm_cross_validation
public static void svm_cross_validation(svm_problem prob, svm_parameter param, int nr_fold, double[] target)
{
int i;
var perm = new int[prob.l];
// random shuffle
for (i = 0; i < prob.l; i++)
perm[i] = i;
for (i = 0; i < prob.l; i++)
{
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042_3"'
int j = i + (int) (SupportClass.Random.NextDouble()*(prob.l - i));
do
{
int _ = perm[i];
perm[i] = perm[j];
perm[j] = _;
} while (false);
}
for (i = 0; i < nr_fold; i++)
{
int begin = i*prob.l/nr_fold;
int end = (i + 1)*prob.l/nr_fold;
int j, k;
var subprob = new svm_problem();
subprob.l = prob.l - (end - begin);
subprob.x = new svm_node[subprob.l][];
subprob.y = new double[subprob.l];
k = 0;
for (j = 0; j < begin; j++)
{
subprob.x[k] = prob.x[perm[j]];
subprob.y[k] = prob.y[perm[j]];
++k;
}
for (j = end; j < prob.l; j++)
{
subprob.x[k] = prob.x[perm[j]];
subprob.y[k] = prob.y[perm[j]];
++k;
}
svm_model submodel = svm_train(subprob, param);
if (param.probability == 1 &&
(param.svm_type == svm_parameter.C_SVC || param.svm_type == svm_parameter.NU_SVC))
{
var prob_estimates = new double[svm_get_nr_class(submodel)];
for (j = begin; j < end; j++)
target[perm[j]] = svm_predict_probability(submodel, prob.x[perm[j]], prob_estimates);
}
else
for (j = begin; j < end; j++)
target[perm[j]] = svm_predict(submodel, prob.x[perm[j]]);
}
}
示例11: svm_train_one
internal static decision_function svm_train_one(svm_problem prob, svm_parameter param, double Cp, double Cn)
{
var alpha = new double[prob.l];
var si = new Solver.SolutionInfo();
switch (param.svm_type)
{
case svm_parameter.C_SVC:
solve_c_svc(prob, param, alpha, si, Cp, Cn);
break;
case svm_parameter.NU_SVC:
solve_nu_svc(prob, param, alpha, si);
break;
case svm_parameter.ONE_CLASS:
solve_one_class(prob, param, alpha, si);
break;
case svm_parameter.EPSILON_SVR:
solve_epsilon_svr(prob, param, alpha, si);
break;
case svm_parameter.NU_SVR:
solve_nu_svr(prob, param, alpha, si);
break;
}
Console.Out.Write("obj = " + si.obj + ", rho = " + si.rho + "\n");
// output SVs
int nSV = 0;
int nBSV = 0;
for (int i = 0; i < prob.l; i++)
{
if (Math.Abs(alpha[i]) > 0)
{
++nSV;
if (prob.y[i] > 0)
{
if (Math.Abs(alpha[i]) >= si.upper_bound_p)
++nBSV;
}
else
{
if (Math.Abs(alpha[i]) >= si.upper_bound_n)
++nBSV;
}
}
}
Console.Out.Write("nSV = " + nSV + ", nBSV = " + nBSV + "\n");
var f = new decision_function();
f.alpha = alpha;
f.rho = si.rho;
return f;
}
示例12: Evaluate
/// <summary>
/// Evaluate the error for the specified model.
/// </summary>
///
/// <param name="param">The params for the SVN.</param>
/// <param name="prob">The problem to evaluate.</param>
/// <param name="target">The output values from the SVN.</param>
/// <returns>The calculated error.</returns>
private static double Evaluate(svm_parameter param, svm_problem prob,
double[] target)
{
int totalCorrect = 0;
var error = new ErrorCalculation();
if ((param.svm_type == svm_parameter.EPSILON_SVR)
|| (param.svm_type == svm_parameter.NU_SVR))
{
for (int i = 0; i < prob.l; i++)
{
double ideal = prob.y[i];
double actual = target[i];
error.UpdateError(actual, ideal);
}
return error.Calculate();
}
for (int i = 0; i < prob.l; i++)
{
if (target[i] == prob.y[i])
{
++totalCorrect;
}
}
return Format.HundredPercent*totalCorrect/prob.l;
}
示例13: x308cb2f3483de2a6
private static double x308cb2f3483de2a6(svm_parameter x0d173b5435b4d6ad, svm_problem xdee3898b83df48b4, double[] x11d58b056c032b03)
{
ErrorCalculation calculation;
int num2;
double num3;
int num5;
int num = 0;
if (0 == 0)
{
if ((((uint) num3) & 0) != 0)
{
goto Label_0134;
}
goto Label_0108;
}
goto Label_008C;
Label_0055:
if (num2 >= xdee3898b83df48b4.l)
{
return calculation.Calculate();
}
Label_008C:
num3 = xdee3898b83df48b4.y[num2];
double actual = x11d58b056c032b03[num2];
if (((uint) num3) < 0)
{
goto Label_0108;
}
calculation.UpdateError(actual, num3);
num2++;
if ((((uint) num3) - ((uint) num3)) >= 0)
{
goto Label_0055;
}
Label_00D4:
if ((((uint) num5) & 0) == 0)
{
while (num5 < xdee3898b83df48b4.l)
{
while (x11d58b056c032b03[num5] == xdee3898b83df48b4.y[num5])
{
num++;
if ((((uint) actual) + ((uint) actual)) <= uint.MaxValue)
{
break;
}
}
num5++;
}
}
goto Label_0134;
Label_0108:
calculation = new ErrorCalculation();
if (((x0d173b5435b4d6ad.svm_type != 3) && ((((uint) num5) | 3) != 0)) && (x0d173b5435b4d6ad.svm_type != 4))
{
num5 = 0;
if ((((uint) num3) + ((uint) actual)) <= uint.MaxValue)
{
goto Label_00D4;
}
goto Label_008C;
}
num2 = 0;
goto Label_0055;
Label_0134:
return ((100.0 * num) / ((double) xdee3898b83df48b4.l));
}
示例14: solve_epsilon_svr
private static void solve_epsilon_svr(svm_problem prob, svm_parameter param, double[] alpha,
Solver.SolutionInfo si)
{
int l = prob.l;
var alpha2 = new double[2*l];
var linear_term = new double[2*l];
var y = new sbyte[2*l];
int i;
for (i = 0; i < l; i++)
{
alpha2[i] = 0;
linear_term[i] = param.p - prob.y[i];
y[i] = 1;
alpha2[i + l] = 0;
linear_term[i + l] = param.p + prob.y[i];
y[i + l] = - 1;
}
var s = new Solver();
s.Solve(2*l, new SVR_Q(prob, param), linear_term, y, alpha2, param.C, param.C, param.eps, si,
param.shrinking);
double sum_alpha = 0;
for (i = 0; i < l; i++)
{
alpha[i] = alpha2[i] - alpha2[i + l];
sum_alpha += Math.Abs(alpha[i]);
}
Console.Out.Write("nu = " + sum_alpha/(param.C*l) + "\n");
}
示例15: svm_svr_probability
// Return parameter of a Laplace distribution
private static double svm_svr_probability(svm_problem prob, svm_parameter param)
{
int i;
int nr_fold = 5;
var ymv = new double[prob.l];
double mae = 0;
var newparam = (svm_parameter) param.Clone();
newparam.probability = 0;
svm_cross_validation(prob, newparam, nr_fold, ymv);
for (i = 0; i < prob.l; i++)
{
ymv[i] = prob.y[i] - ymv[i];
mae += Math.Abs(ymv[i]);
}
mae /= prob.l;
double std = Math.Sqrt(2*mae*mae);
int count = 0;
mae = 0;
for (i = 0; i < prob.l; i++)
if (Math.Abs(ymv[i]) > 5*std)
count = count + 1;
else
mae += Math.Abs(ymv[i]);
mae /= (prob.l - count);
Console.Error.Write(
"Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=" +
mae + "\n");
return mae;
}