且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

sagemaker中的逻辑回归

更新时间:2022-03-03 09:24:59

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

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 个特征上训练了一个模型,则不可能使用同一个模型仅用 2 个特征进行预测.您必须为其他 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]