问题描述
我需要对这些点进行分区。
[ 1] H = G.copy() # networkx graph
[ 2] broken_parts = np.zeros((r*c)) # r,c=10 in this case
[ 3]
[ 4] v = np.where(data[:,0]<0)[0]
[ 5] broken_parts[v] += 1 # this successfully sets each entry in those specified locations to 1
[ 6]
[ 7] H.remove_nodes_from(v) # removes the prevIoUsly selected nodes
[ 8] H = nx.convert_node_labels_to_integers(H)
[ 9]
[10] L,data1 = G_to_Coordinates(H,norm=True) # revises the data excluding the prevIoUsly selected values
[11]
[12] v = np.where(data1[:,0]<0)[0] # finds the next partition
[13] broken_parts[broken_parts==0][v] += 2 # fails here,but intended to set the partition in broken_parts as 2
第 [12] 行成功找到数组位置,但 broken_parts
没有更新为 2s。我该怎么做才能解决这个问题?
解决方法
我有时在 numpy 中使用 +=
时遇到问题,也在屏蔽数组部分时遇到问题。老实说,我不确定原因,或者它们是否导致了您的问题,但请尝试以下操作:
将第 13 行更改为 broken_parts = broken_parts[broken_parts==0][v] + 2
先创建单个掩码:
mask = v && (broken_parts == 0)
broken_parts[mask] += 2
两者结合。