forked from 01shruti/CreditDefault-Evaluation-using-ANN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgo.Rmd
100 lines (74 loc) · 2.53 KB
/
Algo.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
title: "R Notebook"
output: html_notebook
---
#Feedforward Algorithm
```{r}
#DS = read.csv("dataset.csv")
DS = read.csv("ds.csv")
trainingset = DS[1:2000,]
testingset = DS[2001:2500,]
#if(nrow(trainingset) + nrow(testingset) != nrow(DS)){
# print("There was an error while dividing dataset")
# stop();
#}
str(DS)
```
```{r}
library(neuralnet)
#model <- model.matrix(~ Default + Income + LoanBalance + MortgageType + Age + #MortgageYears + InterestRate + LoanToValue + CreditRating, data = DS )
model <- model.matrix(~ loan_status + loan_amnt + funded_amnt + term + annual_inc, data = DS )
```
```{r}
#net <- neuralnet(Default ~ Income + MortgageType + Age + MortgageYears + InterestRate + LoanToValue + CreditRating, data = model, hidden = 4, linear.output = F, lifesign = "minimal", threshold = 0.1)
net <- neuralnet(loan_status ~ loan_amnt + funded_amnt + term + annual_inc, data = model, hidden = 4, linear.output = F, lifesign = "minimal", threshold = 0.1)
plot(net)
```
```{r}
#testSet_Features = subset(testingset,select=c("Income", "MortgageType", "Age", "MortgageYears", "InterestRate", "LoanToValue", "CreditRating"));
testSet_Features = subset(testingset,select=c("loan_amnt", "funded_amnt", "term", "annual_inc"));
head(testSet_Features)
```
```{r}
NNresults = compute(net , testSet_Features );
finalOutput = data.frame(Actual = testingset$Default,
Prediction = NNresults$net.result,
Matches = doesPredictionMatch( testingset$Default, NNresults$net.result, 0.3));
row.names(finalOutput) = NULL;
print(cat("Prediction Success Rate : ",countSuccessPercent(finalOutput$Matches)));
print(finalOutput)
```
```{r}
doesPredictionMatch = function(expected = Null, predicted = Null, threshold = 0.3){
if(is.null(expected) || is.null(predicted)){
print("Necessary arguments missing or null");
stop();
}
results = rep(FALSE,length(expected));
for(i in 1:length(expected)){
if((!is.na(expected[i]))&&(!is.na(predicted[i]))){
if(abs(expected[i]-predicted[i])<threshold ){
results[i] = TRUE;
}
}
}
return (results);
}
```
```{r}
countSuccessPercent = function(input = NULL){
count= 0;
for(i in 1:length(input)){
tmp =as.logical(input[i]);
if(is.logical(tmp) && tmp ==TRUE ){
count=count+1;
}
}
return ((count/length(input))*100);
}
```
```{r}
#sol <- data.frame(actual = testingset$Default, prediction = NNresults$net.result)
sol <- data.frame(actual = testingset$loan_status, prediction = NNresults$net.result)
sol[]
```