2017-07-19 150 views
1

我發現我可以從glrm_model得到特徵向量的集合(H2O廣義低階型號Estimateor glrm(對不起,我不能把這個標籤))這樣說:H2OTwoDimTable似乎缺少功能

EV = glrm_model._model_json [「output」] ['eigenvectors'])

但是,EV的類型是H2OTwoDimTable,它不是很有能力。

如果我嘗試做(其中M爲H2O數據幀):

M.mult(EV) 

我得到的錯誤

AttributeError: 'H2OTwoDimTable' object has no attribute 'nrows' 

如果我嘗試EV轉換爲numpy的矩陣:

EV.as_matrix() 

我得到的錯誤:

AttributeError: 'H2OTwoDimTable' object has no attribute 'as_matrix' 

我可以EV轉換爲熊貓數據幀,然後將其轉換爲numpy的矩陣,這是一個額外的步驟,並執行矩陣乘法

IMHO,這將是更好的,如果特徵向量的參考返回H2O數據幀。

另外,如果H2OTwoDimTable能夠更好地支持矩陣乘法作爲左操作數或右操作數,那將是一件好事。

而EV.as_data_frame()沒有use_pandas = False選項。

這裏是Python代碼可能被修改,以更好地支持矩陣型件事:「TwoDimTable」類是用來存儲模型中的輕量級表格數據

https://github.com/h2oai/h2o-3/blob/master/h2o-py/h2o/two_dim_table.py

+1

如果文檔非常糟糕,請嘗試dir(EV)以查看屬性...如果未記錄某些內容,則庫的開發人員可能會在下一版本中更改它。嘗試dir(EV)後始終檢查文檔。 –

回答

1

。我與你有關使用H2OFrames而不是TwoDimTables的協議,但它是很久以前做出的設計選擇(現在不能改變它)。

由於H2OFrames可以包含非數字數據,所以有一個從H2OFrame或TwoDimTable到Pandas DataFrame的.as_data_frame()方法。所以你可以鏈接.as_data_frame().as_matrix()得到一個矩陣(numpy.ndarray),如果這就是你要找的。這裏有一個例子:

import h2o 
from h2o.estimators.glrm import H2OGeneralizedLowRankEstimator 

h2o.init() 

data = h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/glrm_test/cancar.csv") 

# Train a GLRM model with recover_svd=True to keep eigenvectors 
glrm = H2OGeneralizedLowRankEstimator(k=4, 
             transform="NONE", 
             loss="Quadratic", 
             regularization_x="None", 
             regularization_y="None", 
             max_iterations=1000, 
             recover_svd=True) 
glrm.train(x=data.names, training_frame=data) 

# Get eigenvector TwoDimTable from the model 
EV = glrm._model_json["output"]['eigenvectors'] 

# Convert to various formats 
evdf = EV.as_data_frame() #pandas.core.frame.DataFrame 
evmat = evdf.as_matrix() #numpy.ndarray 

# or directly 
evmat = EV.as_data_frame().as_matrix() 

如果你有興趣加入.as_matrix()方法將TwoDimTable類,你可以提交一份關於h2o-3 repo對於pull請求。我認爲這將是一個有用的擴展。有關於如何在我們的contributing guide中貢獻H2O的更多信息。