Esempio n. 1
0
def model_train(model_id, features, target):
    result = {}
    model_path = os.path.join(config.model_dir, model_id + '.joblib')
    model = ml_models.load_model(model_path)

    #x_train, x_test, y_train, y_test = train_test_split(features, target)
    x_train, y_train = features, target
    x_test, y_test = features, target
    model.fit(x_train, y_train)
    y_pred = model.predict(x_test)

    result["mean_squared_error"] = metrics.mean_squared_error(y_test, y_pred)
    result["mean_absolute_error"] = metrics.mean_absolute_error(y_test, y_pred)

    try:
        train_loss = model.named_steps['model'].lpath['train']
        val_loss = model.named_steps['model'].lpath['val']
    except Exception:
        train_loss = []
        val_loss = []

    result["train_loss"] = train_loss
    result["val_loss"] = val_loss
    ml_models.save_model(model, model_path)

    return result
Esempio n. 2
0
def model_create(model_id, model_type, parameters):
    model_path = os.path.join(config.model_dir, model_id + '.joblib')

    # get definition validator
    validatorDefinition = ml_models.get_validator(model_type)
    # fit value
    validator = validatorDefinition(**parameters)
    # validate
    params = validator()
    model = ml_models.build_model(model_type, params)

    ml_models.save_model(model, model_path)
Esempio n. 3
0
def model_train(model_id, features, target):
    result = {}
    model_path = os.path.join(config.model_dir, model_id + '.joblib')
    model = ml_models.load_model(model_path)

    #x_train, x_test, y_train, y_test = train_test_split(features, target)
    x_train, y_train = features, target
    x_test, y_test = features, target
    model.fit(x_train, y_train)
    y_pred = model.predict(x_test)
    result["mean_squared_error"] = metrics.mean_squared_error(y_test, y_pred)
    result["mean_absolute_error"] = metrics.mean_absolute_error(y_test, y_pred)
    ml_models.save_model(model, model_path)

    return result
Esempio n. 4
0
def model_train(model_id, features, target, **kwags):
    
    result = {}
    model_path = os.path.join(config.model_dir, model_id+'.joblib')
    model = ml_models.load_model(model_path)
    x_train = features
    y_train = target
    model.fit(x_train, y_train, **kwags)

    y_pred = model.predict(x_train)
    cm = metrics.confusion_matrix(y_train, y_pred)
    true_pred = 0
    total = 0
    for i in range (len(cm)):
        true_pred += cm[i][i]
        total += sum(cm[i])
    result["accuracy"] = round(true_pred / total, 4)
    ml_models.save_model(model, model_path)
    return result