【发布时间】:2015-09-30 02:40:44
【问题描述】:
我正在尝试用一个列表构建一个矩阵,然后用 dict 的值填充它。它适用于小数据,但当使用更大的数据(内存不足)时计算机会崩溃。 我的脚本显然太重了,但我不知道如何改进它(第一次编程)。谢谢
import numpy as np
liste = ["a","b","c","d","e","f","g","h","i","j"]
dico = {"a/b": 4, "c/d" : 2, "f/g" : 5, "g/h" : 2}
#now i'd like to build a square array (liste x liste) and fill it up with the values of
# my dict.
def make_array(liste,dico):
array1 = []
liste_i = [] #each line of the array
for i in liste:
if liste_i :
array1.append(liste_i)
liste_i = []
for j in liste:
if dico.has_key(i+"/"+j):
liste_i.append(dico[i+"/"+j])
elif dico.has_key(j+"/"+i):
liste_i.append(dico[j+"/"+i])
else :
liste_i.append(0)
array1.append(liste_i)
print array1
matrix = np.array(array1)
print matrix.shape()
print matrix
return matrix
make_array(liste,dico)
非常感谢您的回答,使用in dico 或列表推导确实提高了脚本的速度,这非常有帮助。
但似乎我的问题是由以下功能引起的:
def clustering(matrix, liste_globale_occurences, output2):
most_common_groups = []
Y = scipy.spatial.distance.pdist(matrix)
Z = scipy.cluster.hierarchy.linkage(Y,'average', 'euclidean')
scipy.cluster.hierarchy.dendrogram(Z)
clust_h = scipy.cluster.hierarchy.fcluster(Z, t = 15, criterion='distance')
print clust_h
print len(clust_h)
most_common = collections.Counter(clust_h).most_common(3)
group1 = most_common[0][0]
group2 = most_common[1][0]
group3 = most_common[2][0]
most_common_groups.append(group1)
most_common_groups.append(group2)
most_common_groups.append(group3)
with open(output2, 'w') as results: # here the begining of the problem
for group in most_common_groups:
for i, val in enumerate(clust_h):
if group == val:
mise_en_page = "{0:36s} groupe co-occurences = {1:5s} \n"
results.write(mise_en_page.format(str(liste_globale_occurences[i]),str(val)))
当使用小文件时,我会得到正确的结果,例如:
联系 a = groupe 2
联系人 b = groupe 2
联系人 c = groupe 2
联系人 d = groupe 2
联系 e = groupe 3
联系 f = groupe 3
但是当使用大量文件时,我每组只能得到一个示例:
联系 a = groupe 2
联系 a = groupe 2
联系 a = groupe 2
联系 a = groupe 2
联系 e = groupe 3
联系 e = groupe 3
【问题讨论】:
-
您能否详细解释一下用列表构建矩阵,然后用dict 的值填充它。?也许展示一个最小的例子!
-
不要使用
has_key它在2.7中被弃用并在3中删除,使用in dico
标签: python arrays list dictionary matrix