簡體   English   中英

如何在2D Numpy數組中構造數據

[英]How to construct data in 2D numpy array

我有數據集的形式,我想將其轉換為2D numpy數組。 數據就像

term = which contains the words
document_number= which has the doc number
tf-idf= which contain the tf-idf of each word with respect to doc in ordered manner

我希望它應該像這樣在2D numpy數組中

            doc1    doc2   doc3....
term1        1        5      6
term2        0        4      1
term3        6        8      10
.
.

我應該如何實施?

您對tf-idf的結構的描述不清楚。 因此,我必須對您的數據結構做出一些假設。

term_len = len(term)
doc_len = len(document_number)

因此,假設tf-idf是一個平面列表(而不是列表列表),其中所有文檔中第一項的頻率都在其中,然后是第二項,依此類推。

term_freq = numpy.zeros((term_len, doc_len), dtype=int)
for (i, freq) in enumerate(tf_ids):
    term_freq[i // term_len, i % doc_len] = freq

如果相反的情況成立,則只需將模和除法運算轉過來。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM