Save model in aws-s2 with function in python

Viewed 22

I need to make a function to save my model in pickle but i have this mistake:

"ParamValidationError: Parameter validation failed:Invalid type for parameter Body, value: None, type: <class 'NoneType'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object"

Parameters

TS = TimeSeriesSplit(n_splits = 5)

dt = DecisionTreeClassifier()

grid = {'max_depth': [5,7,9,11,15], 'min_samples_leaf': [5,7,9,11,13],'criterion': ['gini','entropy']}

gs = GridSearchCV(dt, param_grid=grid, cv=TS)

gs.fit(X_train, y_train)

Function

def save_feature_matrix(bucket, bucket_path,dataset):

session = boto3.Session(aws_access_key_id,aws_secret_access_key,aws_session_token)

s3 = session.resource('s3')

s3.Object(bucket, bucket_path).put(Body=dataset)

Load

bucket = "aplicaciones-cd-12"

key = "modelos/arboles_gridsearch.pkl"

pickle_data=pickle.dump(gs, open('model.pkl','wb'))

save_feature_matrix(bucket, key, pickle_data)

1 Answers

My mistake was that I was not assigning the fitted model to a variable:

model=gs.fit(X_train, y_train)

and then I upload:

def save_model(bucket, bucket_path,dataset): 
   
    session = boto3.Session(

        aws_access_key_id,aws_secret_access_key,aws_session_token)

    s3 = session.resource('s3')
    s3.Object(bucket, bucket_path).put(Body=dataset)

    bucket = "aplicaciones-cd-12"

key = "modelos/arboles_gridsearch.pkl"

pickle_data = pickle.dumps(model)

save_model(bucket, key, pickle_data)
Related