arrays - 如何在 F# 中匹配字符串数组中的子字符串

标签 arrays string machine-learning f# functional-programming

我正在尝试学习机器学习。我是 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/

相关文章:

python - 执行 XGBClassifier 的增量学习

python - Keras,形状不兼容

python - 操作系统错误: Version mismatch while installing h2o?

c++ - 将整数数组转换为数字

c++ - C++ 中的数组排序问题

.net - C++ 中的 BSTR 和 SysAllockStringByteLen()

java - Libgdx 如何显示文本?

c++ - 如何在另一个函数中访问在一个函数中定义和声明的数组?

java - 在 Java 中确定数组的长度(初学者)

java - 为什么这些字符串的 str == str.intern() 的结果不同?