This question already has answers here:
Quick way to upsample numpy array by nearest neighbor tiling [duplicate]
(3 answers)
Closed 2 years ago.
Suppose I have a numpy array that looks like this
[[0 1]
[2 3]]
How do I pad the values from the centers to produce a one like
[[0 0 0 1 1 1]
[0 0 0 1 1 1]
[0 0 0 1 1 1]
[2 2 2 3 3 3]
[2 2 2 3 3 3]
[2 2 2 3 3 3]
np.pad is your friend here. The mode=edge argument makes sure that the padded values are taken from the closest element in the original array:
np.pad([[0, 1], [2, 3]], 2, mode='edge')
>>> array([[0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1],
[2, 2, 2, 3, 3, 3],
[2, 2, 2, 3, 3, 3],
[2, 2, 2, 3, 3, 3]])
Related
I have a Sparse Tensor as follows:
st = tf.sparse.from_dense([[1, 0, 2, 5], [3, 0, 0, 4], [0, 0, 0, 0], [1, 1, 3, 0], [1, 2, 2, 2]])
print(st)
SparseTensor(indices=tf.Tensor(
[[0 0]
[0 2]
[0 3]
[1 0]
[1 3]
[3 0]
[3 1]
[3 2]
[4 0]
[4 1]
[4 2]
[4 3]], shape=(12, 2), dtype=int64), values=tf.Tensor([1 2 5 3 4 1 1 3 1 2 2 2], shape=(12,), dtype=int32), dense_shape=tf.Tensor([5 4], shape=(2,), dtype=int64))
I want to convert this sparse tensor to another 1D tensor of shape (5, 1) where the only column represents the number (or size) of values in each of the rows.
For example, for the above sparse tensor, desired 1D tensor would be [3, 2, 0, 3, 4].
How do you think I could do it?
Sorry, I tried going through the TensorFlow api docs but couldn't find anything to try that I could paste here on what I have already tried.
Thanks in advance.
You can use bin count on the indices.
tf.math.bincount(tf.cast(st.indices[:,0], tf.int32))
I have a numpy array that is built with coded sections. The sections come in 2x2 blocks. I have a large dictionary of what those 2x2 blocks should be replaced with. How do I replace those 2x2 codes with values in the dictionary.
info_dict = {
5: np.array([[1, 0], [1, 0]], "int"),
6: np.array([[1, 0], [0, 1]], "int"),
7: np.array([[0, 1], [1, 0]], "int"),
8: np.array([[1, 1], [0, 0]], "int"),
}
print(np.array([[5, 5, 8, 8], [5, 5, 8, 8], [6, 6, 7, 7], [6, 6, 7, 7]]))
print(np.array([[1, 0, 1, 1], [1, 0, 0, 0], [1, 0, 0, 1], [0, 1, 1, 0]]))
# before (coded)
[[5 5 8 8]
[5 5 8 8]
[6 6 7 7]
[6 6 7 7]]
# after (final matrix)
[[1 0 1 1]
[1 0 0 0]
[1 0 0 1]
[0 1 1 0]]
For reference
#5
[[1 0]
[1 0]]
#6
[[1 0]
[0 1]]
#7
[[0 1]
[1 0]]
#8
[[1 1]
[0 0]]
One way to do it:
import numpy as np
info_dict = {
5: np.array([[1, 0], [1, 0]], "int"),
6: np.array([[1, 0], [0, 1]], "int"),
7: np.array([[0, 1], [1, 0]], "int"),
8: np.array([[1, 1], [0, 0]], "int"),
}
a = np.array([[5, 5, 8, 8], [5, 5, 8, 8], [6, 6, 7, 7], [6, 6, 7, 7]])
np.block([[info_dict[b] for b in r] for r in a[::2, ::2]])
It gives:
[[1 0 1 1]
[1 0 0 0]
[1 0 0 1]
[0 1 1 0]]
Assuming your dictionary has a small number of integers in it, and your squares are N to a side, you info_dict as follows:
mapping = np.zeros((max(info_dict) + 1, N, N), dtype=int)
for channel, value in info_dict.items():
mapping[channel] = value
If you can store the dictionary in this format to begin with, that's even better. Now you can use simple index with some rearrangement to get the result:
after = mapping[before[::N, ::N]].transpose(0, 2, 1, 3).reshape(before.shape)
My dataframe is:
X=[0,1,2
1,0,3
2,3,0]
X shape is 3*3.
For every value, I want to expand n times in every row and column, that is, transform my dataframe to the shape of (3*n)*(3*n),
if n=2, my ideal result is:
X=[0,0,1,1,2,2
0,0,1,1,2,2
1,1,0,0,3,3
1,1,0,0,3,3
2,2,3,3,0,0
2,2,3,3,0,0]
How to do that? thanks!
One way using pandas.Index.repeat:
ind = df.index.repeat(2)
new_df = df.iloc[ind, ind]
print(new_df)
Output:
0 0 1 1 2 2
0 0 0 1 1 2 2
0 0 0 1 1 2 2
1 1 1 0 0 3 3
1 1 1 0 0 3 3
2 2 2 3 3 0 0
2 2 2 3 3 0 0
You could use numpy.repeat, as follows:
import numpy as np
X = np.array([[0, 1, 2],
[1, 0, 3],
[2, 3, 0]] )
res = X.repeat(2, axis=1).repeat(2, axis=0)
print(res)
Output
[[0 0 1 1 2 2]
[0 0 1 1 2 2]
[1 1 0 0 3 3]
[1 1 0 0 3 3]
[2 2 3 3 0 0]
[2 2 3 3 0 0]]
A base python solution (without imports) would be a nested list comprehension:
>>> [[y for y in x for _ in range(3)] for x in X for _ in range(3)]
[[0, 0, 0, 1, 1, 1, 2, 2, 2],
[0, 0, 0, 1, 1, 1, 2, 2, 2],
[0, 0, 0, 1, 1, 1, 2, 2, 2],
[1, 1, 1, 0, 0, 0, 3, 3, 3],
[1, 1, 1, 0, 0, 0, 3, 3, 3],
[1, 1, 1, 0, 0, 0, 3, 3, 3],
[2, 2, 2, 3, 3, 3, 0, 0, 0],
[2, 2, 2, 3, 3, 3, 0, 0, 0],
[2, 2, 2, 3, 3, 3, 0, 0, 0]]
>>>
I am trying to change column position of a matrix by a given indexs of array
import numpy as np
t = np.array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
indexs = np.array([3, 4, 2, 1, 0])
check = [False for i in range(len(indexs))]
for i in range(len(indexs)):
check[i] = True
if (i != indexs[i] and check[indexs[i]] == False):
check[indexs[i]] = True
t[:, [i, indexs[i]]] = t[:, [indexs[i], i]]
print(t)
The result I want:
[[3 4 2 1 0]
[3 4 2 1 0]
[3 4 2 1 0]
[3 4 2 1 0]]
I want to return an array whose column positions is the same as indexs but I can't.
How can I achieve that?
Just index the array along the dimension you want:
t[:, indexs]
if you transpose the matrix it's easy
transposed = t.T
result = np.array([transposed[i] for i in indexs])
result = result.T
array([[3, 4, 2, 1, 0],
[3, 4, 2, 1, 0],
[3, 4, 2, 1, 0],
[3, 4, 2, 1, 0]])
I'm trying to change two vectors of a NumPy matrix at once, but I'm losing one vector components:
import numpy as np
data = np.array([[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4]])
last = data[:, -1]
print(last)
data[:, 1:] = data[:, :-1]
data[:, 0] = last
print(data)
Gives this result:
[4 4 4 4]
[[3 1 2 3]
[3 1 2 3]
[3 1 2 3]
[3 1 2 3]]
But I want to maintain the 4s in the first column. Is there any form to accomplish that?
Use:
>>> np.roll(data, 1, 1)
array([[4, 1, 2, 3],
[4, 1, 2, 3],
[4, 1, 2, 3],
[4, 1, 2, 3]])