本文整理汇总了C++中LinearRegression::Parameters方法的典型用法代码示例。如果您正苦于以下问题:C++ LinearRegression::Parameters方法的具体用法?C++ LinearRegression::Parameters怎么用?C++ LinearRegression::Parameters使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LinearRegression
的用法示例。
在下文中一共展示了LinearRegression::Parameters方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
//.........这里部分代码省略.........
else
{
Log::Fatal << "You must specify either --input_file or --model_file, not "
<< "both." << endl;
}
if (CLI::HasParam("test_file") && !CLI::HasParam("output_predictions"))
Log::Warn << "--test_file (-t) specified, but --output_predictions "
<< "(-o) is not; no results will be saved." << endl;
// If they specified a model file, we also need a test file or we
// have nothing to do.
if (!computeModel && !CLI::HasParam("test_file"))
{
Log::Fatal << "When specifying --model_file, you must also specify "
<< "--test_file." << endl;
}
if (!computeModel && CLI::HasParam("lambda"))
{
Log::Warn << "--lambda ignored because no model is being trained." << endl;
}
// An input file was given and we need to generate the model.
if (computeModel)
{
Timer::Start("load_regressors");
data::Load(trainFile, regressors, true);
Timer::Stop("load_regressors");
// Are the responses in a separate file?
if (CLI::HasParam("training_responses"))
{
// The initial predictors for y, Nx1.
responses = trans(regressors.row(regressors.n_rows - 1));
regressors.shed_row(regressors.n_rows - 1);
}
else
{
// The initial predictors for y, Nx1.
Timer::Start("load_responses");
data::Load(trainingResponsesFile, responses, true);
Timer::Stop("load_responses");
if (responses.n_rows == 1)
responses = trans(responses); // Probably loaded backwards.
if (responses.n_cols > 1)
Log::Fatal << "The responses must have one column.\n";
if (responses.n_rows != regressors.n_cols)
Log::Fatal << "The responses must have the same number of rows as the "
"training file.\n";
}
Timer::Start("regression");
lr = LinearRegression(regressors, responses.unsafe_col(0));
Timer::Stop("regression");
// Save the parameters.
if (CLI::HasParam("output_model_file"))
data::Save(outputModelFile, "linearRegressionModel", lr);
}
// Did we want to predict, too?
if (CLI::HasParam("test_file"))
{
// A model file was passed in, so load it.
if (!computeModel)
{
Timer::Start("load_model");
data::Load(inputModelFile, "linearRegressionModel", lr, true);
Timer::Stop("load_model");
}
// Load the test file data.
arma::mat points;
Timer::Start("load_test_points");
data::Load(testFile, points, true);
Timer::Stop("load_test_points");
// Ensure that test file data has the right number of features.
if ((lr.Parameters().n_elem - 1) != points.n_rows)
{
Log::Fatal << "The model was trained on " << lr.Parameters().n_elem - 1
<< "-dimensional data, but the test points in '" << testFile
<< "' are " << points.n_rows << "-dimensional!" << endl;
}
// Perform the predictions using our model.
arma::vec predictions;
Timer::Start("prediction");
lr.Predict(points, predictions);
Timer::Stop("prediction");
// Save predictions.
if (CLI::HasParam("output_predictions"))
data::Save(outputPredictionsFile, predictions, true, false);
}
}
示例2: mlpackMain
static void mlpackMain()
{
const double lambda = CLI::GetParam<double>("lambda");
RequireOnlyOnePassed({ "training", "input_model" }, true);
ReportIgnoredParam({{ "test", true }}, "output_predictions");
mat regressors;
rowvec responses;
LinearRegression lr;
const bool computeModel = !CLI::HasParam("input_model");
const bool computePrediction = CLI::HasParam("test");
// If they specified a model file, we also need a test file or we
// have nothing to do.
if (!computeModel)
{
RequireAtLeastOnePassed({ "test" }, true, "test points must be specified "
"when an input model is given");
}
ReportIgnoredParam({{ "input_model", true }}, "lambda");
RequireAtLeastOnePassed({ "output_model", "output_predictions" }, false,
"no output will be saved");
// An input file was given and we need to generate the model.
if (computeModel)
{
Timer::Start("load_regressors");
regressors = std::move(CLI::GetParam<mat>("training"));
Timer::Stop("load_regressors");
// Are the responses in a separate file?
if (!CLI::HasParam("training_responses"))
{
// The initial predictors for y, Nx1.
if (regressors.n_rows < 2)
{
Log::Fatal << "Can't get responses from training data "
"since it has less than 2 rows." << endl;
}
responses = regressors.row(regressors.n_rows - 1);
regressors.shed_row(regressors.n_rows - 1);
}
else
{
// The initial predictors for y, Nx1.
Timer::Start("load_responses");
responses = CLI::GetParam<rowvec>("training_responses");
Timer::Stop("load_responses");
if (responses.n_cols != regressors.n_cols)
{
Log::Fatal << "The responses must have the same number of columns "
"as the training set." << endl;
}
}
Timer::Start("regression");
lr = LinearRegression(regressors, responses, lambda);
Timer::Stop("regression");
}
else
{
// A model file was passed in, so load it.
Timer::Start("load_model");
lr = std::move(CLI::GetParam<LinearRegression>("input_model"));
Timer::Stop("load_model");
}
// Did we want to predict, too?
if (computePrediction)
{
// Load the test file data.
Timer::Start("load_test_points");
mat points = std::move(CLI::GetParam<mat>("test"));
Timer::Stop("load_test_points");
// Ensure that test file data has the right number of features.
if ((lr.Parameters().n_elem - 1) != points.n_rows)
{
Log::Fatal << "The model was trained on " << lr.Parameters().n_elem - 1
<< "-dimensional data, but the test points in '"
<< CLI::GetPrintableParam<mat>("test") << "' are " << points.n_rows
<< "-dimensional!" << endl;
}
// Perform the predictions using our model.
rowvec predictions;
Timer::Start("prediction");
lr.Predict(points, predictions);
Timer::Stop("prediction");
// Save predictions.
if (CLI::HasParam("output_predictions"))
CLI::GetParam<rowvec>("output_predictions") = std::move(predictions);
//.........这里部分代码省略.........