r:neural_network
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| r:neural_network [2016/12/14 08:22] – [E.G. 2] hkimscil | r:neural_network [2016/12/14 09:02] (current) – [E.G 5] hkimscil | ||
|---|---|---|---|
| Line 502: | Line 502: | ||
| > paste(" | > paste(" | ||
| [1] " | [1] " | ||
| + | |||
| + | ====== E.G. 3 ====== | ||
| + | {{: | ||
| + | <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. | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | library(" | ||
| + | |||
| + | dataset <- read.csv(" | ||
| + | head(dataset) | ||
| + | |||
| + | # extract a set to train the NN | ||
| + | trainset <- dataset[1: | ||
| + | |||
| + | # select the test set | ||
| + | testset <- dataset[801: | ||
| + | |||
| + | ## build the neural network (NN) | ||
| + | creditnet <- neuralnet(default10yr ~ LTI + age, trainset, hidden = 4, lifesign = " | ||
| + | linear.output = FALSE, threshold = 0.1) | ||
| + | |||
| + | ## plot the NN | ||
| + | plot(creditnet, | ||
| + | </ | ||
| + | |||
| + | __test the resulting output__ | ||
| + | < | ||
| + | |||
| + | creditnet.results <- compute(creditnet, | ||
| + | head(temp_test) | ||
| + | |||
| + | results <- data.frame(actual = testset$default10yr, | ||
| + | results[100: | ||
| + | |||
| + | results$prediction <- round(results$prediction) | ||
| + | results[100: | ||
| + | |||
| + | pred.table <- table(testset$default10yr, | ||
| + | pred.table | ||
| + | |||
| + | library(plyr) | ||
| + | count(testset, | ||
| + | </ | ||
| + | |||
| + | with outputs | ||
| + | < | ||
| + | > library(" | ||
| + | > | ||
| + | > dataset <- read.csv(" | ||
| + | > head(dataset) | ||
| + | clientid | ||
| + | 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 | ||
| + | default10yr | ||
| + | 1 0 | ||
| + | 2 0 | ||
| + | 3 0 | ||
| + | 4 0 | ||
| + | 5 1 | ||
| + | 6 0 | ||
| + | > | ||
| + | > # extract a set to train the NN | ||
| + | > trainset <- dataset[1: | ||
| + | > | ||
| + | > # select the test set | ||
| + | > testset <- dataset[801: | ||
| + | > | ||
| + | > ## build the neural network (NN) | ||
| + | > creditnet <- neuralnet(default10yr ~ LTI + age, trainset, hidden = 4, lifesign = " | ||
| + | + linear.output = FALSE, threshold = 0.1) | ||
| + | hidden: 4 thresh: 0.1 rep: 1/1 steps: | ||
| + | > | ||
| + | > ## plot the NN | ||
| + | > plot(creditnet, | ||
| + | > | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | > | ||
| + | > creditnet.results <- compute(creditnet, | ||
| + | > 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, | ||
| + | > results[100: | ||
| + | actual | ||
| + | 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: | ||
| + | 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 | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | > pred.table | ||
| + | |||
| + | | ||
| + | 0 1035 1 | ||
| + | 1 3 161 | ||
| + | > | ||
| + | |||
| + | > library(plyr) | ||
| + | > count(testset, | ||
| + | default10yr freq | ||
| + | 1 0 1036 | ||
| + | 2 | ||
| + | |||
| + | </ | ||
| + | |||
| + | ====== E.G 5 ====== | ||
| + | |||
r/neural_network.1481673176.txt.gz · Last modified: by hkimscil
