Skip to content

Instantly share code, notes, and snippets.

@jessstringham
jessstringham / simple.py
Created June 20, 2023 04:23
Samples for a session at ITP camp 2023
from collections import defaultdict
import random
WINDOW_SIZE = 2
with open("alice.txt") as f:
full_text = f.read()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
from bokeh.models import HoverTool
from bokeh.plotting import figure, show, output_file, ColumnDataSource
from bokeh.layouts import gridplot
from bokeh.palettes import magma
def plot(u, descs=index_to_interesting_subject):
source = ColumnDataSource(data=dict(
x1=u[:, 0],
x2=u[:, 1],
x3=u[:, 2],
'''This is hacky code I use to convert Jupyter notebooks into Jekyll posts.
Notebooks' first line should be
# Title
and `Title` will be used as the post's title.
I convert LaTeX into the form MathJax needs.
To make plots work, see `get_maybe_image_code` comment.
image = mpimg.imread(IMAGE_LOCATION)
# pasta the first image
h, w, c = image.shape
image = image.reshape(h // 2, 2, w, c)
image = image.transpose(1, 0, 2, 3)
image = np.hstack(image)
# rotate the image
image = image.transpose(1, 0, 2)
import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
PASTA_SIZE = 16
NUM_IMAGE_SPLITS = 2
IMAGE_LOCATION = 'something.png'
image = mpimg.imread(IMAGE_LOCATION)

np.einsum

I had this gross reshape/tensor product/transpose stuff on huge matrices, and I knew it was making intermediate copies of the matrices that I didn't want to. So I tried out np.einsum, and I think it actually turned out simpler than thinking through the other matrix manipulation.

Here are some quick notes.

Real blogs/documentation

import numpy as np
from numpy.lib.stride_tricks import as_strided
# Makes a new matrix like this:
# input: [0, 1, 2, 3]
# output: [[0, 1], [1, 2], [2, 3]]
# But, does it all in memory using `as_strided`, so it's speedy.
# And if you make a mistake, you can see fake numbers
@jessstringham
jessstringham / 0.notes.md
Last active November 17, 2017 09:46
Bayesian linear regression with polynomial basis functions

Srry, this is a bit of an awkward format. I might move this and a few other demos to a repo.

This is code one could drop into Jupyter [1] notebook cells and draw some wicked-looking graphs. I wrote it to understand Bayesian linear regression (for this classMLPR, specifically this and this). No promises that I did it perfectly.

The graphs show samples from the posterior weights in pink.