Skip to content

Instantly share code, notes, and snippets.

View MattSkiff's full-sized avatar

Matthew Skiffington MattSkiff

View GitHub Profile
library(ggplot2)
library(reshape2)
datime <- seq(as.POSIXct("2021-01-01"), as.POSIXct("2021-01-31"), by="d")
set.seed(5)
tmpout <- round(runif(31, 20, 35), 1)
tmpin <- tmpout + round(runif(31, -6, -5), 1)
temps <- data.frame(datime, tmpout, tmpin)
theme_set(theme_bw())
@MattSkiff
MattSkiff / pt_cluster.R
Last active January 26, 2021 22:07
Cluster some synthetic 1d data
library(tidyverse)
if (!"Ckmeans.1d.dp" %in% rownames(installed.packages())) {
install.packages("Ckmeans.1d.dp")
}
library(Ckmeans.1d.dp)
# params
n1 <- 150; u1 = 1.5; sd1 = 0.2
@MattSkiff
MattSkiff / dl_frameworks_pop_viz.R
Created March 15, 2020 11:52
ggplot2 - Sorted column chart with angled labels. Popularity of deep learning packages on github.
# author: matthew skiffington
# # purpose: make simple sorted column chart for dissertation re:dl repos
library(ggplot2)
library(scales)
dl_gitstars.vec <-
c(
'Pytorch' = 39600,
'Keras' = 47200,
@MattSkiff
MattSkiff / neural_network_architectures_diagram.R
Last active March 15, 2020 11:51
Example of simple flowchart using diagrammeR. Content: highly subjective! (from a shallow literature dive)
library(DiagrammeR)
gr <-grViz("digraph flowchart {
# node definitions with substituted label text
node [fontname = Helvetica, shape = rectangle]
# edge definitions with the node IDs
'Linear Combiner (Perceptron) (ANN)' -> 'Multiple Linear Combiner (One Layer Perceptron)';
'Multiple Linear Combiner (One Layer Perceptron)'-> 'MLP (FFNN)'
'MLP (FFNN)' -> 'RBF-NN';
@MattSkiff
MattSkiff / gradient_descent.R
Last active March 15, 2020 12:02
Gradient Descent in R. Uses DFDR (a simple auto differentiation library, for R) by mailund on github.
# Gradient descent example
# Start with some function f(x) we wish to optimise
# This might be the log likelihood - which can often be optimised analytically
# Or using more complex methods (e.g. the hessian, newton-raphson, fisher info)
# In practice this will usually be the loss likelihood of a NN
# To make this little script more generalisation, will use an AD package
#devtools::install_github("mailund/dfdr")
library(dfdr)
@MattSkiff
MattSkiff / vc_dim_highlight_viz.R
Last active March 11, 2020 10:55
Below, the code generates a grid of 16 plots with 4 randomly generated points and performs a logistic regression of them, then plots the decision boundary. The plot is highlighted in red if the classifier does not perfectly shatter the points. This draws attention to the Vapnik–Chervonenkis dimension, which is the minimum number of points a clas…
# author: matthew skiffington
# purpose: plain viz of vc4 (surpassing vc dimension) for linear classifiers to go in dissertation - 16 plots
# highlights when data isn't shattered (VC dim of linear classifier is d+1)
# randomly generates 3 points and fits a glm + plots decision boundary - since it is +2 from data dimension, it is not always shattered
# original source: glm code adapted from:
# glm code adapted from : https://stats.stackexchange.com/questions/6206/how-to-plot-decision-boundary-in-r-for-logistic-regression-model/6207
# plot code apated from: https://www.r-bloggers.com/beyond-basic-r-plotting-with-ggplot2-and-multiple-plots-in-one-figure/
library(ggplot2) # viz
library(cowplot) # multi-viz
@MattSkiff
MattSkiff / sigmoid_viz.R
Created March 11, 2020 07:26
Make sigmoid simple viz to go in dissertation
# author: matthew skiffington
# purpose: make sigmoid simple viz to go in dissertation
sigmoid.func <- function(x) {
sapply(
FUN = function(x) {
x = (exp(x)/(1+exp(x)))
},
X = x,
simplify = T
@MattSkiff
MattSkiff / vapnik_chervonenkis_linear_classifier_viz.R
Last active March 11, 2020 10:55
Random generation of plots and data, but we only create data sets with 3 points of data. You will notice the linear classifier (logistic regression, which has a linear combination of coefficients), perfectly shatters the data in all cases, as number of points of data is within d+1 (the dimensionality of the data is 2, n = 3). Regarding the possi…
# author: matthew skiffington
# purpose: plain viz of vc3 for linear classifiers to go in dissertation - 16 plots
# randomly generates 3 points and fits a glm + plots decision boundary
# original source: glm code adapted from:
# glm code adapted from : https://stats.stackexchange.com/questions/6206/how-to-plot-decision-boundary-in-r-for-logistic-regression-model/6207
# plot code apated from: https://www.r-bloggers.com/beyond-basic-r-plotting-with-ggplot2-and-multiple-plots-in-one-figure/
library(ggplot2) # viz
library(cowplot) # multi-viz
# author: matthew skiffington
# date: 09/03/2020
# purpose: produce timeline of nns for dissertation
# data #################
library(timelineS)
library(lubridate)
# old
# author: matthew skiffington
# purpose: make xor problem simple viz to go in dissertation
library(ggplot2)
xor.df <- setNames(object = data.frame(
rbind(
c(0,1,"Class 1"),
c(1,0,"Class 1"),
c(1,1,"Class 2"),
c(0,0,"Class 2")