Как я могу улучшить сложность этой функции? - PullRequest
0 голосов
/ 14 февраля 2020

Я работаю над проблемой классификации, поэтому я разработал этот хэширующий код:

    # The used packages 

        library("pacman")
        pacman::p_load(dplyr, tidyr, devtools, MASS, pracma, mvtnorm, interval, intervals) 
        pacman::p_load(sprof, RDocumentation, helpRFunctions, foreach , philentropy)       


    # data-matrix

        frids <- data_frame(
          name = c("Nicolas", "Thierry", "Bernard", "Jerome", "peter", "yassine", "karim"),
          age = c(27, 26, 30, 31, 31, 38, 39),
          height = c(180, 178, 190, 185, 187, 160, 158),
          married = c("M", "M", "N", "N", "N", "M", "M")
        )
        i <- Intervals(
          matrix(
            c(0,5000,  
              0,5000,
              7000,10000,  
              7000,10000,
              7000,10000,
              10000,15000,  
              10000,15000
            ),
            byrow = TRUE,
            ncol = 2
          ),
          closed = c( TRUE, TRUE ),
          type = "R"
        )
        frids$salaire = i
        frids


       # The basic function that we will call in the hashing process   

        hash<-function(v,p){

        if(dot(v,p)>0) return(1) else (0)  

        }



    # The hashing process

    LSH<-function(data,K,L){

    # We subset the matrix with only numerical columns df.r

    t<-list.df.var.types(data)
    df.r<-as.matrix(data[c(t$numeric,t$Intervals)])

    # n is the number of rows / observations that we are studying

    n=nrow(df.r)
    print(n)
    print(ncol(df.r))

    # we build a matrix from N(0 ,1 ) law 

    rn=array(rnorm(K*K*L,0,1),c(K,K,L))

      # rd is a matrix that contains integers from 1 to k 
      # it's used to get a combinaison of columns / a subset of the observation in the case we have a high dimension 

rd=unique.array(array(unique(ceiling(runif(K*K*L,0,ncol(df.r)))),c(K,K,L)))

    print(rn)
    print(rd)
    print(df.r)

# Now we build the hashing tables 

    buckets<-array(NA,c(K,n,L)) 
    for (h in 1:L) {
      for (i in 1:K) {
        for (j in 1:n) {
          buckets[i,j,h]<-hash(df.r[j,][rd[,i,h]],rn[,i,h])
        }
      }
    }
      print (buckets)
    return(buckets)
    }

Мой вопрос таков:

Я думаю, что сложность функции L SH: O (LKn) , где k - количество атрибутов, которые я выбрал для хеширования, а n - число наблюдений (строк). L - количество полос / га sh таблиц (размеры = k * n).

Как я могу улучшить производительность сложности или время исключения функции L SH?

Например:

Если n = 1.000.0000 наблюдений вместо n = 7, сколько времени потребуется для исключения?

Выполнение пример: z1=LSH(frids,3,2)

K = 3; для каждой итерации мы берем три столбца между (возраст, рост, salaire.1, salaire.2).

L = 2; Всего мы строим 2 хеш-таблицы. Таблицы состоят из 0 & 1.

enter image description here enter image description here

Пример вычисления:

В последней таблице хеширования первый элемент во второй строке:

(i , j , h ) = (2 , 1 , 2 )

buckets [i , j , h ]=buckets [2 , 1 , 2]

buckets [i , j , h]=hash( df.r[j,]rd[,i,h] ; rn[,i,h]] )

=hash( df.r[1, c(3,4,2)] ; c(-2.99 , 0.64 , 0.51 ) )

= hash( dot ( c(0 , 5000 , 180) ; c(-2.99 , 0.64 , 0.51) )

=1 

Извините, но мои знания в этой области ограничены.

Заранее спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...