예제 #1
0
 def invoke():
     try:
         req_msg = Message(request.json)
         result = f(req_msg)
         if isinstance(result, Message):
             return jsonify(result.to_params())
         return jsonify(Message({"payload": result}).to_params())
     except Exception as e:
         error = {"success": False, "error": str(e)}
         app.logger.error(str(e))
         traceback.print_exc()
         return jsonify(Message({"payload": error}).to_params())
def test_predict_1():
    # Run a single instance through predict
    df = pd.read_csv('./test/1-instance.csv', sep=';')

    # Use only the columns the trained model expects (drops the outcome column)
    x = df.iloc[:, :-1]

    # Convert the outcome column to 0 and 1
    y = df['y'].copy()
    y.loc[y == 'no'] = 0
    y.loc[y == 'yes'] = 1

    instances = list(x.values)
    predictions = predict_rfc(Message({'payload': {
        'instances': instances
    }}), {})
    print(f'Predictions: {predictions}')

    # Check that our model made a correct prediction
    assert y[0] == predictions['predictions'][0]
def test_predict_3():
    instances = [[
        58, "management", "married", "tertiary", "no", 2143, "yes", "no",
        "unknown", 5, "may", 261, 1, -1, 0, "unknown"
    ],
                 [
                     51.0, "technician", "married", "primary", "no", -459,
                     "yes", "yes", "cellular", 5, "may", 261, 1, -1, 0,
                     "unknown"
                 ],
                 [
                     41, "technician", "married", "secondary", "no", 1270,
                     "yes", "no", "unknown", 5, "may", 1389, 1, -1, 0,
                     "unknown"
                 ]]

    y = [0, 0, 1]

    response = predict_rfc(Message({'payload': {'instances': instances}}), {})
    predictions = response['predictions']
    print(f'Predictions: {predictions}')

    # Check that our model made the correct predictions
    assert y == predictions
예제 #4
0
    lmodel_l2 = Ridge(
        alpha=0.1,
        copy_X=True,
        fit_intercept=True,
        max_iter=1000,
        normalize=False,
        random_state=RANDOM_SEED,
        tol=0.0001,
    )
    lmodel_l2.fit(X_train, y_train)
    y_pred = lmodel_l2.predict(X_test)
    l2_err = ((y_test - y_pred)**2).sum()  # Prediction error

    err = r2_score(y_test, y_pred)

    model_binary = f"models/{save_model_as}.pkl"
    pickle_model(
        lmodel_l2,
        scaler,
        "Linear L2",
        err,
        "Linear regression with L2 regularization",
        model_binary,
    )
    print(err)
    return f"model: {model_binary}"


if __name__ == "__main__":
    print(train(Message(json.loads(sys.argv[1]))))
    """
    this function is called when api is invoked. loads the model,scaler and data instances.
    calls sklearn `model.predict` internally and returns list of prediction wrapped in python-dict object
    model_ctx: Dict: (global) loads model once and holds in memory till web service is running
    Args:
        msg: cortex message object

    Returns: dict containing list of in-order predictions

    """
    global model_name, std_scaler
    instances = msg.payload.get("instances", [])
    if model_name not in model_ctx:
        model_ctx[model_name], std_scaler = _train_sample_model()
    instances = np.array(instances, dtype=object)
    instances = instances if instances.ndim == 2 else np.reshape(
        instances, (1, -1))
    predictions = model_ctx[model_name].predict(
        std_scaler.transform(instances))
    return {"predictions": predictions.tolist()}


if __name__ == '__main__':
    """
    [optional]:
        when running as python script
        'pip install -r requirements_local.txt` to install local dependencies
    """
    test_set = _create_api_test_data('encoded_dataset.csv')
    print(predict_titanic_survivor(Message(test_set)))
예제 #6
0
    log.info(f'Instances: {instances[0:5]}')

    df = pd.DataFrame(columns=pipeline.get_context('columns'), data=instances)

    # Prepare model frame for predition using the training pipeline
    df = pipeline.run(df)

    # Use the same scaler transform used on the training data
    scaler = pipeline.get_context('scaler')

    x = scaler.transform(df)
    y = clf.predict(x)

    return {'predictions': json.loads(pd.Series(y).to_json(orient='values'))}


def predict_rfc(msg: Message, model_context: dict) -> dict:
    return do_predict(msg, rfc)


def predict_dt(msg: Message, model_context: dict) -> dict:
    return do_predict(msg, dt)


if __name__ == "__main__":
    with open('./test/2-instances.json') as f:
        test_json = json.load(f)
    msg = Message(test_json)
    result = predict_rfc(msg)
    print(result)
예제 #7
0
    rfc = RandomForestClassifier(n_estimators=n_estimators,
                                 n_jobs=6,
                                 random_state=12)  #criterion = entopy,gini
    train_classifier(exp,
                     X_train,
                     y_train,
                     X_test,
                     y_test,
                     k_fold,
                     rfc,
                     n_estimators=n_estimators,
                     type='RandomForest')

    dt = DecisionTreeClassifier()
    train_classifier(exp,
                     X_train,
                     y_train,
                     X_test,
                     y_test,
                     k_fold,
                     dt,
                     type='DecisionTree')

    ds.save()

    return {'experiment': exp.name}


if __name__ == "__main__":
    train_local(Message({'payload': {'$ref': './data/bank-full.csv'}}))