How to save sklearn model on s3 using joblib.dump?

前端 未结 3 1673
暖寄归人
暖寄归人 2021-02-15 16:09

I have a sklearn model and I want to save the pickle file on my s3 bucket using joblib.dump

I used joblib.dump(model, \'model.pkl\') to save the model local

3条回答
  •  面向向阳花
    2021-02-15 16:26

    Use following code to dump your model to s3 location in .pkl or .sav format:

    import tempfile
    import boto3
    s3 = boto3.resource('s3')
    
    # you can dump it in .sav or .pkl format 
    location = 's3://bucket_name/folder_name/'
    model_filename = 'model.sav'  # use any extension you want (.pkl or .sav)
    OutputFile = location + model_filename
    
    # WRITE
    with tempfile.TemporaryFile() as fp:
        joblib.dump(scikit_learn_model, fp)
        fp.seek(0)
        # use bucket_name and OutputFile - s3 location path in string format.
        s3.Bucket('bucket_name').put_object(Key= OutputFile, Body=fp.read())
    

提交回复
热议问题