我正在尝试指定进入H2o.Frame的列类型。我已经尝试了几种不同的方式,并且在每种情况下。单元测试如下。他们都失败了,除了最后两个,但最后两个只有工作,因为我已经改变了99.0到99.9。为什么我不能告诉它99.0仍然是浮点而不是int?
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()
答案 0 :(得分:3)
问题是int
不是您可以传递给h2o col_types
参数的选项,您需要传递numeric
。
如果你传递numeric
的real和int值,这应该可以解决你的问题 - 虽然整数将被转换为浮点数。对于
使用H2O整数,因此可以将它们映射到分类(使用.asfactor()
)
在H2O中,允许以下类型
“unknown” - 这将强制将列解析为所有NA
“uuid” - 列中的值必须为true UUID或将被解析为NA
“string” - 强制将列解析为字符串
“numeric” - 强制将列解析为数字。 H2O将以最佳方式处理数值数据的压缩。
“enum” - 强制将列解析为分类列。
“time” - 强制将列解析为时间列。 H2O将尝试解析以下日期时间格式列表:(日期)“yyyy-MM-dd”,“yyyy MM dd”,“dd-MMM-yy”,“dd MMM yy”,(时间)“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
答案 1 :(得分:2)
在H2O中,数据存储由H2O优化,因此如果您有一个可以用较少字节存储的列(例如“int”),那么H2O将以这种方式存储它,即使您尝试强制它使用“真实”(或浮动)类型。
为了指定列类型,H2O有自己的词汇表来描述类型(详见Lauren的response),但是当你为第三列指定“numeric”时,你会看到它最终会一个int。
data =[(1,'one', 9),(9,'two',3), (8,'three', 99.0)]
given_types = {'C1': 'int', 'C2': 'string', 'C3': 'numeric'}
frame = h2o.H2OFrame(data, column_types=given_types)
actual_types = frame.types
结果:
In [39]: actual_types
Out[39]: {u'C1': u'int', u'C2': u'string', u'C3': u'int'}
In [40]: given_types
Out[40]: {'C1': 'int', 'C2': 'string', 'C3': 'numeric'}
答案 2 :(得分:0)
在通过传递参数h2o.
而不是()
将文件保存到csv之后,只需尝试使用col_types
import_file
h2o.H2OFrame()
。那解决了我的问题。