Skip to content

Instantly share code, notes, and snippets.

@kylecampbell
kylecampbell / flatten_list_of_lists.py
Last active February 1, 2019 18:57
Flatten List of Lists
# METHOD 1 vs. METHOD 2
# test sample
l = [[1,2,3],[4,5,6], [7], [8,9]]*99
# METHOD 1: List Comprehension (slower)
flat_list = [item for sublist in l for item in sublist]
# tested in .ipynb with %timeit
# METHOD 2: itertools chain (~2x faster)
@kylecampbell
kylecampbell / cubic_solver.c
Last active May 11, 2019 13:31
Cubic solver using Nickalls method
void cubic_solver(double A, double B, double C, double D, double *roots) {
double xN, yN; // yN = f(xN)
double del;
double h;
double theta;
double alpha, beta, gamma; // roots
xN = -B/(3*A);
yN = (A*xN*xN*xN) + (B*xN*xN) + (C*xN) + D;
del = sqrt( (B*B - 3*A*C) / ((9*A*A)) );
@kylecampbell
kylecampbell / python_3D_plotting.py
Created October 20, 2017 19:27
3D Plots with Python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np
import pandas as pd
import seaborn as sns
#uncomment below if using Jupyter
#%config InlineBackend.figure_format = 'retina'
# get data
@kylecampbell
kylecampbell / k_means_clustering.py
Last active December 1, 2023 07:00
K-Means Clustering with Tensorflow
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
import seaborn as sns
#uncomment below if using Jupyter
#%config InlineBackend.figure_format = 'retina'
# get data
df = pd.read_csv('../some/data/path')
@kylecampbell
kylecampbell / linear_regression.py
Last active October 20, 2017 18:52
Linear Regression with Tensorflow
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# setup dummy data
n = 1000
x = np.random.normal(0, 0.55, n)
y = 0.1*x + 0.3 + np.random.normal(0, 0.03, n) #want to find a=0.1 and b=0.3
plt.plot(x, y)
plt.show()