masking a multidimensional array - numpy - arrays

I have two identical arrays of size (2,3,2)
[[[1, 7],
[2, 8],
[3, 9]],
[[4, 10],
[5, 11],
[6, 12]]]
stored in a multidimensional array
>>> a = np.array([[[[ 1, 1],
[ 7, 7]],
[[ 2, 2],
[ 8, 8]],
[[ 3, 3],
[ 9, 9]]],
[[[ 4, 4],
[10, 10]],
[[ 5, 5],
[11, 11]],
[[ 6, 6],
[12, 12]]]])
>>> a.shape
(2, 3, 2, 2)
I'm trying to mask the sub arrays in a with m:
>>> m = np.array([[[1, 0],
[0, 0],
[1, 1]],
[[1, 1],
[0, 1],
[0, 0]]])
which should result in:
[[[[ 1, 1],
[ 0, 0]
[ 0, 0],
[ 0, 0],
[ 3, 3],
[ 9, 9]]
[[ 4, 4],
[10, 10],
[ 0, 0],
[11, 11],
[ 0, 0],
[ 0, 0]]]
I tried to use np.concatenate and np.append eg, np.prod(np.concatenate([a,m],axis=0))
but none of my solutions worked.

Expand the dimensions of m so it broadcasts with a:
In [183]: a*m[...,None]
Out[183]:
array([[[[ 1, 1],
[ 0, 0]],
[[ 0, 0],
[ 0, 0]],
[[ 3, 3],
[ 9, 9]]],
[[[ 4, 4],
[10, 10]],
[[ 0, 0],
[11, 11]],
[[ 0, 0],
[ 0, 0]]]])

Related

How to insert a number to each array in an array of arrays?

From:
arr1 = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ])
To:
arr1 = np.array([ [0, 1, 2, 3], [0, 4, 5, 6], [0, 7, 8, 9] ])
You can try something like this with numpy.full:
x = 0
new = np.full((arr1.shape[0], arr1.shape[1] + 1), x)
new[:, 1:] = arr1
Output
new
array([[0, 1, 2, 3],
[0, 4, 5, 6],
[0, 7, 8, 9]])
Note that you can assign any value to x.
Your (3,3) 2d array:
In [100]: arr1 = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ])
In [101]: arr1
Out[101]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
A new (3,4) array:
In [102]: np.concatenate((np.zeros((3,1),int),arr1), axis=1)
Out[102]:
array([[0, 1, 2, 3],
[0, 4, 5, 6],
[0, 7, 8, 9]])
Any other (3,1) array (or even a (3,n)) could be added "at the start" like this.

Slice sets of columns in numpy

Consider a numpy array as such:
>>> a = np.array([[1, 2, 3, 0, 1], [2, 3, 2, 2, 2], [0, 3, 3, 2, 2]])
>>> a
array([[1, 2, 3, 0, 1],
[2, 3, 2, 2, 2],
[0, 3, 3, 2, 2]])
And an array which contains couples of column indexes to slice (a specific column can appear in multiple couples):
b = [[0,1], [0,3], [1,4]]
How can I slice/broadcast/stride a using b to get a result as such:
array([[[1, 2],
[2, 3],
[0, 3]],
[[1, 0],
[2, 2],
[0, 2]],
[[2, 1],
[3, 2],
[3, 2]]])
Use b as column indices to subset the array and then transpose the result:
a[:, b].swapaxes(0, 1)
# array([[[1, 2],
# [2, 3],
# [0, 3]],
# [[1, 0],
# [2, 2],
# [0, 2]],
# [[2, 1],
# [3, 2],
# [3, 2]]])

Ruby: match first, second, this etc elements from a dimensional array

I have an array of arrays. I want to concatenate the first, second, third elements of arrays.
Example arrays:
a = [[4, 5, 6], [1, 2, 3], [8, 9, 10]]
a1 = [[1, 2, 3], [8, 9, 10]]
a2 = [[4, 5, 6], [1, 2, 3], [8, 9, 10], [11, 21, 31]]
Output:
out of a: [[4,1,8],[5,2,9],[6,3,10]]
out of a1: [[1,8],[2,9],[3,10]]
out of a2: [[4,1,8,11],[5,2,9,21],[6,3,10,31]]
Use transpose method
a.transpose
=> [[4, 1, 8], [5, 2, 9], [6, 3, 10]]
Array#transpose:
[a, a1, a2].map(&:transpose)
# [
# [[4, 1, 8], [5, 2, 9], [6, 3, 10]],
# [[1, 8], [2, 9], [3, 10]],
# [[4, 1, 8, 11], [5, 2, 9, 21], [6, 3, 10, 31]]
# ]
Whenever Array#transpose can be used so can Enumerable#zip.
a.first.zip *a.drop(1)
#=> [[4,1,8],[5,2,9],[6,3,10]]

A method for an array

