Skip to content

Instantly share code, notes, and snippets.

View whiledoing's full-sized avatar

whiledoing whiledoing

View GitHub Profile
View interface-slice.go
// from: https://stackoverflow.com/questions/12753805/type-converting-slices-of-interfaces
// make any slice to interface{} slice
func InterfaceSlice(slice interface{}) []interface{} {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
panic("InterfaceSlice() given a non-slice type")
}
ret := make([]interface{}, s.Len())
View python-pipe-functions.py
class Pipe(object):
def __init__(self, func):
self.func = func
def __ror__(self, other):
def generator():
for obj in other:
if obj is not None:
yield self.func(obj)
return generator()
@whiledoing
whiledoing / exponential-probability-demo.py
Last active February 1, 2020 08:27
[scipy-snippets] scipy snippets #python #scipy
View exponential-probability-demo.py
# from: https://nbviewer.jupyter.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/blob/master/Chapter1_Introduction/Ch1_Introduction_PyMC3.ipynb
import scipy.stats as stats
from matplotlib import pyplot as plt
import numpy as np
a = np.linspace(0, 4, 100)
expo = stats.expon
lambda_ = [0.5, 1]
for l, c in zip(lambda_, colours):
@whiledoing
whiledoing / svm-demo.py
Last active February 1, 2020 08:24
[sklearn-snippets] skl-learn snippets #python #sklearn
View svm-demo.py
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widget
from sklearn.datasets import make_blobs
from sklearn.svm import SVC
def plot_svc_decision_function(model, ax=None, plot_support=True):
"""Plot the decision function for a 2D SVC"""
ax = ax or plt.gca()
@whiledoing
whiledoing / linked-brush-selection.py
Last active January 21, 2020 09:59
[altair-snippets] altair snippets #python #altair #visualization
View linked-brush-selection.py
# load an example dataset
from vega_datasets import data
cars = data.cars()
import altair as alt
interval = alt.selection_interval()
base = alt.Chart(cars).mark_point().encode(
y='Miles_per_Gallon',
@whiledoing
whiledoing / circle-with-auto-color.py
Created January 21, 2020 09:40
[bokeh-snippets] bokeh snippets #python #bokeh #visualization
View circle-with-auto-color.py
import numpy as np
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
# Call once to configure Bokeh to display plots inline in the notebook.
output_notebook()
N = 4000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
@whiledoing
whiledoing / heatmap-witth-rotation.py
Last active January 21, 2020 09:41
[seaborn-snippets] seaborn snippets #python #seaborn #visualization
View heatmap-witth-rotation.py
from sklearn.datasets import load_digits
digits = load_digits()
# split data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, shuffle=True)
# use Gaussian Naive Bayes
from sklearn.naive_bayes import GaussianNB
model = GaussianNB()
@whiledoing
whiledoing / contour-example.py
Last active January 22, 2020 09:34
[matplotlib-snippets] matplotlib snippets #matplotlib #python #visualization
View contour-example.py
def f(x, y):
return np.sin(x) ** 10 + np.cos(10+x*y) + np.cos(x)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 50)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
# draw contour
contour = plt.contour(X, Y, Z, 3, colors='black')
@whiledoing
whiledoing / data-reader.py
Last active January 21, 2020 09:41
[pandas-snippet] pandas snippets #python #pandas #datascience
View data-reader.py
from pandas_datareader import data
goog = data.DataReader('GOOG', start='2014', end='2020', data_source='yahoo')
goog.columns = goog.columns.str.lower()
goog = goog.asfreq('D', method='bfill')
@whiledoing
whiledoing / memo.py
Created December 28, 2019 11:09
[python-memo-decorator] python memo func call result decorator #python #util
View memo.py
from functools import wraps
def memoize(function):
memo = {}
@wraps(function)
def wrapper(*args):
try:
return memo[args]
except KeyError:
rv = function(*args)