0

我正在MLDataTable从给定的 .csv 文件加载一个。根据输入文件的内容自动推断每列的数据类型。
稍后处理表格时,我需要可预测的显式类型。

如何在加载文件时强制执行某种类型,或者在第二步中更改类型?

简化示例:

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'?
4

1 回答 1

2

使用MLDataColumn'smap(to:)方法从具有所需基础类型的现有列派生新列:

let squaresArrayInt = (1...5).map{$0 * $0}
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]
于 2018-11-16T13:38:10.537 回答