Skip to content

Instantly share code, notes, and snippets.

View galenseilis's full-sized avatar
🚀
🟰🦀➕🐍

Galen Seilis galenseilis

🚀
🟰🦀➕🐍
View GitHub Profile
from itertools import combinations
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
from sklearn.datasets import load_iris
from sklearn.manifold import Isomap
X,y = load_iris(return_X_y=True)
pca = Isomap(n_components=2)
@galenseilis
galenseilis / normal_sample_mst.py
Created October 17, 2022 01:33
Minimum spanning tree of normally-distributed data in R^2,
from itertools import combinations
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
X = np.random.normal(size=1000).reshape((500,2))
pos = {i:X[i] for i in range(X.shape[0])}
g = nx.Graph()
import numpy as np
from scipy.odr import *
import pandas as pd
n = 10000
true_X = np.concatenate(tuple([np.eye(3)]*n))
true_X[:,0] = 1.0
true_beta = np.array([3, 5, 7])
true_y = np.matmul(true_X, true_beta)
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
x = np.random.uniform(-10,10,size=10000)
y = x**2
dydx = np.exp(x) / (1 + np.exp(-x)) ** 2
d = {'x':x, 'y':y, 'dydx':dydx}
df = pd.DataFrame(d)
@galenseilis
galenseilis / bracket_depth.py
Created October 17, 2022 01:45
Bracket depth counter for a Javascript file I no longer have...
from graphviz import Digraph
def brack_depth_map(file, bracks=('{', '}')):
open_brack, close_brack = bracks
d = {}
depth = 0
with open(source_path) as f:
for line_index, line in enumerate(f):
d[line] = depth
if open_brack in line:
@galenseilis
galenseilis / describe_calculus_function.py
Created October 17, 2022 01:46
Describe a function from elementary calculus using SymPy.
import numpy as np
import sympy as sp
from sympy.abc import a, x, y, z
def reflect(exp):
for symbol in exp.free_symbols:
exp = exp.subs(symbol, -symbol)
return exp
@galenseilis
galenseilis / EXOR_keras.py
Created October 17, 2022 01:48
Train a simple neural network to learn the exclusive-or function.
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
X = np.array([[0,0],[0,1],[1,0],[1,1]], "float32")
y = np.array([[0],[1],[1],[0]], "float32")
@galenseilis
galenseilis / plot_sphere_sympy_matplotlib.py
Created October 17, 2022 01:54
Plots a sphere using SymPy and Matplotlib.
import matplotlib.pyplot as plt
from sympy import cos, pi, sin
from sympy.abc import phi, theta
from sympy.plotting.plot import plot3d_parametric_surface
plt.rcParams['figure.figsize'] = 10,10
p1 = plot3d_parametric_surface(sin(phi) * cos(theta), sin(phi) * sin(theta), cos(phi),
(phi, 0, pi), (theta, 0, 2 * pi))
@galenseilis
galenseilis / game_of_life_youtube.py
Created October 17, 2022 01:57
Toy example of Game of Life with grid size suitable for YouTube
from itertools import combinations, chain, product
import matplotlib.pyplot as plt
import numpy as np
def full_frame(width=19.20, height=10.80):
import matplotlib as mpl
mpl.rcParams['savefig.pad_inches'] = 0
figsize = None if width is None else (width, height)
fig = plt.figure(figsize=figsize)
ax = plt.axes([0,0,1,1], frameon=False)
@galenseilis
galenseilis / modulo_gcd_raster.py
Created October 17, 2022 02:01
Computes a raster whose entries a i %j, j % i, and gcd(i,j)
import numpy as np
import matplotlib.pyplot as plt
shape=(1080, 1920, 3)
img = np.zeros(shape)
for i in range(1, shape[0]):
for j in range(1, shape[1]):
img[i,j,0] = i%j
img[i,j,1] = j%i