我正在尝试学习机器学习。我是 F# 新手。 对于给定的数据集,假设我有 2 个字符串数组。
let labels = [|"cat"; "dog"; "horse"|]
let scan_data = [|"cat\1.jpg"; "cat\2.jpg"; "dog\1.jpg"; "dog\2.jpg"; "dog\3.jpg"; "horse\1.jpg"; "horse\2.jpg"; "horse\3.jpg"; "horse\4.jpg"; "horse\5.jpg"|]
正如您一定已经猜到的,有 3 个标签(是一种文件夹),其中包含训练图像数据(总共 10 个)。我想使用上面的 2 个数组创建一个像这样的数组:
let data_labels = [| //val data_labels : int [] []
[|1; 0; 0|]; //since 0th scan_data item represent "cat"
[|1; 0; 0|];
[|0; 1; 0|]; //since 2nd scan_data item represent "dog"
[|0; 1; 0|];
[|0; 1; 0|];
[|0; 0; 1|]; //since 5th scan_data item represent "horse"
[|0; 0; 1|];
[|0; 0; 1|];
[|0; 0; 1|];
[|0; 0; 1|];
|]
因此,每当在“scan_data”项中找到子字符串(来自“labels”)匹配时,应该有一个数组将匹配表示为“1”,将不匹配表示为“0”。 关于如何在 F# 中实现此目的的任何想法。
最佳答案
let helper (str1:string) str2 = if str1.Contains(str2) then 1 else 0
let t = scan_data |> Array.map (fun item -> labels |> Array.map (helper item) )
关于arrays - 如何在 F# 中匹配字符串数组中的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49876791/