Presenting the final result,several code still hidden, waitting for confirmation
### Prediction
Regression_Model <- lm(medv~., data=data)
#summary(Regression_Model)
test <- data[-index,]
predict_lm <- predict(Regression_Model,test)
MSE.lm <- sum((predict_lm - test$medv)^2)/nrow(test)
MSE.net_data
## [1] 10.54523
MSE.lm
## [1] 26.99266
## Warning: package 'kohonen' was built under R version 3.5.3
set.seed(1)
som.wines = som(scale(wines), grid = somgrid(5, 5, "hexagonal"))
som.wines
## SOM of size 5x5 with a hexagonal topology.
## Training data included.
dim(getCodes(som.wines))
## [1] 25 13
plot(som.wines, main = "Wine data Kohonen SOM")
plot(som.wines, type = "changes", main = "Wine data: SOM")
som.prediction = predict(som.wines, newdata = testdata)
table(vintages[-training], som.prediction$predictions[["vintages"]])
##
## Barbera Barolo Grignolino
## Barbera 8 0 0
## Barolo 0 3 0
## Grignolino 0 0 16
library("neuralnet")
library(ISLR)
## Warning: package 'ISLR' was built under R version 3.5.3
data = College
#View(data)
n = names(train_data)
f <- as.formula(paste("Private ~", paste(n[!n %in% "Private"], collapse = " + ")))
deep_net = neuralnet(f,data=train_data,hidden=c(5,3),linear.output=F)
plot(deep_net,rep="best")
predicted_data <- compute(deep_net,test_data[,2:18])
print(head(predicted_data$net.result))
## [,1]
## Adrian College 1
## Albertson College 1
## Albertus Magnus College 1
## Albion College 1
## Allentown Coll. of St. Francis de Sales 1
## Alverno College 1
predicted_data$net.result <- sapply(predicted_data$net.result,round,digits=0)
table(test_data$Private,predicted_data$net.result)
##
## 0 1
## 0 59 9
## 1 4 161
library("mlbench")
## Warning: package 'mlbench' was built under R version 3.5.3
library(neuralnet)
data(BreastCancer)
#summary(BreastCancer)
boxplot(data_cleaned[,2:10])
hist(as.numeric(data_cleaned$Mitoses))
par(mfrow=c(3, 3))
hist(as.numeric(data_cleaned$Cl.thickness))
hist(as.numeric(data_cleaned$Cell.size))
hist(as.numeric(data_cleaned$Cell.shape))
hist(as.numeric(data_cleaned$Marg.adhesion))
hist(as.numeric(data_cleaned$Epith.c.size))
hist(as.numeric(data_cleaned$Bare.nuclei))
hist(as.numeric(data_cleaned$Bl.cromatin))
hist(as.numeric(data_cleaned$Normal.nucleoli))
hist(as.numeric(data_cleaned$Mitoses))
## 'data.frame': 683 obs. of 11 variables:
## $ Id : chr "1000025" "1002945" "1015425" "1016277" ...
## $ Cl.thickness : Ord.factor w/ 10 levels "1"<"2"<"3"<"4"<..: 5 5 3 6 4 8 1 2 2 4 ...
## $ Cell.size : Ord.factor w/ 10 levels "1"<"2"<"3"<"4"<..: 1 4 1 8 1 10 1 1 1 2 ...
## $ Cell.shape : Ord.factor w/ 10 levels "1"<"2"<"3"<"4"<..: 1 4 1 8 1 10 1 2 1 1 ...
## $ Marg.adhesion : Ord.factor w/ 10 levels "1"<"2"<"3"<"4"<..: 1 5 1 1 3 8 1 1 1 1 ...
## $ Epith.c.size : Ord.factor w/ 10 levels "1"<"2"<"3"<"4"<..: 2 7 2 3 2 7 2 2 2 2 ...
## $ Bare.nuclei : Factor w/ 10 levels "1","2","3","4",..: 1 10 2 4 1 10 10 1 1 1 ...
## $ Bl.cromatin : Factor w/ 10 levels "1","2","3","4",..: 3 3 3 3 3 9 3 3 1 2 ...
## $ Normal.nucleoli: Factor w/ 10 levels "1","2","3","4",..: 1 2 1 7 1 7 1 1 1 1 ...
## $ Mitoses : Factor w/ 9 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 5 1 ...
## $ Class : Factor w/ 2 levels "benign","malignant": 1 1 1 1 1 2 1 1 1 1 ...
## - attr(*, "na.action")= 'omit' Named int 24 41 140 146 159 165 236 250 276 293 ...
## ..- attr(*, "names")= chr "24" "41" "140" "146" ...
max_data <- apply(input, 2, max)
min_data <- apply(input, 2, min)
input_scaled <- as.data.frame(scale(input,center = min_data, scale = max_data - min_data))
#View(input_scaled)
index = sample(1:nrow(final_data),round(0.70*nrow(final_data)))
train_data <- as.data.frame(final_data[index,])
test_data <- as.data.frame(final_data[-index,])
n = names(final_data[1:9])
f = as.formula(paste("Cancerbenign + Cancermalignant ~", paste(n, collapse = " + ")))
net = neuralnet(f,data=train_data,hidden=5,linear.output=FALSE)
plot(net,rep="best")
predict_net_test <- compute(net,test_data[,1:9])
predict_result<-round(predict_net_test$net.result, digits = 0)
net.prediction = c("benign", "malignant")[apply(predict_result, 1, which.max)]
predict.table = table(data_cleaned$Class[-index], net.prediction)
predict.table
## net.prediction
## benign malignant
## benign 137 1
## malignant 3 64
library(gmodels)
## Warning: package 'gmodels' was built under R version 3.5.2
CrossTable(x = data_cleaned$Class[-index], y = net.prediction,
prop.chisq=FALSE)
##
##
## Cell Contents
## |-------------------------|
## | N |
## | N / Row Total |
## | N / Col Total |
## | N / Table Total |
## |-------------------------|
##
##
## Total Observations in Table: 205
##
##
## | net.prediction
## data_cleaned$Class[-index] | benign | malignant | Row Total |
## ---------------------------|-----------|-----------|-----------|
## benign | 137 | 1 | 138 |
## | 0.993 | 0.007 | 0.673 |
## | 0.979 | 0.015 | |
## | 0.668 | 0.005 | |
## ---------------------------|-----------|-----------|-----------|
## malignant | 3 | 64 | 67 |
## | 0.045 | 0.955 | 0.327 |
## | 0.021 | 0.985 | |
## | 0.015 | 0.312 | |
## ---------------------------|-----------|-----------|-----------|
## Column Total | 140 | 65 | 205 |
## | 0.683 | 0.317 | |
## ---------------------------|-----------|-----------|-----------|
##
##