Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(tidyverse)
- df = read_csv("creditcard.csv")
- df = as.data.frame(df)
- dffraud = df[df$Class==1, ]
- dffraud = sample_n(dffraud, 400)
- dfnonfraud = df[df$Class==0, ]
- dfnonfraud = sample_n(dfnonfraud, 50000)
- dftrain = rbind(dffraud, dfnonfraud)
- dftrain = as.data.frame(dftrain)
- y = dftrain$Class == 1
- sigmoid <- function(x) 1 / (1 + exp(-x))
- feedforward <- function(x, w1, w2) {
- z1 <- cbind(1, x) %*% w1
- h <- sigmoid(z1)
- z2 <- cbind(1, h) %*% w2
- list(output = sigmoid(z2), h = h)
- }
- backpropagate <- function(x, y, y_hat, w1, w2, h, learn_rate) {
- dw2 <- t(cbind(1, h)) %*% (y_hat - y)
- dh <- (y_hat - y) %*% t(w2[-1, , drop = FALSE])
- dw1 <- t(cbind(1, x)) %*% (h * (1 - h) * dh)
- w1 <- w1 - learn_rate * dw1
- w2 <- w2 - learn_rate * dw2
- list(w1 = w1, w2 = w2)
- }
- train <- function(x, y, hidden = 5, learn_rate = 1e-3, iterations = 1e5) {
- d <- ncol(x) + 1
- w1 <- matrix(rnorm(d * hidden), d, hidden)
- w2 <- as.matrix(rnorm(hidden + 1))
- for (i in 1:iterations) {
- ff <- feedforward(x, w1, w2)
- bp <- backpropagate(x, y,
- y_hat = ff$output,
- w1, w2,
- h = ff$h,
- learn_rate = learn_rate)
- w1 <- bp$w1; w2 <- bp$w2
- }
- list(output = ff$output, w1 = w1, w2 = w2)
- }
- x = dftrain[, -dftrain$Class]
- x = as.matrix(x)
- #test
- #nnet = feedforward(x, nnet5$w1, nnet5$w2)
- #mean((nnet$output > .5) == y)
- #frauds = length(which(nnet$output > 0.05))
- #nonfrauds = length(which(nnet$output < 0.05))
- #train NN
- #nnet5 <- train(x, y, hidden = 300, iterations = 1e5)
- #mean((nnet5$output > .5) == y)
- save(nnet5, file = "nnet300_400_50000.RData")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement