I have a 4x2 and a 2x2 matrices. I would like to loop each combination of rows (vectors of dimension 2) through a function foo using vectorize.
Here are the matrices:
X = np.array([[1, 0], [2, 0], [3, 0], [4,0]])
Y = np.array([[1, 0], [2, 0]])
Here's how I'm trying to run it:
def foo(x, y):
print("inputs:", x, y)
return x[0] * y[0]
bar = np.vectorize(foo, signature="???")
output = bar(X, Y)
print(output)
I'm looking for the following output. bar would return a 4x2 matrice:
inputs: [1,0] [1,0]
inputs: [1,0] [2,0]
inputs: [2,0] [1,0]
inputs: [2,0] [2,0]
inputs: [3,0] [1,0]
inputs: [3,0] [2,0]
inputs: [4,0] [1,0]
inputs: [4,0] [2,0]
[[1,2], [2,4], [3,6], [4,8]]
I've tried various combinations of signature, but I'm just not grokking how to use it given the output I'm looking for.
NB: I am aware vectorize just uses Python for loops under the hood and offers no performance benefit. I just want to understand how to use it.
The basic use of vectorize broadcasts the inputs against each other, and passes scalar tuples to your function. A (4,2) can't broadcast with a (2,2). signature is an addition that should make it possible to pass "rows" of your arrays. It's even slower, and I haven't see it used much (or recommended it).
In [536]: bar = np.vectorize(foo, signature="(n),(n)->()")
In [533]: bar(X,Y[0,:])
inputs: [1 0] [1 0]
inputs: [2 0] [1 0]
inputs: [3 0] [1 0]
inputs: [4 0] [1 0]
Out[533]: array([1, 2, 3, 4])
In [537]: bar(X[:,None],Y[None])
inputs: [1 0] [1 0]
inputs: [1 0] [2 0]
inputs: [2 0] [1 0]
inputs: [2 0] [2 0]
inputs: [3 0] [1 0]
inputs: [3 0] [2 0]
inputs: [4 0] [1 0]
inputs: [4 0] [2 0]
Out[537]:
array([[1, 2],
[2, 4],
[3, 6],
[4, 8]])
So this gives bar a (4,1,2) and (1,2,2); which broadcast as (4,2,2). Or with this signature it's broadcasting a (4,1) with 1,2) => (4,2). It's the signature that determines how the last dimensions match.
It may in some cases be convenient, but I wouldn't recommend devoting too much time to understanding vectorize.
Related
I have this array and I want to return unique array combinations. I tried meshgrid but it creates duplicates and inverse array values
>> import numpy as np
>> array = np.array([0,1,2,3])
>> combinations = np.array(np.meshgrid(array, array)).T.reshape(-1,2)
>> print(combinations)
[[0 0]
[0 1]
[0 2]
[0 3]
[1 0]
[1 1]
[1 2]
[1 3]
[2 0]
[2 1]
[2 2]
[2 3]
[3 0]
[3 1]
[3 2]
[3 3]]
What I want to exclude are the repeating arrays: [0,0] [1,1] [2,2] [3,3] and the inverse arrays when [2,3] is returned exclude [3,2] in the output.
Take a look at this combination calculator, this is the output that I like but how can I create it in NumPy?
you could use combinations from itertools
import numpy as np
from itertools import combinations
array = np.array([0,1,2,3])
combs = np.array(list(combinations(arr, 2)))
I have an array of points, and I want to split these into two arrays by the second dimension:
points_right = points[points[:, 0] > p0[0]]
points_left = points[points[:, 0] < p0[0]]
how can I split these points in one loop?
I think np.split is what you're looking for, just use axis=1.
Example splitting a 2x4 matrix:
import numpy as np
pts = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
left_pts, right_pts = np.split(pts, indices_or_sections=2, axis=1)
The original matrix (pts):
[[1 2 3 4]
[5 6 7 8]]
left_pts:
[[1 2]
[5 6]]
right_pts:
[[3 4]
[7 8]]
https://numpy.org/doc/stable/reference/generated/numpy.split.html
I want to create an array made of arrays in Python with numpy
I'm trying to calcule the inverse of a matrix made by some other matrix using numpy method linalg.inv() but it calculates one inverse for each submatrix instead of a general inverse
for example, lets say I have:
a = np.array([[1, 2],
[3, 4]])
b = np.array([[5, 6],
[7, 8]])
i = np.array([[1, 0],
[0, 1]])
what I've tried is:
c = np.array([[a, i],
[i, b]])
what I want is
>> [[1, 2, 1, 0]
[3, 4, 0, 1]
[1, 0, 5, 6]
[0, 1, 7, 8]]
what I get is
>> [[[[1 2]
[3 4]]
[[1 0]
[0 1]]]
[[[1 0]
[0 1]]
[[5 6]
[7 8]]]]
You can use the np.block function, which can be used to assemble a block of matrices. You can do something like this,
np.block([[a,i],[i,b]])
Actually, I'm doing the homework "Art Generation with Neural Style Transfer" of deeplearning.ai on coursera. In the function compute_layer_style_cost(a_S, a_G):
a_S = tf.reshape(a_S, [n_H*n_W, n_C])
a_G = tf.reshape(a_G, [n_H*n_W, n_C])
GS = gram_matrix(tf.transpose(a_S))
GG = gram_matrix(tf.transpose(a_G))
Why does this code give the right answer, however, the following doesn't:
a_S = tf.reshape(a_S, [n_C, n_H*n_W])
a_G = tf.reshape(a_G, [n_C, n_H*n_W])
GS = gram_matrix(a_S)
GG = gram_matrix(a_G)
Here's a trivial example that shows the difference between these two expressions:
import tensorflow as tf
tf.InteractiveSession()
x = tf.range(0, 6)
a = tf.reshape(x, [3, 2])
b = tf.transpose(tf.reshape(x, [2, 3]))
print(x.eval())
print(a.eval())
print(b.eval())
The result:
[0 1 2 3 4 5]
[[0 1]
[2 3]
[4 5]]
[[0 3]
[1 4]
[2 5]]
As you can notice, a and b are different, though have the same shape. That's because the first reshaping "splits" x into [0 1], [2 3] and [4 5], while the second reshaping into [0 1 2] and [3 4 5].
I have a numpy product generator (using meshgrid) which finds the product of two arrays (similar to itertools.product). The problem is that it generates arrays which contain the same elements, but rearranged (thus numpy.unique doesn't filter them).
For example, if I have an array like this:
[[0, 0]
[1, 0]
[0, 1]
[1, 1]]
I would need a result like this:
[[0, 0]
[1, 0]
[1, 1]]
Since [1, 0] and [0, 1] are the same for my purposes.
If you have numpy >= 1.13.0, you can use np.unique on previously sorted array:
>>> a = np.array([[0, 0], [1, 0], [0, 1], [1, 1]])
>>> a
[[0 0]
[1 0]
[0 1]
[1 1]]
>>> b = np.unique(np.sort(a, axis=1), axis=0)
>>> b
[[0 0]
[0 1]
[1 1]]