Python 如何在二维numpy数组中构造数据
我有集合形式的数据,我想把它转换成2D numpy数组。 数据就像Python 如何在二维numpy数组中构造数据,python,arrays,numpy,2d,Python,Arrays,Numpy,2d,我有集合形式的数据,我想把它转换成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.
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\u len=len(term)
文件长度=长度(文件编号)
因此,假设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
如果相反,只需将模和除法运算转过来。您需要进一步澄清输入以获得帮助您是指a吗?另外,请检查pandas DataFrames:您的假设是正确的,但我没有将模和除法运算用于。事实上,我是python新手。是否用于2D数组标题?2D数组与矩阵相同。因此您有
N
行和M
列。它的尺寸是nxm
。您有一个包含N*M
元素的列表<代码>枚举创建从0到N*M-1的运行索引
。我们希望将该索引映射到列和行索引。因此,模可以让你快速循环索引,而除法的步骤则更慢。谢谢你的解释,但我得到的是ValueError:invalid literal for float():0.0,0.1524,0.0,0.45678 error这是一个包含所有这些数字的字符串吗?如果不确切了解您的数据,就很难提供帮助。如果它是一个字符串,您可以在逗号上拆分它,然后将每个字符串转换为浮点:[float(num)for num in data\u string.split(“,”)
。我希望输出为['TERM''TF-IDF1''TF-IDF2'','TF-IDF11'']['acquire''0.0''0.0'','0.027882503172'','act''0.0'','0.0.0'','0.0'','0.0'','0'']。。。,['year''0.0''0.0'.,'0.0''''.]['yet''0.0''0.0'.,'0.0''''.['york''0.0757230086146''0.0'.,'0.0'''.]
您可以查看我的数据在输出中的情况