Skip to content

Instantly share code, notes, and snippets.

Kasper Fredenslund kasperfred

Block or report user

Report or block kasperfred

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@kasperfred
kasperfred / weighted_sentence_constructor.py
Last active Jan 6, 2017
Weighted random sentence constructor from dictionary words and weights
View weighted_sentence_constructor.py
import random
import numpy
dict_weighted = {"word1" : 0.1, "word2" : 0.9} # keys = words, values = weights. Values must sum to 1
def create_d_sentence(dict_weighted):
length = random.randint(5,15)
keys = []
values = []
View avg_pixel.py
from PIL import Image
import numpy as np
img = Image.open('img.jpg', "r")
pixels = np.asarray(img)
add = [0,0,0]
index = 0
for row in pixels:
@kasperfred
kasperfred / crypt.py
Created Feb 13, 2017
Simple encode/decode functions (cryptographically not safe)
View crypt.py
import base64
def encode(key, string):
encoded_chars = []
for i in xrange(len(string)):
key_c = key[i % len(key)]
encoded_c = chr(abs(ord(string[i]) + ord(key_c) % 256))
encoded_chars.append(encoded_c)
encoded_string = "".join(encoded_chars)
@kasperfred
kasperfred / send_email.py
Created Jun 22, 2017
send email using python
View send_email.py
def send_email(user, pwd, recipient, subject, body):
import smtplib
gmail_user = user
gmail_pwd = pwd
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
@kasperfred
kasperfred / auth.py
Created Jun 26, 2017
Small utility that lets you compute and compare cryptographically secure password hashes
View auth.py
"""
Small utility that lets you compute and compare cryptographically secure password hashes
"""
def hash_password(password, salt=None, iterations=100000):
"""
Compute hash for a string using SHA1
input:
String: password
View genius.py
# Import a bunch of models
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.cross_decomposition import PLSRegression
from sklearn.ensemble import AdaBoostRegressor
View numerical_derivative.py
def numerical_derivative(func, func_input, respect_to_index=0, h=0.0001):
"""Compute the numerical derivative of a function
Args:
func (function): A function
func_input (list): A list of inputs given to the function
respect_to_index (int): The index of the value the derivative is calculated with respect to
h (float): the amount to tweak the function with to compute the gradient
Returns:
View merge_dicts.py
def flatten(obj):
flat = []
if hasattr(obj, '__iter__'):
for i in obj:
flat.extend(flatten(i))
else:
flat.append(obj)
return flat
def merge_dicts(list_of_dicts, flat=True):
View recursive_flatten.py
def flatten(obj):
flat = []
if hasattr(obj, '__iter__'):
for i in obj:
flat.extend(flatten(i))
else:
flat.append(obj)
return flat
View mnist_tensorboard_demo.py
import tensorflow as tf
tf.reset_default_graph()
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
### model ###
# input
with tf.name_scope('input') as scope:
You can’t perform that action at this time.