не может индексировать igraph против вывода с numpy - PullRequest
0 голосов
/ 07 апреля 2020

Я хочу проиндексировать вершины графа списком, сгенерированным numpy. Он генерирует только [], хотя тот же список, указанный явно, работает как положено. Вот пример:

import igraph as ig
import numpy as np

# Create a graph and give the vertices some numerical values, just 0 to start.
t = ig.Graph()
t.add_vertices(10)
t.vs['inf'] = [0]*10
print('the initial list:            ', t.vs['inf'])

# Now choose a few indices and change the values to 1.
choose = [0, 1, 2, 3, 4]
print('the chosen indices:          ', choose)
t.vs[choose]['inf'] = 1
print('indices selected explicitly: ', t.vs['inf'])
# That works as expected.

# Create the same list using numpy.
nums = np.arange(0,10)
npchoose = list(np.where(nums < 5)[0])
print('indices selected via numpy:  ', npchoose)
t.vs[npchoose]['inf'] = 2
print('this has no effect:          ', t.vs['inf'])
# The list appears identical but it does not index the .vs

# The index is actually empty. 
print('just the chosen list, explicitly:  ', t.vs[choose]['inf'])
print('just the chosen list, by numpy:    ', t.vs[npchoose]['inf'])

Вывод:

the initial list:             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
the chosen indices:           [0, 1, 2, 3, 4]
indices selected explicitly:  [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
indices selected via numpy:   [0, 1, 2, 3, 4]
this has no effect:           [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
just the chosen list, explicitly:   [1, 1, 1, 1, 1]
just the chosen list, by numpy:     []

Я в растерянности, потому что хочу выбрать вершины для модификации на основе нескольких условий в других массивах, где numpy .where () - самый удобный инструмент.

1 Ответ

0 голосов
/ 09 апреля 2020

Два решения, которые я придумала, на случай, если у кого-то есть такая же проблема:

  1. Используйте npchoose = np.where(nums < 5)[0].tolist() вместо list(np.where(nums < 5)[0]).

  2. Лучше использовать упорядочение вершин igraph, чем прямое индексирование: t.vs.select(lambda ve: ve.index in npchoose)['inf'] = 2. npchoose отлично работает как массив numpy в лямбде.

...