Skip to content

Instantly share code, notes, and snippets.

@sethbunke
sethbunke / flask-restart-on-file-change.txt
Last active January 28, 2021 12:59
Simple Way to Restart Python Flask on File Change During Development
export FLASK_DEBUG=1
export FLASK_APP=name-of-your-python-file-here.py
flask run
@sethbunke
sethbunke / pandas-dummy-variables.txt
Last active May 24, 2019 23:15
Simple example of creating dummy variables using Python Pandas
#import pandas and numpy
import pandas as pd
import numpy as np
#create dataframe with some random data
df = pd.DataFrame(np.random.rand(10, 2) * 10, columns=['Price', 'Qty'])
#add a column with random string values that would need to have dummy variables created for them
df['City'] = [np.random.choice(('Chicago', 'Boston', 'New York')) for i in range(df.shape[0])]
@sethbunke
sethbunke / install-tensorflow-using-anaconda.txt
Last active November 1, 2018 00:19
Create a virtual environment using Anaconda and install various ML tools and TensorFlow
//Mac OS or Linux
conda create -n tensorflow python=3.5
source activate tensorflow
pip install widgetsnbextension
conda install pandas matplotlib jupyter notebook scipy scikit-learn
conda install -c conda-forge tensorflow
//Windows
conda create -n tensorflow python=3.5
activate tensorflow
@sethbunke
sethbunke / understanding-softmax-function.txt
Last active February 24, 2017 20:52
Understanding Softmax Function
#Concise implementation
def softmax(inputs):
return np.exp(inputs) / np.sum(np.exp(inputs))
#Verbose implementation (for clarification/understanding)
def softmax_verbose(inputs):
results = [np.exp(x) for x in inputs]
summation = sum(results)
probabilities = [(x / summation) for x in results]
return probabilities
@sethbunke
sethbunke / bag-of-words.txt
Created February 28, 2017 00:35
Simple implementation of method to create a "bag of words" (counts of number of words in a string)
from collections import Counter
def bag_of_words(text):
return Counter(text.split())
test_text = 'the quick brown fox jumps over the lazy dog'
print(bag_of_words(test_text))
#Output will be: Counter({'the': 2, 'jumps': 1, 'dog': 1, 'brown': 1, 'over': 1, 'quick': 1, 'fox': 1, 'lazy': 1})
@sethbunke
sethbunke / exit-edit-mode-in-vim
Created March 6, 2017 20:21
Not Really a Gist - I Just Keep Forgetting How To Do This
To save your work and exit press Esc and then :wq (w for write and q for quit).
Alternatively, you could both save and exit by pressing Esc and then :x
@sethbunke
sethbunke / text-to-integer-and-integer-to-text-mappings-lamba.txt
Last active March 31, 2017 07:21
Create text to integer and integer to text mappings
create_text_lookup_tables = lambda text: ({c: i for i, c in enumerate(set(text))}, dict(enumerate(set(text))))
vocab_to_int, int_to_vocab = create_text_lookup_tables(['this','is','my','test'])
print(vocab_to_int)
#{'is': 0, 'my': 1, 'this': 2, 'test': 3}
print(int_to_vocab)
#{0: 'is', 1: 'my', 2: 'this', 3: 'test'}
@sethbunke
sethbunke / filter-strings-by-length-using-list-comprehension
Created May 2, 2017 00:06
Filter strings of a minimum and maximum length using list comprehension
min_length = 1 #as long or longer than this
max_length = 4 #no longer than this
data = ['', 'a', 'bb', 'cc', '', 'ddd', 'eee', 'ffff', 'ggggg', 'hhhhhh']
results = [item[:max_length] for item in data if len(item) >= min_length]
print(results)
#['a', 'bb', 'cc', 'ddd', 'eee', 'ffff', 'gggg', 'hhhh']
@sethbunke
sethbunke / tanh-and-tanh-derivative.txt
Created May 8, 2017 22:26
tanh and tanh derivative
def tanh(x):
return (1.0 - numpy.exp(-2*x))/(1.0 + numpy.exp(-2*x))
def tanh_derivative(x):
return (1 + tanh(x))*(1 - tanh(x))
@sethbunke
sethbunke / duplicate-values-in-array
Created May 25, 2017 01:05
Identify duplicate values in an array
import collections
input = [1,2,3,2,1,5,6,5,5,5]
result = [item for item, count in collections.Counter(input).items() if count > 1]
print(result)
#[1, 2, 5]