Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
UKMP authored Dec 11, 2024
1 parent 732c979 commit 1022b11
Show file tree
Hide file tree
Showing 31 changed files with 10,799 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
########################################################################
##Chapter 1 - Introduction to Neural Networks - using R ################
###Simple R program to build, train and test neural networks ###########
### Classification based on 3 inputs and 1 categorical output ##########
########################################################################

###Choose the libraries to use
library(NeuralNetTools)
library(nnet)

###Set working directory for the training data
setwd("C:/R")
getwd()

###Read the input file
mydata=read.csv('RestaurantTips.csv',sep=",",header=TRUE)
mydata
attach(mydata)
names(mydata)

##Train the model based on output from input
model=nnet(CustomerWillTip~Service+Ambience+Food,
data=mydata,
size =5,
rang=0.1,
decay=5e-2,
maxit=5000)
print(model)
plotnet(model)
garson(model)
########################################################################
34 changes: 34 additions & 0 deletions 2024/Neural Network/Chapter01/Chapter1_NN_Introduction_Squares.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#########################################################################
###Chapter 1 - Introduction to Neural Networks - using R ################
###Simple R program to build, train and test neural Networks#############
#########################################################################
#Choose the libraries to use
library("neuralnet")

#Set working directory for the training data
setwd("C:/R")
getwd()

#Read the input file
mydata=read.csv('Squares.csv',sep=",",header=TRUE)
mydata
attach(mydata)
names(mydata)

#Train the model based on output from input
model=neuralnet(formula = Output~Input,
data = mydata,
hidden=10,
threshold=0.01 )
print(model)

#Lets plot and see the layers
plot(model)

#Check the data - actual and predicted
final_output=cbind (Input, Output,
as.data.frame(model$net.result) )
colnames(final_output) = c("Input", "Expected Output",
"Neural Net Output" )
print(final_output)
#########################################################################
31 changes: 31 additions & 0 deletions 2024/Neural Network/Chapter01/RestaurantTips.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
no,CustomerWillTip,Service,Ambience,Food,TipOrNo
1,1,4,4,5,Tip
2,1,6,4,4,Tip
3,1,5,2,4,Tip
4,1,6,5,5,Tip
5,1,6,3,4,Tip
6,1,3,4,5,Tip
7,1,5,5,5,Tip
8,1,5,4,4,Tip
9,1,7,6,4,Tip
10,1,7,6,4,Tip
11,1,6,7,2,Tip
12,1,5,6,4,Tip
13,1,7,3,3,Tip
14,1,5,1,4,Tip
15,1,7,5,5,Tip
16,0,3,1,3,No-tip
17,0,4,6,2,No-tip
18,0,2,5,2,No-tip
19,0,5,2,4,No-tip
20,0,4,1,3,No-tip
21,0,3,3,4,No-tip
22,0,3,4,5,No-tip
23,0,3,6,3,No-tip
24,0,4,4,2,No-tip
25,0,6,3,6,No-tip
26,0,3,6,3,No-tip
27,0,4,3,2,No-tip
28,0,3,5,2,No-tip
29,0,5,5,3,No-tip
30,0,1,3,2,No-tip
10 changes: 10 additions & 0 deletions 2024/Neural Network/Chapter01/Squares.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Input,Output
0,0
1,1
2,4
3,9
4,16
5,25
6,36
7,49
8,64
39 changes: 39 additions & 0 deletions 2024/Neural Network/Chapter02/Chapter2_Boston.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#####################################################################
###Chapter 2 - Introduction to Neural Networks - using R ############
###Simple R program to build, train, test regression neural networks#
#####################################################################
library("neuralnet")
library(MASS)

set.seed(1)

data = Boston

max_data <- apply(data, 2, max)
min_data <- apply(data, 2, min)
data_scaled <- scale(data,center = min_data, scale = max_data - min_data)

index = sample(1:nrow(data),round(0.70*nrow(data)))
train_data <- as.data.frame(data_scaled[index,])
test_data <- as.data.frame(data_scaled[-index,])

n = names(data)
f = as.formula(paste("medv ~", paste(n[!n %in% "medv"], collapse = " + ")))
net_data = neuralnet(f,data=train_data,hidden=10,linear.output=T)
plot(net_data)

predict_net_test <- compute(net_data,test_data[,1:13])

predict_net_test_start <- predict_net_test$net.result*(max(data$medv)-min(data$medv))+min(data$medv)
test_start <- as.data.frame((test_data$medv)*(max(data$medv)-min(data$medv))+min(data$medv))
MSE.net_data <- sum((test_start - predict_net_test_start)^2)/nrow(test_start)

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
MSE.lm
###########################################################################
35 changes: 35 additions & 0 deletions 2024/Neural Network/Chapter02/Chapter2_Kohonen.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
######################################################################
###Chapter 2 - Introduction to Neural Networks - using R ##########
###Usuervised ML technique using Kohonen package ####################
######################################################################

library("kohonen")

data("wines")
str(wines)
head(wines)
View (wines)

set.seed(1)
som.wines = som(scale(wines), grid = somgrid(5, 5, "hexagonal"))
som.wines
dim(getCodes(som.wines))

plot(som.wines, main = "Wine data Kohonen SOM")
par(mfrow = c(1, 1))
plot(som.wines, type = "changes", main = "Wine data: SOM")

