Skip to content

Instantly share code, notes, and snippets.

@clayford
Last active July 29, 2020 19:21
Show Gist options
  • Save clayford/92b661115536efedb0a87ddde850a185 to your computer and use it in GitHub Desktop.
Save clayford/92b661115536efedb0a87ddde850a185 to your computer and use it in GitHub Desktop.
# R version of load_planar_dataset() on https://datascience-enthusiast.com/DL/Planar-data-classification-with-one-hidden-layer.html
# Could use python code to export result of load_planar_dataset() as CSV:
# import pandas as pd
# df1 = pd.DataFrame(np.transpose(X), columns = ['X1','X2'])
# df2 = pd.DataFrame(np.transpose(Y), columns = ['Y'])
# df = pd.concat([df1, df2], axis = 1)
# df.to_csv('planar_flower.csv', index = False)
# Or create data in R as a data frame
planar_dataset <- function(){
set.seed(1)
m <- 400
N <- m/2
D <- 2
X <- matrix(0, nrow = m, ncol = D)
Y <- matrix(0, nrow = m, ncol = 1)
a <- 4
for(j in 0:1){
ix <- seq((N*j)+1, N*(j+1))
t <- seq(j*3.12,(j+1)*3.12,length.out = N) + rnorm(N, sd = 0.2)
r <- a*sin(4*t) + rnorm(N, sd = 0.2)
X[ix,1] <- r*sin(t)
X[ix,2] <- r*cos(t)
Y[ix,] <- j
}
d <- as.data.frame(cbind(X, Y))
names(d) <- c('X1','X2','Y')
d
}
# try out
df <- planar_dataset()
library(ggplot2)
ggplot(df, aes(x = X1, y = X2, color = factor(Y))) +
geom_point()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment