How to make dotted line in a binary array - python

how to create the dotted line in the below NumPy array
import NumPy as np
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
x=np.array( [ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]])
def make_figure(inp_arr: np.array, outputname):
# create graphical output for visual check
cmap = ListedColormap([ 'r','b','g'])
plt.imshow(inp_arr, cmap=cmap)
plt.grid(color='b', linestyle=':', linewidth=0.55)
plt.savefig(input_folder + 'pics_' + str(outputname) + '.png', format='png', dpi=350)
# plt.show()
#plt.clf()
bh=make_figure(b,'gh')
requirement: how to convert element 1 into 0 with the step of two expected outputs is like
I tried with a brute force algorithm, but I am not able to find the solution
output array looks like
y=np.array( [ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0]])
for visual representation like making a dotted line

Here's one way to find the minimum weight full path, then take the first point, skip two points, and repeat until the end of the path.
import numpy as np
from sklearn.neighbors import radius_neighbors_graph
from scipy import sparse
import networkx as nx
x = np.array( [ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1] ] )
x_nonzeros = x.nonzero()
num_points = len(x_nonzeros[0])
x_coords = [[x_nonzeros[0][k], x_nonzeros[1][k]] for k in range(num_points)]
neighbors = radius_neighbors_graph(x_coords, radius=1.5, mode="distance")
G = nx.Graph(neighbors)
full_paths = [
{"path": path, "weight": nx.classes.path_weight(G, path, weight="weight")}
for path in nx.all_simple_paths(G, 0, 40) if len(path)==num_points
]
full_paths.sort(key=lambda rec: rec["weight"])
the_path = full_paths[0]["path"]
y_coords = [x_coords[coord] for coord in the_path[0::3]]
y = sparse.coo_array(([1]*len(y_coords),np.array(y_coords).T)).toarray()
print(y)
# [[1 0 0 0 0 0 0 0 0 0 0 0 0]
# [0 0 0 0 0 0 0 0 0 0 0 0 0]
# [0 0 0 0 0 0 0 0 0 0 0 0 0]
# [0 0 0 1 0 0 0 0 0 0 0 0 0]
# [0 0 0 0 0 0 0 0 0 0 0 0 0]
# [0 0 0 0 0 0 1 0 0 1 0 0 1]
# [0 0 0 0 0 0 0 0 0 0 0 0 0]
# [0 0 0 0 0 0 0 0 0 0 0 0 0]
# [1 0 0 1 0 0 1 0 0 1 0 0 1]
# [0 0 0 0 0 0 0 0 0 0 0 0 0]
# [0 0 0 0 0 0 0 0 0 0 0 0 0]
# [0 0 0 1 0 0 1 0 0 0 0 0 0]
# [0 0 0 0 0 0 0 0 1 0 0 1 0]]

Related

Is there a way to populate a full matrix given only an octant segment, with symmetry?

