代码之家  ›  专栏  ›  技术社区  ›  Harikrishna

sagemaker中的Logistic回归

  •  2
  • Harikrishna  · 技术社区  · 7 年前

    我正在使用aws sagemaker进行逻辑回归。为了在测试数据上验证模型,使用了以下代码

    runtime= boto3.client('runtime.sagemaker')
    
    payload = np2csv(test_X)
    response = runtime.invoke_endpoint(EndpointName=linear_endpoint,
                                       ContentType='text/csv',
                                       Body=payload)
    result = json.loads(response['Body'].read().decode())
    test_pred = np.array([r['score'] for r in result['predictions']])
    

    我想知道如何运行预测模型,根据两个特定特征预测结果。我在模型中有30个特征,并使用这些特征训练了模型。现在,对于我的预测,我想知道当feature1='x'和feature2='y'时的结果。但是,当我将数据过滤到这些列并在同一代码中传递时,我得到了以下错误。

    Customer Error: The feature dimension of the input: 4 does not match the feature dimension of the model: 30. Please fix the input and try again.
    

    什么是glm的等价物。在AWS Sagemaker实现中预测R中的('feature1','feature2')?

    1 回复  |  直到 7 年前
        1
  •  5
  •   philipgautier    7 年前

    当您在数据上训练回归模型时,您正在学习从输入特征到响应变量的映射。然后,通过将新的输入特征提供给模型,使用该映射进行预测。

    如果你只想知道这两个特征如何影响预测,那么你可以查看训练模型的权重(也称为“参数”或“系数”)。如果特征1的权重为x,则当特征1增加1.0时,预测响应增加x。

    output sagemaker.estimator.Estimator 方法

    import os
    import mxnet as mx
    import boto3
    
    bucket = "<your_bucket>"
    key = "<your_model_prefix>"
    boto3.resource('s3').Bucket(bucket).download_file(key, 'model.tar.gz')
    
    os.system('tar -zxvf model.tar.gz')
    
    # Linear learner model is itself a zip file, containing a mxnet model and other metadata.
    # First unzip the model.
    os.system('unzip model_algo-1') 
    
    # Load the mxnet module
    mod = mx.module.Module.load("mx-mod", 0)
    
    # model weights
    weights = mod._arg_params['fc0_weight'].asnumpy().flatten()
    
    # model bias
    bias = mod._arg_params['fc0_bias'].asnumpy().flatten()
    
    # weight for the first feature
    weights[0]