首页 文章

sagemaker中的逻辑回归

提问于
浏览
2

我正在使用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.

在AWS Sagemaker实现中,R中的glm.predict('feature1','feature2')相当于什么?

1 回答

  • 4

    当您在数据上训练回归模型时,您将学习从输入要素到响应变量的映射 . 然后,您可以使用该映射通过向模型提供新的输入要素来进行预测 .

    如果您在30个特征上训练了模型,则不可能使用相同的模型仅使用2个特征进行预测 . 您必须为其他28个功能提供值 .

    如果您只想知道这两个特征如何影响预测,那么您可以查看训练模型的权重(a.k.a . '参数'或'系数') . 如果特征1的权重是x,则当特征1增加1.0时,预测响应增加x .

    要查看使用Amazon SageMaker中的线性学习器算法训练的模型的权重,您可以下载model.tar.gz工件并在本地打开它 . 模型工件可以从您在 output 参数中指定的S3位置下载到 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]
    

相关问题