public Generate ( Matrix X, |
||
X | Matrix | The Matrix to process. |
y | The Vector to process. | |
Résultat | IModel |
public override IModel Generate(Matrix X, Vector y)
{
X = IncreaseDimensions(X, this.PolynomialFeatures);
this.Preprocess(X);
// guarantee 1/0 based label vector
y = y.ToBinary(f => f == 1d, falseValue: 0d);
// add intercept term
X = X.Insert(Vector.Ones(X.Rows), 0, VectorType.Col, false);
Vector theta = Vector.Rand(X.Cols);
// run gradient descent
var optimizer = new numl.Math.Optimization.Optimizer(theta, this.MaxIterations, this.LearningRate)
{
CostFunction = new numl.Math.Functions.Cost.LogisticCostFunction()
{
X = X,
Y = y,
Lambda = this.Lambda,
Regularizer = new numl.Math.Functions.Regularization.L2Regularizer(),
LogisticFunction = this.LogisticFunction
}
};
optimizer.Run();
LogisticRegressionModel model = new LogisticRegressionModel()
{
Descriptor = this.Descriptor,
NormalizeFeatures = base.NormalizeFeatures,
FeatureNormalizer = base.FeatureNormalizer,
FeatureProperties = base.FeatureProperties,
Theta = optimizer.Properties.Theta,
LogisticFunction = this.LogisticFunction,
PolynomialFeatures = this.PolynomialFeatures
};
return model;
}
public void Save_And_Load_LogisticRegression() { Matrix m = new[,] { { 0.0512670, 0.6995600 }, { -0.0927420, 0.6849400 }, { -0.2137100, 0.6922500 }, { -0.3750000, 0.5021900 }, { -0.5132500, 0.4656400 }, { -0.5247700, 0.2098000 }, { -0.3980400, 0.0343570 }, { -0.3058800, -0.1922500 }, { 0.0167050, -0.4042400 }, { 0.1319100, -0.5138900 }, { -0.6111800, -0.0679820 }, { -0.6630200, -0.2141800 }, { -0.5996500, -0.4188600 }, { -0.7263800, -0.0826020 }, { -0.8300700, 0.3121300 }, { -0.7206200, 0.5387400 }, { -0.5938900, 0.4948800 }, { -0.4844500, 0.9992700 }, { -0.0063364, 0.9992700 }, { 0.6326500, -0.0306120 }, }; Vector y = new Vector(new double[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); var generator = new LogisticRegressionGenerator() { Lambda = 1, LearningRate = 0.01, PolynomialFeatures = 6, MaxIterations = 400 }; var model = generator.Generate(m, y) as LogisticRegressionModel; Serialize(model); var lmodel = Deserialize<LogisticRegressionModel>(); Assert.AreEqual(model.Theta, lmodel.Theta); Assert.AreEqual(model.PolynomialFeatures, lmodel.PolynomialFeatures); Assert.AreEqual(model.LogisticFunction.GetType(), lmodel.LogisticFunction.GetType()); }