Skip to content

Instantly share code, notes, and snippets.

View gabraganca's full-sized avatar
🚀
going above and beyond

Gustavo Bragança gabraganca

🚀
going above and beyond
View GitHub Profile
"""
Usage: python remove_output.py notebook.ipynb [ > without_output.ipynb ]
Modified from remove_output by Minrk
"""
import sys
import io
import os
import argparse
from IPython.nbformat.current import read, write
@gabraganca
gabraganca / README.md
Last active December 25, 2022 00:34
Kinesis Agent on Ubuntu 16.04

Setting Up Kinesis Agent on Ubuntu 16.04

To ease the process, I created a script that automatizes the installation of the Agent. The steps are:

  1. Create the configuration file for the agent following these readings:
@gabraganca
gabraganca / palindrome.py
Created May 17, 2018 02:20
Check if the word is or almost is a palindrome. To be almost a palindrome, we would need to change only one character.
def IsAlmostPalindrome(word):
"""
Check if the word is or almost is a palindrome.
To be almost a palindrome, we would need to change
only one character.
Parameters
----------
import time
import datetime
import random
import pandas as pd
import numpy as np
from surprise import SVD, BaselineOnly, Reader, Dataset
from surprise.model_selection import GridSearchCV, cross_validate, KFold
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
# Treinando algoritmo obtido com menor MAE
algo_svd_mae = gs.best_estimator['mae']
algo_svd_mae.fit(full_trainset)
print(get_rec_movies('Esposa', algo_svd_mae)) #Lista de filmes
# Treinando algoritmo obtido com menor RMSE
algo_svd_rmse = gs.best_estimator['rmse']
algo_svd_rmse.fit(full_trainset)
print(get_rec_movies('Esposa', algo_svd_rmse)) #Lista de filmes
# Obtém os resultados do Grid Search
df_results = pd.DataFrame.from_dict(gs.cv_results)
df_results.columns = df_results.columns.str.replace('param_','')
# Grafica os mapas de calor
n_epochs = len(param_grid['n_epochs'])
fig, axes = plt.subplots(nrows=n_epochs, ncols=3, figsize=(22, 6*n_epochs))
for ax_row, n_epoch in zip(axes, param_grid['n_epochs']):
for ax, metric in zip(ax_row, ['mae', 'rmse', 'time']):
def get_rec_movies(user_id:str, algoritmo, n_top: int =10) -> pd.DataFrame:
"""
Obtém uma lista de recomendação para N filmes para um
usuário.
Parameters:
user_id: o ID do usuário
n_top: O número de filmes desejados
Returns:
algo_base=BaselineOnly()
algo_base.fit(full_trainset)
# Implementação baseada em
# https://github.com/NicolasHug/Surprise/blob/master/examples/benchmark.py
set_cell_seed(MY_SEED)
# Os algortimos para verificar
classes = (SVD, BaselineOnly)
kf = KFold(random_state=MY_SEED) # certifica que as dobras serão as mesmas
table = []
# Define a grade de hiperparâmetros
param_grid = {
'n_factors': [10, 100, 200, 400],
'lr_all': [0.002, 0.005, 0.007, 0.009],
'n_epochs':[20, 100, 200, 400]
}
# Ajusta os modelos na grade com Validação Cruzada
gs = GridSearchCV(SVD, param_grid, measures=['mae', 'rmse'], cv=5, n_jobs=-1)
gs.fit(data)