[英]Nearest neighbor and pattern recognition with Haskell
Here's a simplified version of the 3 data sets I have: 这是我拥有的3个数据集的简化版本:
Set A = [1, 1, 2, 2, 1, 2, 2, 1]
Set B = [2, 2, 1, 2, 2, 1, 1, 3]
Set C = [8, 4, 4, 4, 4, 9, 8, 4]
Does Haskell have any built in features for finding unspecified patterns between data sets? Haskell是否具有用于在数据集之间查找未指定模式的内置功能? I'd like to run my program over 2 or more data sets, and have it report back which ones are similar, which, in this case, would be sets A and B.
我想在2个或更多数据集上运行我的程序,并让它报告哪些相似,在这种情况下,将是A和B。
If you are not talking about finding consequent intersection. 如果您不是在谈论寻找结果相交处。
For each 2 lists we can use intersect
function from Data.List
, which takes intersection of them. 对于每两个列表,我们可以使用
Data.List
intersect
函数,将它们相交。
So, idea is to calculate intersection of all list and sort them. 因此,想法是计算所有列表的交集并对它们进行排序。
> snd . last . sort $ [ (length $ intersect x y, (x,y)) | let list = [[1,1,2,2,1,2,2,1],[2,2,1,2,2,1,1,3],[8,4,4,4,4,9,8,4]], x <- list, y <- list, x /= y ]
([1,1,2,2,1,2,2,1],[2,2,1,2,2,1,1,3])
If you are interested in founding the consequent intersection, you can use something like that: 如果您有兴趣建立后续的相交点,则可以使用以下方法:
import Data.List (sort, subsequences)
intersectCons :: (Ord a) => [a] -> [a] -> [a]
intersectCons x y = snd . last . sort $
[ (length x1, x1) | x1 <- subsequences x
, x2 <- subsequences y
, x1 == x2 ]
For example: 例如:
> intersectCons [1, 1, 2, 2, 1, 2, 2, 1] [2, 2, 1, 2, 2, 1, 1, 3]
[2,2,1,2,2,1]
Also we can use it for finding most similar pair of lists: 我们也可以使用它来查找最相似的列表对:
> snd . last . sort $ [ (length $ intersectCons x y, (x,y)) | let list = [[1,1,2,2,1,2,2,1],[2,2,1,2,2,1,1,3],[8,4,4,4,4,9,8,4]], x <- list, y <- list, x /= y ]
([2,2,1,2,2,1,1,3],[1,1,2,2,1,2,2,1])
Actually, if you want to get not only one pair of lists, but all pairs that are "similar", you can remove snd . last . sort $
实际上,如果您不仅要获取一对列表,而且要获取所有“相似”的对,则可以删除
snd . last . sort $
snd . last . sort $
snd . last . sort $
and get all of them. snd . last . sort $
并全部获取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.