我必须在4 * 4维上实现kohonen算法。这个可以用,但是我有一个小问题,因为我得到这个错误:
######################################
## Initialize random matrix phi/psi ##
######################################
##Pour mac
setwd("/Users/amandinelecerfdefer/Desktop/Kohonen_MAP")
data_phipsi<-read.csv("/Users/amandinelecerfdefer/Desktop/Kohonen_MAP/pbA.txt.new.2.rep30.phipsi",h=F)
colnames(data_phipsi)<-c("phi1","psi2","phy2","psi3","phy3","psi4","phy4","psi5")
random_list<-list()
borne<-16
for(n in 1:borne){
random_list[[n]]<-floor(runif(8,min=-180,max=180))
}
# ##############Print random matrix#####
# for(n in 1:16){
# print(random_list[[n]])
# }
#creation of a matrix 4*4 representing the Kohonen map containing the vectors of the angles phi/psi
Kohonen_matrix<-matrix(random_list,ncol=sqrt(borne),nrow=sqrt(borne))
rownames(Kohonen_matrix)<-c('1','2','3','4')
colnames(Kohonen_matrix)<-c('1','2','3','4')
#############################################
##Function for distance calculation (RMSDA)##
#############################################
RMSDA<-function(data_phipsi,Kohonen_matrix)
{
difference<-data_phipsi-Kohonen_matrix
for(j in 1:length(difference)){
if (difference[j]< -180) {difference[j]=difference[j]+360}
if (difference[j]> +180) {difference[j]=difference[j]-360}
}
distance=mean(sqrt(difference^2))
return(distance)
}
#######################
##LEARNING FUNCTION ##
#######################
learning<-function(initial_rate,iteration,data_phipsi){
return(initial_rate/(1+(iteration/nrow(data_phipsi))))
}
##############################
## Program for Kohonen MAp ###
##############################
initial_rate=0.75 # Decrease with training time
initial_radius=2 # Decrease with training time
iteration=3
for(step in 1:iteration)
{
data_phipsi<-data_phipsi[sample(nrow(data_phipsi)),] # Sample vectors of training (samples of lines of the dataframe)
print(step) #Visualize where we are in loops
for(k_row in 1:nrow(data_phipsi))
{
#Update learn_rate and radius at each row of each iteration
learn_rate<-learning(initial_rate,((step-1)*nrow(data_phipsi))+k_row,data_phipsi)
learn_radius<-learning(initial_rate,((step-1)*nrow(data_phipsi))+k_row,data_phipsi)
#Find distance between each vectors of angles of Kohonen Map and the training vector
phipsi_RMSDA<-lapply(random_list, RMSDA, data_phipsi=data_phipsi[k_row,])
#Unlist and create a matrix of distances
vector_RMSDA<-unlist(phipsi_RMSDA)
matrix_RMSDA<-matrix(vector_RMSDA,sqrt(borne),sqrt(borne))
#Take the index of the winning neuron, it's the neuron that are more similar than the training vector
win_index<-which(matrix_RMSDA==min(matrix_RMSDA), arr.ind=TRUE)
current_index<-list(c(1,1),c(2,1),c(3,1), c(4,1), c(1,2),c(2,2),c(3,2), c(4,2),c(1,3),c(2,3),c(3,3), c(4,3), c(1,4),c(2,4),c(3,4), c(4,4), c(1,4),c(2,4),c(3,4), c(4,4))
#Update of angles of Kohonen Map vectors with the equation (be careful at number of parenthesis)
for (mlist in 1:borne)
{
distance<-as.numeric(dist(rbind(win_index[1,], current_index[[mlist]])))
random_list[[mlist]]<-(random_list[[mlist]]+ ( prot_phipsi[i_row,]-random_list[[mlist]])* (learn_rate*( exp (- ((distance)^2/(2*((learn_radius)^2)) )) ) ))
}
Kohonen_matrix<-matrix(random_list,sqrt(borne),sqrt(borne))
}
}
运行此代码启动后,出现错误:
[1] 1 win_index [1,]中的错误:下标超出范围此外: 有16条警告(请使用warnings()查看)
你能帮我解决吗?
谢谢。谢谢。
答案 0 :(得分:0)
由于错误表明win_index [1,]的索引超出范围,这意味着R无法访问该矩阵的第一行元素,因为它不存在。您应该仔细检查矩阵是否确实存在并且是否已经初始化,因为在您提供的代码片段中找不到该矩阵。
如果您发布正确的代码,我们的外观会更好。
此外,您还会收到其他警告-可能没有问题。如果您只是在R控制台中输入warnings(),那么您会发现那里发生了什么。
另请参见Subscript out of bounds - general definition and solution?。