Skip to main content
 首页 » 编程设计

python之为什么 H2oFrame 忽略我的输入列类型

2025年05月04日2152php

我正在尝试指定进入我的 H2o.Frame 的列类型。在每种情况下,我都尝试过几种不同的方法。单元测试如下。除了最后两个,它们都失败了,但最后两个只能工作,因为我已经将 99.0 更改为 99.9。为什么我不能告诉它 99.0 仍然是一个 float 而不是一个整数?

import unittest 
from unittest import TestCase 
import h2o 
 
class TestInputtingTypes(TestCase): 
    def setUp(self): 
        h2o.init() 
 
    def test_h2o_1(self): 
        data =[(1,'one', 9),(9,'two',3), (8,'three', 99.0)] 
        given_types = {'C1': 'int', 'C2': 'string', 'C3': 'real'} 
        frame = h2o.H2OFrame(data, column_types=given_types) 
        actual_types = frame.types 
 
        self.assertDictEqual(given_types, actual_types) 
 
    def test_h2o_2(self): 
        data =[(1,'one', 9),(9,'two',3), (8,'three', 99.0)] 
        given_types = {'C1': 'int', 'C2': 'string', 'C3': 'real'} 
        names = ['C1', 'C2', 'C3'] 
        frame = h2o.H2OFrame(data, column_types=given_types, column_names=names) 
        actual_types = frame.types 
 
        self.assertDictEqual(given_types, actual_types) 
 
    def test_h2o_3(self): 
        data =[{'C1': 1, 'C2': 'one',   'C3': 9}, 
               {'C1': 9, 'C2': 'two',   'C3': 3}, 
               {'C1': 8, 'C2': 'three', 'C3': 99.0}] 
 
        given_types = {'C1': 'int', 'C2': 'string', 'C3': 'real'} 
        names = ['C1', 'C2', 'C3'] 
        frame = h2o.H2OFrame(data, column_types=given_types, column_names=names) 
        actual_types = frame.types 
 
        self.assertDictEqual(given_types, actual_types) 
 
    def test_h2o_4(self): 
        data =[{'C1': 1, 'C2': 'one',   'C3': 9}, 
               {'C1': 9, 'C2': 'two',   'C3': 3}, 
               {'C1': 8, 'C2': 'three', 'C3': 99.0}] 
 
        given_types = {'C1': 'int', 'C2': 'string', 'C3': 'real'} 
        given_types_input = {'C1': 'numeric', 'C2': 'string', 'C3': 'float'} 
        names = ['C1', 'C2', 'C3'] 
        frame = h2o.H2OFrame(data, column_types=given_types_input, column_names=names) 
        actual_types = frame.types 
 
        self.assertDictEqual(given_types, actual_types) 
 
    def test_h2o_5(self): 
        data =[(1,'one', 9),(9,'two',3), (8,'three', 99.0)] 
        given_types = ['int', 'string', 'real'] 
        names = ['C1', 'C2', 'C3'] 
        frame = h2o.H2OFrame(data, column_types=given_types, column_names=names) 
        actual_types = frame.types 
 
        self.assertDictEqual(given_types, actual_types) 
 
    def test_h2o_6_this_one_passes_because_has_nonzero_decimals(self): 
        data =[(1,'one', 9),(9,'two',3), (8,'three', 99.9)] 
        given_types = {'C1': 'int', 'C2': 'string', 'C3': 'real'} 
        given_types_input = ['int', 'string', 'real'] 
        names = ['C1', 'C2', 'C3'] 
        frame = h2o.H2OFrame(data, column_types=given_types_input, column_names=names) 
        actual_types = frame.types 
 
        self.assertDictEqual(given_types, actual_types) 
 
    def test_h2o_7_this_one_passes_because_has_nonzero_decimals(self): 
        data =[(1,'one', 9),(9,'two',3), (8,'three', 99.9)] 
        given_types = {'C1': 'int', 'C2': 'string', 'C3': 'real'} 
        names = ['C1', 'C2', 'C3'] 
        frame = h2o.H2OFrame(data) 
        actual_types = frame.types 
 
        self.assertDictEqual(given_types, actual_types) 
 
if __name__ == "__main__" : 
    unittest.main() 

请您参考如下方法:

问题是 int 不是您可以传递给 h2o col_types 参数的选项,您需要传递 numeric。

如果您将 numeric 传递给您的 real 和 int 值,那应该可以解决您的问题 - 尽管整数将被转换为 float 。为了 使用 H2O 整数,因此可以将它们映射到分类(使用 .asfactor())

在 H2O 中允许以下类型

“未知”- 这将强制将该列解析为所有 NA

“uuid” - 列中的值必须是真实的 UUID,否则将被解析为 NA

“string” - 强制将列解析为字符串

“数字”- 强制将列解析为数字。 H2O 将以最佳方式处理数字数据的压缩。

“枚举”- 强制将列解析为分类列。

“time” - 强制将列解析为时间列。 H2O 将尝试解析以下日期时间格式列表:(date) “yyyy-MM-dd”, “yyyy MM dd”, “dd-MMM-yy”, “dd MMM yy”, (time) “HH: mm:ss”, “HH:mm:ss:SSS”, “HH:mm:ss:SSSnnnnnn”, “HH.mm.ss” “HH.mm.ss.SSS”, “HH.mm.ss.SSSnnnnnn” ”。时间也可以包含“AM”或“PM”。

您可以在文档中查看更多详细信息:http://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/h2o.html?highlight=import_file#h2o.import_file