如何为 MLDataTable 中的列定义/更改 MLDataValue.ValueType
How to define / change MLDataValue.ValueType for a column in MLDataTable
我正在从给定的 .csv 文件加载 MLDataTable
。根据输入文件的内容自动推断每列的数据类型。
当我稍后处理 table 时,我需要 predictable,显式类型。
如何在加载文件时强制执行某种类型或在第二步中更改类型?
简化示例:
import Foundation
import CreateML
// file.csv:
//
// value1,value2
// 1.5,1
let table = try MLDataTable(contentsOf:URL(fileURLWithPath:"/path/to/file.csv"))
print(table.columnTypes)
// actual output:
// ["value2": Int, "value1": Double] <--- type for value2 is 'Int'
//
// wanted output:
// ["value2": Double, "value1": Double] <--- how can I make it 'Double'?
使用 MLDataColumn
的 map(to:)
方法从现有列派生具有所需基础类型的新列:
let squaresArrayInt = (1...5).map{[=10=] * [=10=]}
var table = try! MLDataTable(dictionary: ["Ints" : squaresArrayInt])
print(table)
let squaresColumnDouble = table["Ints"].map(to: Double.self)
table.addColumn(squaresColumnDouble, named: "Doubles")
print(table)
产生以下输出:
Columns:
Ints integer
Rows: 5
Data:
+----------------+
| Ints |
+----------------+
| 1 |
| 4 |
| 9 |
| 16 |
| 25 |
+----------------+
[5 rows x 1 columns]
Columns:
Ints integer
Doubles float
Rows: 5
Data:
+----------------+----------------+
| Ints | Doubles |
+----------------+----------------+
| 1 | 1 |
| 4 | 4 |
| 9 | 9 |
| 16 | 16 |
| 25 | 25 |
+----------------+----------------+
[5 rows x 2 columns]
我正在从给定的 .csv 文件加载 MLDataTable
。根据输入文件的内容自动推断每列的数据类型。
当我稍后处理 table 时,我需要 predictable,显式类型。
如何在加载文件时强制执行某种类型或在第二步中更改类型?
简化示例:
import Foundation
import CreateML
// file.csv:
//
// value1,value2
// 1.5,1
let table = try MLDataTable(contentsOf:URL(fileURLWithPath:"/path/to/file.csv"))
print(table.columnTypes)
// actual output:
// ["value2": Int, "value1": Double] <--- type for value2 is 'Int'
//
// wanted output:
// ["value2": Double, "value1": Double] <--- how can I make it 'Double'?
使用 MLDataColumn
的 map(to:)
方法从现有列派生具有所需基础类型的新列:
let squaresArrayInt = (1...5).map{[=10=] * [=10=]}
var table = try! MLDataTable(dictionary: ["Ints" : squaresArrayInt])
print(table)
let squaresColumnDouble = table["Ints"].map(to: Double.self)
table.addColumn(squaresColumnDouble, named: "Doubles")
print(table)
产生以下输出:
Columns:
Ints integer
Rows: 5
Data:
+----------------+
| Ints |
+----------------+
| 1 |
| 4 |
| 9 |
| 16 |
| 25 |
+----------------+
[5 rows x 1 columns]
Columns:
Ints integer
Doubles float
Rows: 5
Data:
+----------------+----------------+
| Ints | Doubles |
+----------------+----------------+
| 1 | 1 |
| 4 | 4 |
| 9 | 9 |
| 16 | 16 |
| 25 | 25 |
+----------------+----------------+
[5 rows x 2 columns]