I have an octant of a symmetric matrix which looks like this:
arr_in = [[1],
[0, 0],
[0, 0, 1],
[0, 0, 0, 0],
[0, 1, 0, 1, 0]]
I need to convert this into the full array, is there a way to do this with numpy? The full matrix end product should be:
0 1 0 1 0 1 0 1 0
1 0 0 0 0 0 0 0 1
0 0 1 0 0 0 1 0 0
1 0 0 0 0 0 0 0 1
0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 1
0 0 1 0 0 0 1 0 0
1 0 0 0 0 0 0 0 1
0 1 0 1 0 1 0 1 0
I used np.clip and np.rot90:
import numpy as np
arr_in = [[1],
[0, 0],
[0, 0, 1],
[0, 0, 0, 0],
[0, 1, 0, 1, 0]]
x = np.zeros((5, 5), dtype="uint8")
for idx, row in enumerate(arr_in):
x[idx, :len(row)] = row
np.clip(x + x.T, 0, 1, out=x)
final = np.zeros((9, 9), dtype="uint8")
final[:5, :5] = np.rot90(x, 2) # NW corner
final[:5, 4:] = np.rot90(x, 1) # NE corner
final[4:, :5] = np.rot90(x, 3) # SW corner
final[4:, 4:] = x # SE corner
Output:
array([[0, 1, 0, 1, 0, 1, 0, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 0, 1, 0, 1, 0, 1, 0]], dtype=uint8)
Place the triangle in a square numpy array.
Reflect that in the diagonal.
Place the result in a bigger numpy array.
Reflect that horizontally then vertically.
import numpy as np
arr_in = [[1],
[0, 0],
[0, 0, 1],
[0, 0, 0, 0],
[0, 1, 0, 1, 0]]
l = len( arr_in )
arr = np.zeros( (l,l), dtype = np.int64 )
# Generate square numpy array. There may be a neater way to do this.
for row, a in enumerate(arr_in):
arr[ row, :len(a) ] = a
arr
# array([[1, 0, 0, 0, 0],
# [0, 0, 0, 0, 0],
# [0, 0, 1, 0, 0],
# [0, 0, 0, 0, 0],
# [0, 1, 0, 1, 0]])
tr = np.tril( arr, -1 ) # Lower triangle, missing the diagonal ( -1 )
tr
# array([[0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0],
# [0, 1, 0, 1, 0]])
arr += tr.T # arr += transpose of tr
arr
# array([[1, 0, 0, 0, 0],
# [0, 0, 0, 0, 1],
# [0, 0, 1, 0, 0],
# [0, 0, 0, 0, 1],
# [0, 1, 0, 1, 0]])
result = np.zeros( (9,9), dtype = np.int64 ) # Create result array
result[ 4:, 4:] = arr # Fill the lower RH square
result
# array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 1, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 1],
# [0, 0, 0, 0, 0, 0, 1, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 1],
# [0, 0, 0, 0, 0, 1, 0, 1, 0]])
result[ :4 ] = result[ 5:][::-1] # Reflect in horizontal mirror
result
# array([[0, 0, 0, 0, 0, 1, 0, 1, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 1],
# [0, 0, 0, 0, 0, 0, 1, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 1],
# [0, 0, 0, 0, 1, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 1],
# [0, 0, 0, 0, 0, 0, 1, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 1],
# [0, 0, 0, 0, 0, 1, 0, 1, 0]])
result[ :, :4 ] = result[ :, 5: ][:, ::-1] # Reflect in vertical mirror
result
# array([[0, 1, 0, 1, 0, 1, 0, 1, 0],
# [1, 0, 0, 0, 0, 0, 0, 0, 1],
# [0, 0, 1, 0, 0, 0, 1, 0, 0],
# [1, 0, 0, 0, 0, 0, 0, 0, 1],
# [0, 0, 0, 0, 1, 0, 0, 0, 0],
# [1, 0, 0, 0, 0, 0, 0, 0, 1],
# [0, 0, 1, 0, 0, 0, 1, 0, 0],
# [1, 0, 0, 0, 0, 0, 0, 0, 1],
# [0, 1, 0, 1, 0, 1, 0, 1, 0]])

Convert a list of strings containing list-of-integers into an array

I have a series that is a list of lists that contain integers that I am attempting to turn into an array. This is a small snip-it of the list I am trying to convert into an array.
['[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]',
'[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]',
'[0, 0, 0, 0, 0, 0, 0, 1, 0, 0]',
'[0, 0, 0, 0, 0, 0, 0, 1, 0, 1]',
'[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]']
I've tried to replace the quotes with .replace, but that hasn't worked out.
sequence = [i.replace(" '' ", ' ') for i in sequence]
You can use ast.literal_eval to change the string to list of lists of ints
sequence = [literal_eval(i) for i in sequence]
# [[0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1]]
You can change it to numpy array
import numpy as np
array = np.asarray(sequence)
print(array)
output
[[0 0 0 0 0 0 0 0 0 1]
[0 0 0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 0 1 0 1]
[0 0 0 0 0 0 0 1 1 1]]
Or to 1d pandas array
import pandas as pd
array = pd.array([item for items in sequence for item in items])
print(array)
outout
<IntegerArray>
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1]
Length: 50, dtype: Int64

How can I add a small matrix into a bigger one along the diagonal in a specific way?