training = sample(nrow(wines), 150)
Xtraining = scale(wines[training, ])
Xtest = scale(wines[-training, ],
center = attr(Xtraining, "scaled:center"),
scale = attr(Xtraining, "scaled:scale"))
trainingdata = list(measurements = Xtraining,
vintages = vintages[training])
testdata = list(measurements = Xtest, vintages = vintages[-training])
mygrid = somgrid(5, 5, "hexagonal")
som.wines = supersom(trainingdata, grid = mygrid)

som.prediction = predict(som.wines, newdata = testdata)
table(vintages[-training], som.prediction$predictions[["vintages"]])
######################################################################
32 changes: 32 additions & 0 deletions 2024/Neural Network/Chapter03/Chapter3_College.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
###########################################################################
#############Chapter 3 - Deep Learning with neuralnet######################
###########################################################################
library("neuralnet")
library(ISLR)

data = College
View(data)

max_data <- apply(data[,2:18], 2, max)
min_data <- apply(data[,2:18], 2, min)
data_scaled <- scale(data[,2:18],center = min_data, scale = max_data - min_data)

Private = as.numeric(College$Private)-1
data_scaled = cbind(Private,data_scaled)

index = sample(1:nrow(data),round(0.70*nrow(data)))
train_data <- as.data.frame(data_scaled[index,])
test_data <- as.data.frame(data_scaled[-index,])

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)

predicted_data <- compute(deep_net,test_data[,2:18])
print(head(predicted_data$net.result))
predicted_data$net.result <- sapply(predicted_data$net.result,round,digits=0)

table(test_data$Private,predicted_data$net.result)

###########################################################################
31 changes: 31 additions & 0 deletions 2024/Neural Network/Chapter03/Chapter3_H2O.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
##########################################################################
#################Chapter 3 - Deep Learning with H2O and R#################
##########################################################################

library(h2o)

c1=h2o.init(max_mem_size = "2G",
nthreads = 2,
ip = "localhost",
port = 54321)

data(iris)
summary(iris)

iris_d1 <- h2o.deeplearning(1:4,5,
as.h2o(iris),hidden=c(5,5),
export_weights_and_biases=T)
iris_d1
plot(iris_d1)

h2o.weights(iris_d1, matrix_id=1)
h2o.weights(iris_d1, matrix_id=2)
h2o.weights(iris_d1, matrix_id=3)
h2o.biases(iris_d1, vector_id=1)
h2o.biases(iris_d1, vector_id=2)
h2o.biases(iris_d1, vector_id=3)

#plot weights connecting `Sepal.Length` to first hidden neurons
plot(as.data.frame(h2o.weights(iris_d1, matrix_id=1))[,1])

##########################################################################
93 changes: 93 additions & 0 deletions 2024/Neural Network/Chapter04/Chapter4_Perceptron.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
######################################################################
###Chapter 4 - Introduction to Neural Networks - using R ##########
###Simple Perceptron implementation function in R - iris dataset ####
######################################################################

data(iris)
head(iris, n=20)

iris_sub=iris[1:100, c(1, 3, 5)]
names(iris_sub)=c("sepal", "petal", "species")
head(iris_sub)

library(ggplot2)

ggplot(iris_sub, aes(x = sepal, y = petal)) +
geom_point(aes(colour=species, shape=species), size = 3) +
xlab("Sepal length") +
ylab("Petal length") +
ggtitle("Species vs Sepal and Petal lengths")

euclidean.norm = function(x) {sqrt(sum(x * x))}

distance.from.plane = function(z,w,b) {
sum(z*w) + b
}

classify.linear = function(x,w,b) {
distances = apply(x, 1, distance.from.plane, w, b)
return(ifelse(distances < 0, -1, +1))
}

perceptron = function(x, y, learning.rate=1) {
w = vector(length = ncol(x)) # initialize weights
b = 0 # Initialize bias
k = 0 # count updates
R = max(apply(x, 1, euclidean.norm))
mark.complete = TRUE

while (mark.complete) {
mark.complete=FALSE
yc = classify.linear(x,w,b)
for (i in 1:nrow(x)) {
if (y[i] != yc[i]) {
w = w + learning.rate * y[i]*x[i,]
b = b + learning.rate * y[i]*R^2
k = k+1
mark.complete=TRUE
}
}
}
s = euclidean.norm(w)
return(list(w=w/s,b=b/s,updates=k))
}

x = cbind(iris_sub$sepal, iris_sub$petal)

y = ifelse(iris_sub$species == "setosa", +1, -1)

p = perceptron(x,y)

plot(x,cex=0.2)

points(subset(x,Y==1),col="black",pch="+",cex=2)
points(subset(x,Y==-1),col="red",pch="-",cex=2)

intercept = - p$b / p$w[[2]]
slope = - p$w[[1]] /p$ w[[2]]

abline(intercept,slope,col="green")
}


# Set the x inputs as Sepal and Petal lengths
x = cbind(iris_sub$sepal, iris_sub$petal)

# Lets label output as positive for setosa and the rest as negative
y = ifelse(iris_sub$species == "setosa", +1, -1)

#run the perceptron function
p = perceptron(x,y)

#Plot the linear separation
plot(x,cex=0.2)

points(subset(x,Y==1),col="black",pch="+",cex=2)
points(subset(x,Y==-1),col="red",pch="-",cex=2)

intercept = - p$b / p$w[[2]]
slope = - p$w[[1]] /p$ w[[2]]

abline(intercept,slope,col="green")

######################################################################
Loading

0 comments on commit 1022b11

Please sign in to comment.