Skip to content

Instantly share code, notes, and snippets.

View coreyjs's full-sized avatar

Corey Schaf coreyjs

View GitHub Profile
def set_value(ary, index: int) -> int:
if index < len(ary):
return ary[index]
else:
return None
def array_sum(ary) -> int:
if len(ary) == 0:
return 0
@coreyjs
coreyjs / sagemaker
Created April 2, 2021 01:11
Sagemaker Helpers
# Now the transform job has executed and the result, the estimated sentiment of each review, has been saved on S3.
# Since we would rather work on this file locally we can perform a bit of notebook magic to copy the file to the data_dir.
!aws s3 cp --recursive $xgb_transformer.output_path $data_dir
# First we will remove all of the files contained in the data_dir directory
!rm $data_dir/*
# And then we delete the directory itself
@coreyjs
coreyjs / sparsearr.py
Created February 20, 2021 04:32
Sparse Arrays - HackerRank
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the matchingStrings function below.
def matchingStrings(strings, queries):
@coreyjs
coreyjs / text.py
Created February 17, 2021 02:09
encode text
# encode the text and map each character to an integer and vice versa
# we create two dictionaries:
# 1. int2char, which maps integers to characters
# 2. char2int, which maps characters to unique integers
chars = tuple(set(text))
int2char = dict(enumerate(chars))
char2int = {ch: ii for ii, ch in int2char.items()}
# encode the text
@coreyjs
coreyjs / grammatrix.py
Created January 23, 2021 13:18
Calculate Gram Matrix
def gram_matrix(tensor):
""" Calculate the Gram Matrix of a given tensor
Gram Matrix: https://en.wikipedia.org/wiki/Gramian_matrix
"""
## get the batch_size, depth, height, and width of the Tensor
## reshape it, so we're multiplying the features for each channel
## calculate the gram matrix
batch_size, d, h, w = tensor.size()
tensor = tensor.view(d, h * w)
@coreyjs
coreyjs / classifier.py
Created October 26, 2020 14:00
Model training with dropout
model = Classifier()
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.parameters(), lr=0.003)
epochs = 30
steps = 0
train_losses, test_losses = [], []
for e in range(epochs):
@coreyjs
coreyjs / sigmoid.py
Created October 11, 2020 16:08
Sigmoid, yhat, error forumula and weight updating
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def output_formula(features, weights, bias):
return sigmoid(np.dot(features, weights) + bias)
def error_formula(y, output):
return - y*np.log(output) - (1 - y) * np.log(1-output)
def update_weights(x, y, weights, bias, learnrate):
@coreyjs
coreyjs / build_roc_auc.py
Created September 26, 2020 15:35
Function for calculating auc and roc
# Function for calculating auc and roc
def build_roc_auc(model, X_train, X_test, y_train, y_test):
'''
INPUT:
model - an sklearn instantiated model
X_train - the training data
y_train - the training response values (must be categorical)
X_test - the test data
y_test - the test response values (must be categorical)
@coreyjs
coreyjs / file_server.rb
Created August 6, 2020 21:27
File Server
class Storage
attr_accessor :value
def initialize
@value = nil
@map = {} # string, Storage
end
def map
@coreyjs
coreyjs / sum_pairs.py
Created August 6, 2020 16:52
K-diff pairs in an array
from collections import Counter
def find_pairs(nums, k) -> int:
# if k < 0, the result is 0
if k < 0:
return 0
count = Counter(nums)
pairs = set([])