Class MrsCake::Model
In: mrscake.rb.c
Parent: Object

A Model can be used to predict values from (so far unknown) input data. Models are "lightweight", in that they don‘t store any data other than that needed to do predictions (in particular, they don‘t contain any explicit training data)

Methods

generate_code   predict   print   save  

Public Instance methods

Generate code for this model. Supported languages are: "ruby", "c", "javascript" and "python".

[Source]

/* call-seq:
 *   model.generate_code(language) -> string
 *
 * Generate code for this model. Supported languages are: "ruby", "c", "javascript" and "python".
 */
static VALUE rb_model_generate_code(VALUE cls, VALUE _language)
{
    Check_Type(_language, T_STRING);
    const char*language = StringValuePtr(_language);
    Get_Model(model,cls);
    char*code = model_generate_code(model->model, language);
    return rb_str_new2(code);
}

Use the model to classify a set of features.

[Source]

/* call-seq:
 *   model.predict({feature1=>value1,feature2=>value2}) -> prediction
 *
 * Use the model to classify a set of features.
 */
static VALUE rb_model_predict(VALUE cls, VALUE input)
{
    Get_Model(model,cls);
    if(TYPE(input) != T_ARRAY && TYPE(input) != T_HASH) {
        rb_raise(rb_eArgError, "First argument to predict() must be an array or a hash");
    }

    example_t*e = value_to_example(input);

    if(e->num_inputs != model->model->sig->num_inputs)
        rb_raise(rb_eArgError, "You supplied %d inputs for a model with %d inputs", e->num_inputs, model->model->sig->num_inputs);

    row_t*row = example_to_row(e, model->model->sig->column_names);

    variable_t prediction = model_predict(model->model, row);
    row_destroy(row);

    if(prediction.type == CONTINUOUS)
        return rb_float_new(prediction.value);
    else if(prediction.type == CATEGORICAL)
        return INT2FIX(prediction.category);
    else if(prediction.type == TEXT)
        return rb_str_new2(prediction.text);
    else
        return T_NIL;
}

Print the model to stdout.

[Source]

/* call-seq:
 *   model.print()
 *
 * Print the model to stdout.
 */
static VALUE rb_model_print(VALUE cls)
{
    Get_Model(model,cls);
    model_print(model->model);
    return cls;
}

Save the model to a file, using mrscake‘s internal file format.

[Source]

/* call-seq:
 *   model.save()
 *
 * Save the model to a file, using mrscake's internal file format.
 */
static VALUE rb_model_save(VALUE cls, VALUE _filename)
{
    Check_Type(_filename, T_STRING);
    const char*filename = StringValuePtr(_filename);
    Get_Model(model,cls);
    model_save(model->model, filename);
    return cls;
}

[Validate]