例は物事を簡単に伝えます:
import numpy
# ------------------------------------------------------------------------
# Edit:
# commenting out below `a` assignation for the more general case as shown
#+below this commented block
# ------------------------------------------------------------------------
# a = np.array(range(8))
# print a
# array([0, 1, 2, 3, 4, 5, 6, 7])
# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
a = np.random.randn(8)
print a
array([-0.53683985, -0.321736 , 0.15684836, 0.32085469, 1.99615701,
-1.16908367, -0.10995894, -1.90925978])
b = [4, 7]
# ^ ^ These values are indices of values in `a` I want to keep unchanged
# I want to set all values to,
# say np.random.random_integers(10, 100) or simply `nan` except for indices given by `b`:
# So I want something like this:
a[: (!b)] = np.random.random_integers(10, 100) # I'm using "!" as the NOT operator
print a
array([62, 96, 47, 74, 1.99615701, 32, 11, -1.90925978])
# not changed: ^^^^^^^^^^ ^^^^^^^^^^
# or:
a[: (!b)] = np.nan
print a
array([nan, nan, nan, nan, 1.99615701, nan, nan, -1.90925978])
# not changed: ^^^^^^^^^^ ^^^^^^^^^^
np.ma.array(a, mask = False) と a.mask[b] = True を使用できることはわかっていますが、この時点から、乱数をマスクされていない値のみに割り当てる方法がわかりません