Issue
Having trouble manipulating a 2D numpy array (below). I'm using np.where to add a value if the given condition is met, but doesn't seem to be working. Is this simply not possible or am I making a simple mistake?
a = ['circle','square']
b=['north','south']
c=['red','blue']
d=['long','short']
x = []
for shape in a:
for direction in b:
for color in c:
for length in d:
x.append([shape, direction, color, length, 0])
x = np.array(x)
# conditional add - if condition is met, add 1 to numeric index, else assign value of 2
x[:,4] = np.where((x[:,0]=='circle') & (x[:,1]=='south'), x[:,4]+1, 2)
Solution
A solution would be to use indices instead of labels when creating your numpy array; that way it would be a numerical array and your last statement would work:
from itertools import product
import numpy as np
parameters = [
['circle', 'square'],
['north', 'south'],
['red', 'blue'],
['long', 'short'],
]
x = np.array(list(product(*map(lambda my_list: range(len(my_list)), parameters), [0])))
x[:,4] = np.where((x[:,0]==0) & (x[:,1]==1), x[:,4]+1, 2)
Answered By - Swifty
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.