Skip to content

Instantly share code, notes, and snippets.

View obernardocosta's full-sized avatar
👨‍💻
It's all about data and maths

Bernardo Costa obernardocosta

👨‍💻
It's all about data and maths
View GitHub Profile
@obernardocosta
obernardocosta / Vector.py
Created March 5, 2020 16:04
Dunder (or Magic) Methods in Python (Implementing a Vector class)
class Vector(dict):
def __init__(self, a, b):
import uuid
self._load(a, b)
self.id = uuid.uuid4()
self.__name__ = 'Vector:'
def _build(self, a, b):
return Vector(a, b)
def _load(self, a, b):
@obernardocosta
obernardocosta / monty_hall_runner.py
Created February 7, 2020 19:31
Monty Hall Runner
acumulator = [0, 0]
for _ in range(10**6):
mh = MontyHall()
mh.set_random_guess()
mh.open_goat_door()
mh.switch_door() # comment to don't swich
result = mh.validade_guess_door()
if result: acumulator[0] += 1
@obernardocosta
obernardocosta / MontyHall.py
Created February 7, 2020 19:24
Monty Hall
"""author: `Bernardo Costa <bernardoantunescosta at gmail.com>`"""
import random
class MontyHall:
"""Monty Hall Simulator Class."""
def __init__(self):
self.doors = list('123')
self.reward_door = None
@obernardocosta
obernardocosta / boto3_hard_mock.md
Created January 28, 2020 18:52
boto3 Hard Mock

Boto3 Hard Mock

Start Mock

boto3_resource = boto3.resource

def boto3_mock_resource(service_name, region_name):
  return boto3_resource(
                        service_name=service_name,
 region_name=region_name,
@obernardocosta
obernardocosta / playing_with_primes.py
Last active January 16, 2020 23:24
Primes Factors and Primes Until a Number
"""Playing with primes."""
def get_square_root(n):
"""Return square root of n."""
return n ** (1.0 / 2)
def is_divisor(n, d):
"""Return True if n divides d, False otherwise."""
@obernardocosta
obernardocosta / Apriori DataFrame
Created December 10, 2019 23:03
Apriori DataFrame
import pandas as pd
# create sample dataset
columns = ['ID', 'Beer', 'Diaper', 'Gum', 'Soda', 'Snack']
dataset = [[1, 1, 1, 1, 1, 0],
[2, 1, 1, 0, 0, 0],
[3, 1, 1, 1, 0, 1],
[4, 1, 1, 0, 1, 1],
[5, 0, 1, 0, 1, 0],
@obernardocosta
obernardocosta / Apriori Usage
Created December 10, 2019 23:01
Apriori Class Usage
# Running Apriori
if 'ID' in df.columns: del df['ID'] # ID is not relevant to apriori
apriori_runner = Apriori(df, threshold=0.4, transform_bol=True)
apriori_df = apriori_runner.run(use_colnames=True)
print(apriori_df)
#output
# support itemsets length
@obernardocosta
obernardocosta / Apriori
Last active December 10, 2019 23:03
Class of Apriori
from mlxtend.frequent_patterns import apriori
class Apriori:
"""Apriori Class. Its has Apriori steps."""
threshold = 0.5
df = None
def __init__(self, df, threshold=None, transform_bol=False):
"""Apriori Constructor.
@obernardocosta
obernardocosta / python-venv-tutorial.md
Last active September 13, 2023 13:28
Python venv

Create virtual environment

python3 -m venv v-env

Active virtual environment

source v-env/bin/active
@obernardocosta
obernardocosta / run.sh
Last active October 13, 2019 21:53
Build and Deploy a Docker Image to a Kubernetes Cluster
# 1. open cloud shell
# 2. download the the resource lab named resources-echo-web.tar.gz
# 3. upload it in the cloud shell
# 4. extract the file
tar -xvzf resources-echo-web.tar.gz
# 5. build the image as v1 tag
docker build -t gcr.io/<PROJECT_ID>/echo-app:v1 .
# 6. check if the image was Built