Skip to content

Instantly share code, notes, and snippets.

@jimpea
jimpea / matrix_multiplication.py
Created September 21, 2018 13:49
Matrix multiplication in Python 2.7
# Use the recomended numpy array notation
import numpy as np
y = np.array([6.95,5.22,6.36,7.03,9.71,9.67,10.69,13.88,13.21,14.82])
X0 = np.array(np.ones(10))
X1 = np.array(range(0, 10, 1))
X = np.column_stack((X0.T, X1.T))
yhat = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y.T)
print yhat
'''
> [ 4.97309091 1.06242424]
@jimpea
jimpea / polyplot.py
Last active December 3, 2018 16:17
Fit a Polynomial Model
import matplotlib.pyplot as plt
import numpy as np
x_data = np.arange(0, 10)
y_data = [n + np.random.normal() for n in x_data]
def poly_model(x_data, coeffs):
'''Fit data to a linear polynomial model
Args:
@jimpea
jimpea / logging.py
Created December 5, 2018 12:42
Log to file and console
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('combined.log')
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
@jimpea
jimpea / boxplots.py
Created January 4, 2019 09:23
Matplotlib Boxplots
import numpy as np
figsize = (10, 4) # set the fgsize for both the box plots and histograms
# make boxplots
# notch shape box plot
def make_boxplot(ax, data, title):
labels = [d.name for d in data]
ax.boxplot(data,
notch=True, # notch shape
@jimpea
jimpea / secondary_axis.py
Created July 11, 2019 09:44
Use matplotlib to plot two data series on separate y-axes.
from matplotlib import pyplot as plt
fractions = range(1, 16)
mcgs = [0,0,0,0,0,190,145,10,0,0,0,0,0,0,0]
dpms = [0,0,0,0,0,290,390,0,0,0,0,5,20,80,350]
#check lists for length
for list in [dpms, mcgs]:
assert len(list) == len(fractions), "lists should be same length"
@jimpea
jimpea / als.py
Last active August 13, 2019 08:55
asymmetric least squares
from scipy import sparse
from scipy.sparse.linalg import spsolve
import numpy as np
def als(self, y, lam, p, niter):
'''Asymmetric Least Squares Smoothing algorithm of Eilers and Boelens (2005)
From Stackoverflow 29185844 by Sparrowcide as modified by jpatina
Good choices for the parameters are:
@jimpea
jimpea / flatten.txt
Last active May 4, 2024 08:15
Excel Lambda function to convert an array to a list
FLATTEN = LAMBDA(array,
LET(
//name definitions
rows_, ROWS(array),
cols_, COLUMNS(array),
// index the zero-based array
seq_, SEQUENCE(rows_*cols_, 1, 0, 1),
// row and column indices
@jimpea
jimpea / appendrangev.txt
Created February 15, 2022 16:52
Excel Lambda function to stack two arrays vertically
//from <https://exceljet.net/formula/lambda-append-range>
//stack arrays vertically
APPENDRANGEV = LAMBDA(range1, range2, default,
LET(
rows1, ROWS(range1),
rows2, ROWS(range2),
cols1, COLUMNS(range1),
cols2, COLUMNS(range2),
rowindex, SEQUENCE(rows1 + rows2),
colindex, SEQUENCE(1, MAX(cols1, cols2)),
@jimpea
jimpea / appendrangeh.txt
Created February 15, 2022 16:53
SExcel Lambda function to stack two arrays horizontally
//from <https://exceljet.net/formula/lambda-append-range>
//stack arrays horizontally
APPENDRANGEH = LAMBDA(range1, range2, default,
LET(
rows1, ROWS(range1),
rows2, ROWS(range2),
cols1, COLUMNS(range1),
cols2, COLUMNS(range2),
rowindex, SEQUENCE(MAX(rows1, rows2)),
colindex, SEQUENCE(1, cols1 + cols2),
@jimpea
jimpea / lambdas.txt
Last active May 4, 2024 07:46
Some usefull Excel Lambda functions
/*
Append two ranges horizontally.
Inputs:
- range1: the first range
- range2: the second range
- default: the value entered into missing rows.
Return: The merged ranges, with empty rows filled with the default value. Missing
value within either of the two ranges filled with zeros (0). The number of rows