【问题标题】:How do I make this partition in my numpy array?如何在我的 numpy 数组中创建这个分区?
【发布时间】:2021-06-01 12:23:51
【问题描述】:

我需要划分这些点。

[ 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 更新。我能做些什么来解决这个问题?

【问题讨论】:

    标签: python numpy networkx


    【解决方案1】:

    在 numpy 中使用 += 以及屏蔽数组部分时,我有时会遇到麻烦。老实说,我不确定原因,或者它们是否会导致您的问题,但请尝试以下方法:

    将第 13 行更改为 broken_parts = broken_parts[broken_parts==0][v] + 2

    先创建单个掩码:

    mask = v && (broken_parts == 0)
    broken_parts[mask] += 2
    

    两者结合。

    【讨论】:

      猜你喜欢
      • 2020-02-12
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 2019-05-26
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 2019-07-09
      相关资源
      最近更新 更多