Skip to content

Instantly share code, notes, and snippets.

View RoaldSchuring's full-sized avatar

Roald Schuring RoaldSchuring

  • Amsterdam
View GitHub Profile
@RoaldSchuring
RoaldSchuring / sklearn_nearest_neighbors.py
Last active February 5, 2020 13:00
Wine Recommender Sklearn Nearest Neighbors
import argparse
import pandas as pd
import os
from sklearn.externals import joblib
from sklearn.neighbors import NearestNeighbors
import numpy as np
import subprocess
import sys
def install(package):
def predict_fn(input_data, model):
input_data_reshaped = input_data.reshape(1, -1)
distance, indice = model.kneighbors(input_data_reshaped, 11)
distance_list = distance[0].tolist()[1:]
indice_list = indice[0].tolist()[1:]
nearest_neighbors = [distance_list, indice_list]
print('predict_fn output is', nearest_neighbors)
return np.array(nearest_neighbors)
from sagemaker_containers.beta.framework import worker, encoders
from six import BytesIO
def _npy_dumps(data):
# Serializes a numpy array into a stream of npy-formatted bytes.
buffer = BytesIO()
np.save(buffer, data)
return buffer.getvalue()
def output_fn(prediction_output, accept):
def model_fn(model_dir):
clf = joblib.load(os.path.join(model_dir, "model.joblib"))
return clf
@RoaldSchuring
RoaldSchuring / sklearn_nearest_neighbors.py
Created July 6, 2019 13:47
Running SKLearn script in Sagemaker
from sagemaker.sklearn.estimator import SKLearn
script_path = 'sklearn_nearest_neighbors.py'
sess = sagemaker.Session()
# run the Scikit-Learn script
sklearn = SKLearn(
entry_point=script_path,
train_instance_type="ml.m5.large",
role=role,
@RoaldSchuring
RoaldSchuring / sklearn_nearest_neighbors.py
Last active July 6, 2019 13:56
predict_method_sklearn_nearest_neighbors
recommendations = predictor.predict(sample_vector)
print(recommendations)
@RoaldSchuring
RoaldSchuring / creating_wine_review_embeddings.ipynb
Created July 7, 2019 23:42
specify_blazingtext_training_info
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@RoaldSchuring
RoaldSchuring / wine_recommender.ipynb
Last active July 7, 2019 23:45
set_sagemaker_and_model_params_blazingtext
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@RoaldSchuring
RoaldSchuring / sagemaker_functions.py
Last active July 8, 2019 02:08
setup_blazingtext_container2
train_data = 's3://{}/wine-corpus.txt'.format(bucket)
s3_output_location = 's3://{}/output'.format(bucket)
region_name = boto3.Session().region_name
container = sagemaker.amazon.amazon_estimator.get_image_uri(region_name, "blazingtext", "latest")
print('Using SageMaker BlazingText container: {} ({})'.format(container, region_name))
sess = sagemaker.Session()
# define the specifications of the sagemaker training instance
bt_model = sagemaker.estimator.Estimator(container,
role,
train_instance_count=2,
train_instance_type='ml.c4.2xlarge',
train_volume_size = 5,
train_max_run = 360000,
input_mode= 'File',