I am trying to write a big matrix that includes a smaller row matrix (size changeable) that are spread on the "diagonal" of the matrix. All the other values are 0. How do I create such a matrix?
I've tried np.put, np.append. Here's what I have so far:
t = [1,2,3]
n=3
m=4
A = np.zeros((2*m,m*n+m),dtype=int)
for i in range (m):
A[i-1:i-1+t.shape[0], n*(i-1):n*(i-1)+t.shape[1]] += t
print("A= \n",np.matrix(A))
I want the following matrix (I'm sorry I don't know how to show matrix but if someone can help me with this too I would appreciate it a lot) :
A=
[[1 2 3 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 1 2 3 0 0 0 0 0 0 0 0 0 0 ]
[0 0 0 0 0 0 1 2 3 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 1 2 3 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
It causes the following error:
ValueError: operands could not be broadcast together with shapes (0,0) (1,3) (0,0)
You can use careful reshaping like so:
t = [1,2,3]
n=3
m=4
A = np.zeros((2*m,m*n+m),dtype=int)
A.ravel()[:m*(m*n+m+n)].reshape(m,-1)[:,:len(t)] = t
A
# array([[1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
Make mask for 12 positions and use it for assignment
idx = np.zeros(A.shape).astype(bool)
for i in range(m):
idx[i,i*n:i*n+3] = True
A[idx]= t*m
array([[1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

Apply a function to series of list without apply in pandas

I have a dataframe
df = pd.DataFrame({'Binary_List': [[0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1]]})
df
Binary_List
0 [0, 0, 1, 0, 0, 0, 0]
1 [0, 1, 0, 0, 0, 0, 0]
2 [0, 0, 1, 1, 0, 0, 0]
3 [0, 0, 0, 0, 1, 1, 1]
I want to apply a function to each list, without use of apply because apply is very slow when running on large dataset
def count_one(lst):
index = [i for i, e in enumerate(lst) if e != 0]
# some more steps
return len(index)
df['Value'] = df['Binary_List'].apply(lambda x: count_one(x))
df
Binary_List Value
0 [0, 0, 1, 0, 0, 0, 0] 1
1 [0, 1, 0, 0, 0, 0, 0] 1
2 [0, 0, 1, 1, 0, 0, 0] 2
3 [0, 0, 0, 0, 1, 1, 1] 3
I tried using this, but no improvement
vfunc = np.vectorize(count_one)
df['Value'] = vfunc(df['Binary_List'])
This gives me error
df['Value'] = count_one(df['Binary_List'])
you can try DataFrame.explode:
df.explode('Binary_List').reset_index().groupby('index').sum()
Binary_List
index
0 1
1 1
2 2
3 3
Also you can do:
pd.Series([np.array(key).sum() for key in df['Binary_List']])
0 1
1 1
2 2
3 3
dtype: int64
for getting length of list items you can use str function like below
df = pd.DataFrame({'Binary_List': [[0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1]]})
df["Binary_List"].astype(np.str).str.count("1")

How to find most common elemtent in a ndarray [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a numpy array with the following shape (11617, 37). The data is multi class data, and to establish a baseline, I need to find which class (or classes) are the most common.
I have tried this formula and also this
A = np.array([[0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0],
[0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0],
[0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0]])
axis = 0
u, indices = np.unique(arr, return_inverse=True)
answer = u[np.argmax(np.apply_along_axis(np.bincount, axis, indices.reshape(arr.shape),
None, np.max(indices) + 1), axis=axis)]
I need to find the most frequent combination of the 37 classes in my array
Expected output:
[0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0]
To find the most frequent combination (rows, which means axis=0), you can try this!
A = np.array([[1,0,0,0],
[1,0,0,1],
[1,0,0,0]])
unique_rows,counts = np.unique(A, return_counts=True,axis=0)
unique_rows[np.argmax(counts)]
FYI, If the array you mentioned in the question is your target variable, then it is an example of multi-label data.
This may be of use for you to understand multi-class and multi-label
You could try np.unique with return_counts parameter:
from operator import itemgetter
import numpy as np
A = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
uniques, counts = np.unique(A, axis=0, return_counts=True)
idxmax, _ = max(zip(range(len(counts)), counts), key=itemgetter(1))
print(uniques[idxmax])
Output
[0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0]
You can use collections.Counter.most_common if you convert your list of list elements to a tuple (convert the lists to tuples so they can be counted)
from collections import Counter
A = [[0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0]]
c = Counter(tuple(x) for x in A)
print(c.most_common()[0]) # ((0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0), 2)
This returns a tuple containing the most common list and the number of occurrences.
A really quick and easy solution:
A = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
print(max(A, key=A.count))
Which prints:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
If you need to pay attention to runtime or want to optimize your code - this is not the way you want to go. However, if you just need a quick solution, it might help to keep this one-liner in mind.
(A.tolist() gets you a list from a np.ndarray if you need that first.)
from collections import Counter
A = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
most_common = [Counter(i).most_common(1).pop()[0] for i in A]
most_common
[0, 0, 0]

Categories

Resources