Skip to content

Instantly share code, notes, and snippets.

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:
# 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
@lunaluxie
lunaluxie / auth.py
Created June 26, 2017 09:09
Small utility that lets you compute and compare cryptographically secure password hashes
"""
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
@lunaluxie
lunaluxie / send_email.py
Created June 22, 2017 16:38
send email using python
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
@lunaluxie
lunaluxie / crypt.py
Created February 13, 2017 12:19
Simple encode/decode functions (cryptographically not safe)
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)
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:
@lunaluxie
lunaluxie / weighted_sentence_constructor.py
Last active January 6, 2017 15:24
Weighted random sentence constructor from dictionary words and weights
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 = []