从数据 0,1,2,2,2,3,111,2,333,4,3,2 中找到 111 和 333 这两个异常值
const outlierDetector = (collection) => {
const size = collection.length;
let q1, q3;
if (size < 2) {
return collection;
}
const sortedCollection = collection.slice().sort((a, b) => a - b);
if (((size - 1) / 4) % 1 === 0 || (size / 4) % 1 === 0) {
q1 =
(1 / 2) *
(sortedCollection[Math.floor(size / 4) - 1] +
sortedCollection[Math.floor(size / 4)]);
q3 =
(1 / 2) *
(sortedCollection[Math.ceil((size * 3) / 4) - 1] +
sortedCollection[Math.ceil((size * 3) / 4)]);
} else {
q1 = sortedCollection[Math.floor(size / 4)];
q3 = sortedCollection[Math.floor((size * 3) / 4)];
}
const iqr = q3 - q1;
const maxValue = q3 + iqr * 1.5;
return sortedCollection.filter((value) => value >= maxValue);
};
let a = [1,2,3,1,2,111,22,2,1,2]
let b = [1,4,7,20,3,5,12,33]
let c = [1,4,7,20,3,5,12,33,50,100]
outlierDetector(a);
outlierDetector(b);
outlierDetector(c);
上一篇
JavaScript 动态时间规整 (DTW) 算法实现
JavaScript 实现的动态时间规整 (DTW) 算法,用于计算两个时间序列(尤其是不同长度的序列)的相似度。该算法在语音识别、手势识别等领域有广泛应用。
下一篇
使用 aeneas 进行音文对齐
这篇笔记记录了如何使用 aeneas 库进行音文对齐。aeneas 是一个用于音文对齐的 Python 库。Github 地址:https://github.com/readbeyond/aeneas 安装文档:https://github.com/readbeyond/aeneas/blob/master/wiki/INSTALL.md