How to reshape an array with numpy like this: - python-3.x

I have this:
array([[0, 0, 1, 1, 2, 2, 3, 3],
[0, 0, 1, 1, 2, 2, 3, 3]])
And I would like to reshape my array like this:
array([[0, 0, 1, 1],
[0, 0, 1, 1],
[2, 2, 3, 3],
[2, 2, 3, 3]])
How do I do it using python numpy?

You can just split and concatenate:
a = np.array([[0, 0, 1, 1, 2, 2, 3, 3],
[0, 0, 1, 1, 2, 2, 3, 3]])
cols = a.shape[1] // 2
np.concatenate((a[:,:cols], a[:,cols:]))
#[[0 0 1 1]
# [0 0 1 1]
# [2 2 3 3]
# [2 2 3 3]]

You can simply swap rows after reshaping it.
a= np.array([[0, 0, 1, 1, 2, 2, 3, 3],
[0, 0, 1, 1, 2, 2, 3, 3]]).reshape(4,4)
a[[1,2]] = a[[2,1]]
Output:
array([[0, 0, 1, 1],
[0, 0, 1, 1],
[2, 2, 3, 3],
[2, 2, 3, 3]])

Related

partitioning blocks of elements from the indices of a numpy 2D array

I have the indices of a 2D array. I want to partition the indices such that the corresponding entries form blocks (block size is given as input m and n).
For example, if the indices are as given below
(array([0, 0, 0, 0, 1, 1, 1, 1, 6, 6, 6, 6, 7, 7, 7, 7 ]), array([0, 1, 7, 8, 0,1,7,8, 0,1,7,8, 0, 1, 7, 8]))
for the original matrix (from which the indices are generated)
array([[3, 4, 2, 0, 1, 1, 0, 2, 4],
[1, 3, 2, 0, 0, 1, 0, 4, 0],
[1, 0, 0, 1, 1, 0, 1, 1, 3],
[0, 0, 0, 3, 3, 0, 4, 0, 4],
[4, 3, 4, 2, 1, 1, 0, 0, 4],
[0, 1, 0, 4, 4, 2, 2, 2, 1],
[2, 4, 0, 1, 1, 0, 0, 2, 1],
[0, 4, 1, 3, 3, 2, 3, 2, 4]])
and if the block size is (2,2), then the blocks should be
[[3, 4],
[1, 3]]
[[2, 4]
[4, 0]]
[[2, 4]
[0, 4]]
[[2, 1]
[2, 4]]
Any help to do it efficiently?
Does this help? A is your matrix.
row_size = 2
col_size = 3
for i in range(A.shape[0] // row_size):
for j in range(A.shape[1] // col_size):
print(A[row_size*i:row_size*i + row_size, col_size*j:col_size*j + col_size])

Numpy Selecting Elements given row and column index arrays

I have row indices as a 1d numpy array and a list of numpy arrays (list as same length as the size of the row indices array. I want to extract values corresponding to these indices. How can I do it ?
This is an example of what I want as output given the input
A = np.array([[2, 1, 1, 0, 0],
[3, 0, 2, 1, 1],
[0, 0, 2, 1, 0],
[0, 3, 3, 3, 0],
[0, 1, 2, 1, 0],
[0, 1, 3, 1, 0],
[2, 1, 3, 0, 1],
[2, 0, 2, 0, 2],
[3, 0, 3, 1, 2]])
row_ind = np.array([0,2,4])
col_ind = [np.array([0, 1, 2]), np.array([2, 3]), np.array([1, 2, 3])]
Now, I want my output as a list of numpy arrays or list of lists as
[np.array([2, 1, 1]), np.array([2, 1]), np.array([1, 2, 1])]
My biggest concern is the efficiency. My array A is of dimension 20K x 10K.
As #hpaulj commented, likely, you won't be able to avoid looping - e.g.
import numpy as np
A = np.array([[2, 1, 1, 0, 0],
[3, 0, 2, 1, 1],
[0, 0, 2, 1, 0],
[0, 3, 3, 3, 0],
[0, 1, 2, 1, 0],
[0, 1, 3, 1, 0],
[2, 1, 3, 0, 1],
[2, 0, 2, 0, 2],
[3, 0, 3, 1, 2]])
row_ind = np.array([0,2,4])
col_ind = [np.array([0, 1, 2]), np.array([2, 3]), np.array([1, 2, 3])]
# make sure the following code is safe...
assert row_ind.shape[0] == len(col_ind)
# 1) select row (A[r, :]), then select elements (cols) [col_ind[i]]:
output = [A[r, :][col_ind[i]] for i, r in enumerate(row_ind)]
# output
# [array([2, 1, 1]), array([2, 1]), array([1, 2, 1])]
Another way to do this could be to use np.ix_ (still requires looping). Use with caution though for very large arrays; np.ix_ uses advanced indexing - in contrast to basic slicing, it creates a copy of the data instead of a view - see the docs.

numpy remove column by different value in batch

I want to ask how numpy remove columns in batch by list.
The value in the list corresponds to the batch is different from each other.
I know this problem can use the for loop to solve, but it is too slow ...
Can anyone give me some idea to speed up?
array (batch size = 3):
[[0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6]]
remove index in the list (batch size = 3)
[[2, 3, 4], [1, 2, 6], [0, 1, 5]]
output:
[[0, 1, 5, 6], [0, 3, 4, 5], [2, 3, 4, 6]]
Assuming the array is 2d, and the indexing removes equal number of elements per row, we can remove items with a boolean mask:
In [289]: arr = np.array([[0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6]]
...: )
In [290]: idx = np.array([[2, 3, 4], [1, 2, 6], [0, 1, 5]])
In [291]: mask = np.ones_like(arr, dtype=bool)
In [292]: mask[np.arange(3)[:,None], idx] = False
In [293]: arr[mask]
Out[293]: array([0, 1, 5, 6, 0, 3, 4, 5, 2, 3, 4, 6])
In [294]: arr[mask].reshape(3,-1)
Out[294]:
array([[0, 1, 5, 6],
[0, 3, 4, 5],
[2, 3, 4, 6]])

Finding the number of occurrences of various groups in a list

`So,I am given a list-
group = [2,1,3,4]
Each index of this list represents a group.
So group 0 = 2
group 1 = 1
group 2 = 3
group 3 = 4
I am given another list called :
l =[[[0, 0, 3, 3, 3, 3], [0, 0, 1, 3, 3, 3, 3]], [[0, 1]], [[2, 2, 2, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3, 3]], [[0, 0, 2, 2, 2, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3, 3]]]
the output I want is:
dict = {0:[0,3],1;[1],2:[2,3],3:[0,2]}
If the number of times the element appears in each sublist of l ie if both l[0][0] and l[0][1] have 0's appear 2 times, it is added to the index 0 of the dict. Since both l[0][0] and l[0][1] have 3 appear 4 times(this is because group[3] is 4), it is added to the index 0.
now in l[1][0] and 0 appears just once(instead of twice) so its not added to index 1. However 1 just appears once so it is added to index 1.Thanks!
What I have tried so far:
def tryin(l,groups):
for i in range(len(l)):
count = 0
for j in range(len(l[i])):
if j in (l[i][j]):
count+=1
if count == groups[i]:
print(i,j)
try this code :
input:
group = [2,1,3,4]
l =[[[0, 0, 3, 3, 3, 3], [0, 0, 1, 3, 3, 3, 3]], [[0, 1]], [[2, 2, 2, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3, 3]], [[0, 0, 2, 2, 2, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3, 3]]]
def IntersecOfSets(list_):
result = set(list_[0])
for s in list_[1:]:
result.intersection_update(s)
return result
def nb_occ(l,group):
d = {}
for i in l:
l2=[]
for j in i:
l1 = []
for x in group:
if j.count(group.index(x)) >= x :
y=group.index(x)
l1.append(y)
l2.append(l1)
if len(l2)>1:
d[str(l.index(i))]= IntersecOfSets(l2)
else:
d[str(l.index(i))]= l2[0]
return d
print(nb_occ(l,group))
output:
{'2': {2, 3}, '1': [1], '0': {0, 3}, '3': {0, 2}}

Where is the error in this python code?

First few examples:
Input:
10
1
4 5 6
Output:
6
another one:
Input:
10
2
3 3 3
7 7 4
Output:
4
I put this code it is correct for some cases but not for all where is the problem?
n = int(input())
q = int(input())
z = 0
repeat = 0
ans = 0
answ = []
arrx = []
arry = []
for i in range(q):
maxi = 0
x,y,w = [int(i) for i in input().split()]
x,y = x+1, y+1
if((arrx.count(x)>=1)):
index = arrx.index(x)
if(y==arry[index]):
if(answ[index]==ans):
repeat += answ[index]
z = answ[index]
arrx.append(x)
arry.append(y)
if((w>x or w>y) or (w>(n-x) or w>(n-y))):
maxi = max(x, y, (n-x), (n-y))
if(((x>=w) or (y>=w)) or (((n-x)>=w) or ((n-y)>=w))):
maxi = w
ans = max(ans, maxi)
answ.append(ans)
if(ans>z):
repeat = 0
print(ans+repeat)
The problem I see with your code is you are handling the data as two one dimensional arrays, arrx and arry, when the problem calls for a two dimensional array. You should be able to print out your data structure and see the heat map for the volcanoes. For the first example, you've got a single hot volcano in the middle of the map:
10
1
4 5 6
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[2, 2, 2, 2, 2, 2, 2, 2, 2, 1]
[2, 3, 3, 3, 3, 3, 3, 3, 2, 1]
[2, 3, 4, 4, 4, 4, 4, 3, 2, 1]
[2, 3, 4, 5, 5, 5, 4, 3, 2, 1]
[2, 3, 4, 5, 6, 5, 4, 3, 2, 1]
[2, 3, 4, 5, 5, 5, 4, 3, 2, 1]
[2, 3, 4, 4, 4, 4, 4, 3, 2, 1]
[2, 3, 3, 3, 3, 3, 3, 3, 2, 1]
[2, 2, 2, 2, 2, 2, 2, 2, 2, 1]
Where the hottest (6) spot is obviously the one volcano itself. For the second example, you've got two cooler volcanos:
10
2
3 3 3
3 3 3
7 7 4
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0]
[0, 1, 2, 2, 2, 1, 0, 0, 0, 0]
[0, 1, 2, 3, 2, 1, 0, 0, 0, 0]
[0, 1, 2, 2, 3, 2, 1, 1, 1, 1]
[0, 1, 1, 1, 2, 3, 2, 2, 2, 2]
[0, 0, 0, 0, 1, 2, 3, 3, 3, 2]
[0, 0, 0, 0, 1, 2, 3, 4, 3, 2]
[0, 0, 0, 0, 1, 2, 3, 3, 3, 2]
[0, 0, 0, 0, 1, 2, 2, 2, 2, 2]
Where the hot spot will either be the hotter of the two volcanos or potentially some spot that falls in their overlap that gets heated by both. In this case, the overlap spots don't get hotter than the hotest (4) volcano. But if the volcanoes were closer, one or more might have.

Resources