Skip to content

Instantly share code, notes, and snippets.

View marskar's full-sized avatar
👨‍💻
Per aspera💪ad astra✨

Martin Skarzynski marskar

👨‍💻
Per aspera💪ad astra✨
View GitHub Profile
@marskar
marskar / pipe.py
Last active August 31, 2019 05:48
Love❤️the #rstats®️pipe operator (%>%)❓ Try this👇🏽#Python🐍pipe function❗️
from functools import partial
def pipe(data, *funcs):
for f in funcs:
data = f(data)
return data
pipe(
import pandas as pd
from sklearn.model_selection import KFold, GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
# Get data
df = pd.read_csv("data/diamonds.csv")
df = df[df.cut.isin(["Ideal", "Good"])]
X = df.select_dtypes(["int64", "float64"])
y = df.cut
from functools import reduce
from pathlib import Path
import pandas as pd
def process_weather_data(filename):
df = pd.read_csv(filename).dropna()
first, *others = df.columns.to_list()
return df.melt(id_vars=first,
@marskar
marskar / logarithms.py
Last active August 29, 2019 21:18
Explain logarithms using the Python standard library math module
from math import log10, log2, e, log
x = 3.0
# Log base 10
one_k = 10**x # 1000.0
log10(one_k) # 3.0
# Log base 2
eight = 2**x # 8.0
@marskar
marskar / logarithms_numpy.py
Last active August 29, 2019 21:23
Explain logarithms using numpy
import numpy as np
r = list(range(-2, 3)) # [-2, -1, 0, 1, 2]
# Log base 10
tens = [10**i for i in r] # [0.01, 0.1, 1, 10, 100]
np.log10(tens).astype(int).tolist() # [-2, -1, 0, 1, 2]
# Log base 2
twos = [2**i for i in r] # [0.25, 0.5, 1, 2, 4]
@marskar
marskar / logistic.md
Last active August 29, 2019 20:24
Obtain the logistic function mathematically in 6 _easy_ steps ;)

Obtain the logistic function mathematically in 6 easy steps ;)

Step 1. Write out the linear regression equation

$\Huge y=\beta_0+\beta_1 x_1+...+\beta_n x_n$

Step 2. The logistic regression equation is the same as above except output is log odds

$\Huge log(odds)=\beta_0+\beta_1 x_1+...+\beta_n x_n$

Step 3. Exponentiate both sides of the logistic regression equation to get odds

$\Huge odds=e^{\beta_0+\beta_1 x_1+...+\beta_n x_n}$

Step 4. Write out the probability equation

$\Huge p=\frac{odds}{1+odds}$

@marskar
marskar / singles_numpy.py
Created August 17, 2019 17:56
Use Python🐍to get items that only show up once in a list & preserve order:
import numpy as np
def get_singles(arr):
while arr.size:
first, arr = arr[0], arr[1:]
if first in arr:
arr = arr[arr != first]
else:
yield first
@marskar
marskar / singles.py
Created August 17, 2019 17:53
Use Python🐍to get items that only show up once in a list & preserve order:
def get_singles(a_list):
while a_list:
x = a_list.pop(0)
if x in a_list:
a_list.remove(x)
else:
yield x
list(get_singles([0, 1, 1, 2, 3]))
@marskar
marskar / zen.py
Last active August 30, 2019 23:25
Pass "The Zen of Python" thru a pipeline to obtain and count words.
from this import s as zen
from codecs import encode
from functools import partial
def get_alpha(string):
return (char.lower() if char.isalpha() else " " for char in string)
def join_then_split(iterable):
@marskar
marskar / fibonacci_fizzbuzz.py
Last active August 15, 2019 20:21
A Pythonic pipeline to create a fizzbuzz fibonacci series.
def fib(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b
def fizzbuzz(iterable):
return (
"fizz" * (i % 3 == 0)