rearange numpy multilevel array by column values - numpy

i wish to rearange numpy array by value of one of its columns. Meaning -
[[0, 0], [583, 0], [1166, 0], [1365, 0], [0, 583], [583, 583], [1166, 583], [1365, 583]]
to
[[[0, 0], [583, 0], [1166, 0], [1365, 0]],
[[0, 583], [583, 583], [1166, 583], [1365, 583]]]
I currently reshape it with "hard coded" shape, but I wish to do it automaticaly.

Here is the code which covers the case of variable number of second row values:
X = np.array([[0, 0], [583, 0], [1166, 0], [1365, 0], [0, 583], [583, 583], [1166, 583], [1365, 583],
[0, 1], [581, 1], [1166, 0], [1365, 0], [3, 1], [2, 583], [1166, 583], [1365, 1]])
out = []
vs = set(X[:,-1])
for v in vs:
idxs = X[:,-1] == v
out.append(np.sort(X[idxs],axis=0))
out = np.array(out)

You can use numpy reshape for reshaping this array
import numpy as np
array = np.array([[0, 0], [583, 0], [1166, 0], [1365, 0], [0, 583], [583, 583], [1166, 583], [1365, 583]])
array.reshape(2, 4,2)
output:
array([[[ 0, 0],
[ 583, 0],
[1166, 0],
[1365, 0]],
[[ 0, 583],
[ 583, 583],
[1166, 583],
[1365, 583]]])
For detail visit here
Thanks

Related

Tensorflow.js: tf.pad results in TypeError: t.map is not a function

This code is from the TF API docs:
let t = tf.tensor([[1, 2, 3], [4, 5, 6]])
let padding = tf.tensor([[1, 1,], [2, 2]])
When I execute it:
tf.pad(t, padding, "CONSTANT")
I get:
TypeError: t.map is not a function
I'm using the latest version of tfjs.
padding is a normal js array of tuples ( array of arrray) and not a tensor.
As for now, the version 1.3.1, only the CONSTANT mode is supported. Here is the way to go:
let t = tf.tensor([[1, 2, 3], [4, 5, 6]])
let padding = [[2, 2,], [1, 1]]
tf.pad(t, padding).print()
// [[0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0],
// [0, 1, 2, 3, 0],
// [0, 4, 5, 6, 0],
// [0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0]]

Random valid data items in numpy array

Suppose I have a numpy array as follows:
data = np.array([[1, 3, 8, np.nan], [np.nan, 6, 7, 9], [np.nan, 0, 1, 2], [5, np.nan, np.nan, 2]])
I would like to randomly select n-valid items from the array, including their indices.
Does numpy provide an efficient way of doing this?
Example
data = np.array([[1, 3, 8, np.nan], [np.nan, 6, 7, 9], [np.nan, 0, 1, 2], [5, np.nan, np.nan, 2]])
n = 5
Get valid indices
y_val, x_val = np.where(~np.isnan(data))
n_val = y_val.size
Pick random subset of size n by index
pick = np.random.choice(n_val, n)
Apply index to valid coordinates
y_pick, x_pick = y_val[pick], x_val[pick]
Get corresponding data
data_pick = data[y_pick, x_pick]
Admire
data_pick
# array([2., 8., 1., 1., 2.])
y_pick
# array([3, 0, 0, 2, 3])
x_pick
# array([3, 2, 0, 2, 3])
Find nonzeros by :
In [37]: a = np.array(np.nonzero(data)).reshape(-1,2)
In [38]: a
Out[38]:
array([[0, 0],
[0, 0],
[1, 1],
[1, 1],
[2, 2],
[2, 3],
[3, 3],
[3, 0],
[1, 2],
[3, 0],
[1, 2],
[3, 0],
[2, 3],
[0, 1],
[2, 3]])
Now pick a random choice :
In [44]: idx = np.random.choice(np.arange(len(a)))
In [45]: data[a[idx][0],a[idx][1]]
Out[45]: 2.0

is there an numpy function that will return an array with different combinations of the original?

For example if my array was
(2,2)
array([[1, 0],
[0, 1]])
I would want it to return:
(4,2,2)
array([[[0, 0],
[0, 1]],
[[1, 1],
[0, 1]],
[[1, 0],
[1, 1]],
[[1, 0],
[0, 0]]])
You can flip binary number at a time using:
(np.identity(inp.size, int)^inp.ravel()).reshape(-1, *inp.shape)
or more verbose but also more economical:
>>> out = np.empty(2*(inp.size,), inp.dtype)
>>> out[...] = inp.ravel()
>>> np.einsum('ii->i', out)[...]^=1
>>> out = out.reshape(-1, *inp.shape)

tensorflow manipulate labels vector into "multiple hot encoder"

is it possible (in a nice way, that is) in tensorflow to achieve the next functionality:
assume we have a dense vector of tags
labels = [0,3,1,2,0]
I need to make a "multiple hot encoder" of it. meaning, for each row I need 1's up to the index of the label minus 1
so the required result will be
[[0, 0, 0],
[1, 1, 1],
[0, 0, 1],
[0, 1, 1],
[0, 0, 0]]
thanks
You could do this using tf.nn.embeddings_lookup as shown here:
embeddings = tf.constant([[0,0,0], [0,0,1], [0,1,1], [1,1,1]])
labels = [0,3,1,2,0]
encode_tensors = tf.nn.embedding_lookup(embeddings,labels)
Output of sess.run(encode_tensors) :
array([[0, 0, 0],
[1, 1, 1],
[0, 0, 1],
[0, 1, 1],
[0, 0, 0]], dtype=int32)
Hope this helps !
for completion:
it's also possible to use:
In [397]: labels = np.array([1, 2, 0, 3, 0])
In [398]: sess.run(tf.sequence_mask(labels, 3, dtype=tf.int8))
Out[398]:
array([[1, 0, 0],
[1, 1, 0],
[0, 0, 0],
[1, 1, 1],
[0, 0, 0]], dtype=int8)
the result matrix will be reversed from what I asked though

Numpy `Broadcast` array

I would like to do a transformation like dimshuffle in theano using numpy.
Example input:
np.array([[1, 0, 0], [1, 0, 0]])
Example output:
np.array([
[[1, 0, 0], [1, 0, 0]],
[[1, 0, 0], [1, 0, 0]],
[[1, 0, 0], [1, 0, 0]]
])
I don't know what dimshuffle does, but the output can be produced with repeat
In [319]: np.repeat(np.array([[1, 0, 0], [1, 0, 0]])[None,:,:],3,axis=0)
Out[319]:
array([[[1, 0, 0],
[1, 0, 0]],
[[1, 0, 0],
[1, 0, 0]],
[[1, 0, 0],
[1, 0, 0]]])
The input is 2d (2,3), so I have to add an axis - output is (3,2,3). tile would work, so would indexing, or even:
A=np.array([[1, 0, 0], [1, 0, 0]])
np.array([A,A,A])