What is the most concise and explicit way to write a method for this?
Given an array a of numbers and a number n, find the n consecutive elements of a whose sum is the largest.
Return the largest sum and the index of the first element in the group.
For example, with a = [1, 1, 1, 1, 1, 1, 1, 2] and n = 2, the
result would be a sum 3 and position 6.
arr = [1,3,2,4,3,5,2,1,3,4,2,5,1]
size = 3
Inefficient but pretty
arr.each_cons(size).with_index.map { |a,i| [a.inject(:+), i] }.max_by(&:first)
#=> [12, 3]
Efficient but whupped with an ugly stick1
tot = arr[0,size].inject(:+)
(1..arr.size-size).each_with_object([tot, 0]) do |i, best|
tot += arr[i+size-1] - arr[i-1]
best.replace([tot, i]) if tot > best.first
end
#=> [12, 3]
Steps performed by the pretty one
enum0 = arr.each_cons(size)
#=> #<Enumerator: [1, 3, 2, 4, 3, 5, 2, 1, 3, 4, 2, 5, 1]:each_cons(3)>
enum1 = enum0.with_index
#=> #<Enumerator: #<Enumerator: [1, 3, 2, 4, 3, 5, 2, 1, 3, 4, 2, 5, 1]:
# each_cons(3)>:with_index>
Carefully examine the above return value for enum1. You will see it is effectively a "compound" enumerator. We can see the values that enum1 will generate and pass to map by converting it to an array:
enum1.to_a
#=> [[[1, 3, 2], 0], [[3, 2, 4], 1], [[2, 4, 3], 2], [[4, 3, 5], 3],
# [[3, 5, 2], 4], [[5, 2, 1], 5], [[2, 1, 3], 6], [[1, 3, 4], 7],
# [[3, 4, 2], 8], [[4, 2, 5], 9], [[2, 5, 1], 10]]
Continuing:
b = enum1.map { |a,i| [a.inject(:+), i] }
#=> [[6, 0], [9, 1], [9, 2], [12, 3], [10, 4], [8, 5],
# [6, 6], [8, 7], [9, 8], [11, 9], [8, 10]]
Note the since the first element of enum1 that map passes to the block is [[1, 3, 2], 0], the two block variables are assigned as follows (using parallel or multiple assignment):
a, i = [[1, 3, 2], 0]
#=> [[1, 3, 2], 0]
a #=> [1, 3, 2]
i #=> 0
and the block calculation is performed:
[a.inject(:+), i]
#=> [6, 0]
Lastly,
b.max_by(&:first)
#=> [12, 3]
Enumerable#max_by determines the largest value among
b.map(&:first)
#=> [6, 9, 9, 12, 10, 8, 6, 8, 9, 11, 8]
Steps performed by the less pretty one
a = arr[0,size]
#=> [1, 3, 2]
tot = a.inject(:+)
#=> 6
enum = (1..arr.size-size).each_with_object([tot, 0])
#=> (1..13-3).each_with_object([6, 0])
#=> #<Enumerator: 1..10:each_with_object([6, 0])>
enum.to_a
#=> [[1, [6, 0]], [2, [6, 0]], [3, [6, 0]], [4, [6, 0]], [5, [6, 0]],
# [6, [6, 0]], [7, [6, 0]], [8, [6, 0]], [9, [6, 0]], [10, [6, 0]]]
enum.each do |i, best|
tot += arr[i+size-1] - arr[i-1]
best.replace([tot, i]) if tot > best.first
end
#=> [12, 3]
The first element of enum, [1, [6, 0]], is passed to the block, assigned to the block variables and the block calculation is performed:
i, best = [1, [6, 0]]
#=> [1, [6, 0]]
i #=> 1
best
#=> [6, 0]
tot += arr[i+size-1] - arr[i-1]
# tot = 6 + arr[1+3-1] - arr[1-1]
# = 6 + 4 - 1
# = 9
best.replace([tot, i]) if tot > best.first
#=> best.replace([9, 1]) if 9 > 6
#=> [9, 1]
best
#=> [9, 1]
The remaining calculations are similar.
1 Credit to Bo Diddley (at 2:51)

remove duplicates based on one field in a numpy array

How do I remove duplicates when a numpy array field has duplicates.
for example, i have an array like this:
vals = numpy.array([[1,2,3],[1,5,6],[1,8,7],[0,4,5],[2,2,1],[0,0,0],[5,4,3]])
array([[1, 2, 3],
[1, 5, 6],
[1, 8, 7],
[0, 4, 5],
[2, 2, 1],
[0, 0, 0],
[5, 4, 3]])
i need to remove the duplicates for field [0], so that i got the results like:
([1,2,3],
[0, 4, 5],
[2, 2, 1],
[0, 0, 0],
[5, 4, 3]])
You can use numpy.unique:
In [11]: vals
Out[11]:
array([[1, 2, 3],
[1, 5, 6],
[1, 8, 7],
[0, 4, 5],
[2, 2, 1],
[0, 0, 0],
[5, 4, 3]])
In [12]: unique_keys, indices = np.unique(vals[:,0], return_index=True)
In [13]: vals[indices]
Out[13]:
array([[0, 4, 5],
[1, 2, 3],
[2, 2, 1],
[5, 4, 3]])
To maintain the original order:
In [17]: vals[np.sort(indices)]
Out[17]:
array([[1, 2, 3],
[0, 4, 5],
[2, 2, 1],
[5, 4, 3]])

Resources