User Tools

Site Tools


r:neural_network

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
r:neural_network [2016/12/14 08:22] – [E.G. 2] hkimscilr:neural_network [2016/12/14 09:02] (current) – [E.G 5] hkimscil
Line 502: Line 502:
 > paste("오차율 =", round(error.overall, digit = 2), "%") > paste("오차율 =", round(error.overall, digit = 2), "%")
 [1] "오차율 = 1.85 %"</code> [1] "오차율 = 1.85 %"</code>
 +
 +====== E.G. 3 ======
 +{{:r:creditset.csv}}
 +<WRAP info 70%>The dataset contains information on different clients who received a loan at least 10 years ago. The variables income (yearly), age, loan (size in euros) and LTI (the loan to yearly income ratio) are available. Our goal is to devise a model which predicts, based on the input variables LTI and age, whether or not a default will occur within 10 years.
 +</WRAP>
 +
 +<code>set.seed(1234567890)
 +library("neuralnet")
 +
 +dataset <- read.csv("creditset.csv")
 +head(dataset)
 +
 +# extract a set to train the NN
 +trainset <- dataset[1:800, ]
 +
 +# select the test set
 +testset <- dataset[801:2000, ]
 +
 +## build the neural network (NN)
 +creditnet <- neuralnet(default10yr ~ LTI + age, trainset, hidden = 4, lifesign = "minimal", 
 +    linear.output = FALSE, threshold = 0.1)
 +
 +## plot the NN
 +plot(creditnet, rep = "best")
 +</code>
 +
 +__test the resulting output__
 +<code>temp_test <- subset(testset, select = c("LTI", "age"))
 +
 +creditnet.results <- compute(creditnet, temp_test)
 +head(temp_test)
 +
 +results <- data.frame(actual = testset$default10yr, prediction = creditnet.results$net.result)
 +results[100:115, ]
 +
 +results$prediction <- round(results$prediction)
 +results[100:115, ]
 +
 +pred.table <- table(testset$default10yr, results$prediction)
 +pred.table
 +
 +library(plyr)
 +count(testset, 'default10yr')
 +</code>
 +
 +with outputs
 +<code>> set.seed(1234567890)
 +> library("neuralnet")
 +
 +> dataset <- read.csv("creditset.csv")
 +> head(dataset)
 +  clientid      income         age          loan             LTI
 +1        1 66155.92510 59.01701507 8106.53213129 0.1225367511623
 +2        2 34415.15397 48.11715310 6564.74501768 0.1907515806612
 +3        3 57317.17006 63.10804949 8020.95329639 0.1399397996720
 +4        4 42709.53420 45.75197235 6103.64226014 0.1429105321411
 +5        5 66952.68885 18.58433593 8770.09923520 0.1309894999955
 +6        6 24904.06414 57.47160710   15.49859844 0.0006223320961
 +  default10yr
 +1           0
 +2           0
 +3           0
 +4           0
 +5           1
 +6           0
 +
 +> # extract a set to train the NN
 +> trainset <- dataset[1:800, ]
 +
 +> # select the test set
 +> testset <- dataset[801:2000, ]
 +
 +> ## build the neural network (NN)
 +> creditnet <- neuralnet(default10yr ~ LTI + age, trainset, hidden = 4, lifesign = "minimal", 
 ++                        linear.output = FALSE, threshold = 0.1)
 +hidden: 4    thresh: 0.1    rep: 1/1    steps:    7266 error: 0.79202 time: 2.94 secs
 +
 +> ## plot the NN
 +> plot(creditnet, rep = "best")
 +
 +</code>
 +
 +<code>> temp_test <- subset(testset, select = c("LTI", "age"))
 +
 +> creditnet.results <- compute(creditnet, temp_test)
 +> head(temp_test)
 +              LTI         age
 +801 0.02306808811 25.90644520
 +802 0.13729704954 40.77430558
 +803 0.10456984914 32.47350580
 +804 0.15985046411 53.22813215
 +805 0.11161429579 46.47915325
 +806 0.11489364221 47.12736998
 +
 +> results <- data.frame(actual = testset$default10yr, prediction = creditnet.results$net.result)
 +> results[100:115, ]
 +    actual                                 prediction
 +900      0 0.0000000000000000000000000015964854322398
 +901      0 0.0000000000000000000000000065162871249459
 +902      0 0.0000000000164043993271687692878796349660
 +903      1 0.9999999999219191249011373656685464084148
 +904      0 0.0000000000000000013810778585990655628959
 +905      0 0.0000000000000000539636283549268839978413
 +906      0 0.0000000000000000000234592312583964807452
 +907      1 0.9581419934268182725389806364546529948711
 +908      0 0.2499229633059938393557786184828728437424
 +909      0 0.0000000000000007044361454974903653282470
 +910      0 0.0006082559674722681341413332845036165963
 +911      1 0.9999999878713862200285689141310285776854
 +912      0 0.0000000000000000000000000015562211243506
 +913      1 0.9999999993455563895849991240538656711578
 +914      0 0.0000000000000000000000000000003082538282
 +915      0 0.0000000019359618836434052080615331181690
 +
 +> results$prediction <- round(results$prediction)
 +> results[100:115, ]
 +    actual prediction
 +900      0          0
 +901      0          0
 +902      0          0
 +903      1          1
 +904      0          0
 +905      0          0
 +906      0          0
 +907      1          1
 +908      0          0
 +909      0          0
 +910      0          0
 +911      1          1
 +912      0          0
 +913      1          1
 +914      0          0
 +915      0          0
 +</code>
 +
 +<code>> pred.table <- table(testset$default10yr, results$prediction)
 +> pred.table
 +   
 +          1
 +  0 1035    1
 +  1    3  161
 +>
 +
 +> library(plyr)
 +> count(testset, 'default10yr')
 +  default10yr freq
 +1           0 1036
 +2            164
 +
 +</code>
 +
 +====== E.G 5 ======
 +
r/neural_network.1481673176.txt.gz · Last modified: 2016/12/14 08:22 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki