【发布时间】:2014-11-07 05:17:42
【问题描述】:
我在一个 3.0GB 的 CSV 文件中有 292 万个数据点,我需要循环两次以创建一个要加载到 NetworkX 中的图形。按照目前的速度,我需要几天的时间来生成这个图表。如何加快速度?
similarity = 8
graph = {}
topic_pages = {}
CSV.foreach("topic_page_node_and_edge.csv") do |row|
topic_pages[row[0]] = row[1..-1]
end
CSV.open("generate_graph.csv", "wb") do |csv|
i = 0
topic_pages.each do |row|
i+=1
row = row.flatten
topic_pages_attributes = row[1..-1]
graph[row[0]] = []
topic_pages.to_a[i..-1].each do |row2|
row2 = row2.flatten
topic_pages_attributes2 = row2[1..-1]
num_matching_attributes = (topic_pages_attributes2 & topic_pages_attributes).count
if num_matching_attributes >= similarity or num_matching_attributes == topic_pages_attributes2.count or num_matching_attributes == topic_pages_attributes.count
graph[row[0]].push(row2[0])
end
end
csv << [row[0], graph[row[0]]].flatten
end
end
【问题讨论】:
-
向我们展示您的代码?
-
@theTinMan 添加了代码。谢谢。
-
那台机器上有多少可用内存?您试图在内存中保存 2.92M 数据点,并且每个点不占用一个字节。
-
您真的需要绘制所有 3e6 点吗?一个不错的随机样本应该可以为您提供足够好的眼球图表。
-
您能否至少将数据存入数据库,在数据库中进行所需的任何准备工作,然后从那里转储 CSV? 3e6 行对于一个像样的数据库来说不算什么。
标签: python ruby graph cluster-analysis networkx