我正在学习机器学习。我刚开始接触F#。对于给定的数据集,假设我有两个字符串数组。
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张)。我想使用上述两个数组创建一个这样的数组:
let data_labels = [| //val data_labels : int [] [] [|1; 0; 0|]; //因为第0个scan_data项代表"cat" [|1; 0; 0|]; [|0; 1; 0|]; //因为第2个scan_data项代表"dog" [|0; 1; 0|]; [|0; 1; 0|]; [|0; 0; 1|]; //因为第5个scan_data项代表"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 0let t = scan_data |> Array.map (fun item -> labels |> Array.map (helper item) )