This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# Complete the matchingStrings function below. | |
def matchingStrings(strings, queries): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Storage | |
attr_accessor :value | |
def initialize | |
@value = nil | |
@map = {} # string, Storage | |
end | |
def map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]) |
NewerOlder