智者的逻辑回归 [英] Logistic regression in sagemaker

查看:94
本文介绍了智者的逻辑回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the aws sagemaker for logistic regression. For validating the model on test data, the following code is used

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'时的结果.但是,当我将数据过滤到那些列并以相同的代码传递它们时,会出现以下错误.

The result contains the prediction values and the probability scores. I want to know how I can run a prediction model to predict the outcome based on two specific features. Eg. I have 30 features in the model and have trained model using those features. Now for my prediction, I want to know the outcome when feature1='x' and feature2='y'. But when I filter the data to those columns and pass that in the same code, I get the following error.

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')相当于什么?

What is the equivalent of say glm.predict('feature1','feature2')in R in AWS Sagemaker implementation?

推荐答案

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

When you train a regression model on data, you're learning a mapping from the input features to the response variable. You then use that mapping to make predictions by feeding new input features to the model.

如果您针对30种功能训练了模型,则无法仅使用其中两个功能就使用同一模型进行预测.您必须提供其他28个功能的值.

If you trained a model on 30 features, it's not possible to use that same model to predict with only 2 of the features. You would have to supply values for the other 28 features.

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

If you just want to know how those two features affect the predictions, then you can look at the weights (a.k.a. 'parameters' or 'coefficients') of your trained model. If the weight for feature 1 is x, then the predicted response increases by x when feature 1 increases by 1.0.

要在Amazon SageMaker中查看使用线性学习器算法训练的模型的权重,您可以下载model.tar.gz工件并在本地将其打开.可以从您在output参数中指定的S3位置的sagemaker.estimator.Estimator方法中下载模型工件.

To view the weights of a model trained with the linear learner algorithm in Amazon SageMaker, you can download the model.tar.gz artifact and open it locally. The model artifact can be downloaded from the S3 location you specified in the output argument to the sagemaker.estimator.Estimator method.

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]

这篇关于智者的逻辑回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