I recently posted a question here which was answered exactly as I asked. However, I think I overestimated my ability to manipulate the answer further. I read the broadcasting doc, and followed a few links that led me way back to 2002 about numpy broadcasting.
I've used the second method of array creation using broadcasting:
N = 10
out = np.zeros((N**3,4),dtype=int)
out[:,:3] = (np.arange(N**3)[:,None]/[N**2,N,1])%N
which outputs:
[[0,0,0,0]
[0,0,1,0]
...
[0,1,0,0]
[0,1,1,0]
...
[9,9,8,0]
[9,9,9,0]]
but I do not understand via the docs how to manipulate that. I would ideally like to be able to set the increments in which each individual column changes.
ex. Column A changes by 0.5 up to 2, column B changes by 0.2 up to 1, and column C changes by 1 up to 10.
[[0,0,0,0]
[0,0,1,0]
...
[0,0,9,0]
[0,0.2,0,0]
...
[0,0.8,9,0]
[0.5,0,0,0]
...
[1.5,0.8,9,0]]
Thanks for any help.
You can adjust your current code just a little bit to make it work.
The changes are:
int
dtype on the array, since we need it to hold floats in some columns. You could specify afloat
dtype if you want (or even something more complicated that only allows floats in the first two columns).N**3
total values, figure out the number of distinct values for each column, and multiply them together to get our total size. This is used for bothzeros
andarange
.//
operator in the first broadcast operation because we want integers at this point, but later we'll want floats.A,B,C
numbers of values, divide byB*C, C, 1
).%
operation to match the bounds on each column.This small example helps me understand what is going on:
So we generate a range of numbers (0 to 7) and divide them by 4,2, and 1.
The rest of the calculation just changes each value without further broadcasting
Apply
%N
to each elementAssigning to an
int
array is the same as converting the floats to integers: