weight = distances(neighbor_index).^(-1/(m-1));
时间: 2024-04-09 13:31:12 浏览: 10
这行代码计算了权重值,根据先前选择的邻居索引 `neighbor_index` 和距离向量 `distances`。
对于每个邻居,它们的权重通过将其对应的距离值 `distances` 的 (-1/(m-1)) 次幂来计算得到。这里的 `m` 是一个参数。
具体而言,`distances(neighbor_index).^(-1/(m-1))` 这个表达式将 `distances` 向量中选取了与邻居对应的距离值,并将其每个值都取了 (-1/(m-1)) 次幂。
最终得到的权重向量被赋值给变量 `weight`。
相关问题
% originally, this weight calculation should be: % weight = distances(neighbor_index).^(-2/(m-1)); % but since we didn't take sqrt above and the inverse 2th power % the weights are: % weight = sqrt(distances(neighbor_index)).^(-2/(m-1)); % which is equaliavent to:
这段注释解释了权重计算的原始公式和调整后的公式。
原始的权重计算公式应为 `weight = distances(neighbor_index).^(-2/(m-1))`,其中 `m` 是一个参数。这个公式要求先对距离向量 `distances` 进行平方根操作,然后再取倒数的平方。
然而,在之前的代码中并没有进行平方根操作和取倒数的平方,所以需要对权重计算公式进行调整。
调整后的公式为 `weight = sqrt(distances(neighbor_index)).^(-2/(m-1))`,即先对距离向量 `distances` 进行平方根操作,再取倒数的平方。
这两个公式是等价的,只是由于之前的代码没有进行平方根操作,所以使用调整后的公式来保持一致性。
neighbor_index = indeces(1:k_values(k)); weight = ones(1,length(neighbor_index)); if fuzzy, % originally, this weight calculation should be: % weight = distances(neighbor_index).^(-2/(m-1)); % but since we didn't take sqrt above and the inverse 2th power % the weights are: % weight = sqrt(distances(neighbor_index)).^(-2/(m-1)); % which is equaliavent to: weight = distances(neighbor_index).^(-1/(m-1)); % set the Inf (infite) weights, if there are any, to 1. if max(isinf(weight)) % warning(['Some of the weights are Inf for sample: ' ... % num2str(i) '. These weights are set to 1.']); weight(isinf(weight))=1; end end
这段代码根据排序后的索引值 `indeces`,选择前 `k_values(k)` 个邻居的索引,并将其赋值给变量 `neighbor_index`。
接下来,代码创建了一个长度为 `length(neighbor_index)` 的全一向量,并将其赋值给变量 `weight`。
如果 `fuzzy` 变量为真(即非零),则进行模糊权重的计算。在这种情况下,权重的计算公式应为 `weight = distances(neighbor_index).^(-2/(m-1))`,其中 `m` 是一个参数。
然而,由于在之前的计算中没有进行平方根操作,以及倒数的平方操作,权重的计算公式需要做相应的调整。具体来说,权重计算公式变为 `weight = distances(neighbor_index).^(-1/(m-1))`。
此外,代码检查权重是否存在无穷大的情况。如果有,则将这些无穷大的权重设置为1。这一步是为了避免权重中出现无穷大导致后续计算出错。
需要注意的是,原始代码中有一段被注释掉的警告信息,提醒一些样本的权重被设置为1。你可以根据需要取消注释并查看警告信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)