首页 > 解决方案 > 'x' 和 'y' 长度在自定义熵函数中不同

问题描述

我正在尝试学习 R,但我对它的工作方式有疑问。我试图从头开始创建变量的熵函数,p1-p我尝试添加一些ifs 以避免NaN除以 0 时遇到问题。

当我尝试使用绘图的自定义熵时,它可以正常工作,但它会显示NaN我打印结果的时间。但是当我尝试添加ifs 时,它会说:

xy.coords(x, y, xlabel, ylabel, log) 中的错误:“x”和“y”长度不同

entropy <- function(p){

    cat("p = " , p)

    if (p==0 || p==1) {

       result = 0

    }else{

        result = - p*log2(p)-(1-p)*log2((1-p))

    }

    cat("\nresult=",result)

    return(result) 


}

p <- seq(0,1,0.01)

plot(p, entropy(p), type='l', main='Funcion entropia con dos valores posibles')

我不明白它,因为我使用了一个数组的绘图 asx和一个以该数组作为参数的函数 as y,所以它应该是相同的长度,有和没有ifs。

if没有s 的控制台:

p = 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.3 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.4 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59 0.6 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.7 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.8 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1

result= NaN 0.08079314 0.1414405 0.1943919 0.2422922 0.286397 0.3274449 0.3659237 0.4021792 0.4364698 0.4689956 0.499916 0.5293609 0.5574382 0.5842388 0.6098403 0.6343096 0.6577048 0.680077 0.7014715 0.7219281 0.7414827 0.7601675 0.7780113 0.7950403 0.8112781 0.8267464 0.8414646 0.8554508 0.8687212 0.8812909 0.8931735 0.904381​​5 0.9149264 0.9248187 0.9340681 0.9426832 0.9506721 0.958042 0.9647995 0.9709506 0.9765005 0.9814539 0.985815 0.9895875 0.9927745 0.9953784 0.9974016 0.9988455 0.9997114 1 0.9997114 0.9988455 0.9974016 0.9953784 0.9927745 0.9895875 0.985815 0.9814539 0.9765005 0.9709506 0.9647995 0.958042 0.9506721 0.9426832 0.9340681 0.9248187 0.9149264 0.904381​​5 0.8931735 0.8812909 0.8687212 0.8554508 0.8414646 0.8267464 0.8112781 0.7950403 0.7780113 0.7601675 0.7414827 0.7219281 0.7014715 0.680077 0.6577048 0.6343096 0.6098403 0.5842388 0.5574382 0.5293609 0.499916 0.4689956 0.4364698 0.4021792 0.3659237 0.3274449 0.286397 0.2422922 0.1943919 0.1414405 0.08079314 N

带有 s 的控制台if

p = 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.3 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.4 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59 0.6 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.7 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.8 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1 结果= 0xy.coords 中的错误(x,y,xlabel,ylabel,log):“x”和“y”长度不同

标签: rplot

解决方案


您没有创建向量,而是创建了一个标量,因为您没有在 if else 子句中使用向量化功能。您的函数的结果只是一个数字。这应该有效:

entropy <- function(p){

  # initialize a vector of the desired length with zeros
  result <- numeric(length(p))

  # subset the vector for which you want to apply your formula on
  x <- p[!(p %in% c(0,1))]

  # overwrite only those positions for which you want to calculate values based
  # on your formula
  result[!(p %in% c(0,1))] <- - x*log2(x)-(1-x)*log2((1-x))


  #cat("\nresult=",result)

  return(result) 


}

p <- seq(0,1,0.01)

plot(p, entropy(p), type='l', main='Funcion entropia con dos valores posibles')

推荐阅读