Flatten 3d list to 2d in python - python

So I have a list of lists in python that is something like this:
[[[0, 1, 0, 1, 0]]
[[1, 1, 1, 1, 1]]
[[1, 0, 0, 1, 1]]
[[0, 1, 0, 0, 0]]]
I want to flatten this list and end up with this:
[[0, 1, 0, 1, 0]
[1, 1, 1, 1, 1]
[1, 0, 0, 1, 1]
[0, 1, 0, 0, 0]]
Is there a straightforward way to do this in python?

Using numpy.squeeze you can do what you want:
import numpy as np
a = np.array([[[0, 1, 0, 1, 0]],
[[1, 1, 1, 1, 1]],
[[1, 0, 0, 1, 1]],
[[0, 1, 0, 0, 0]]])
a.squeeze()
[[0 1 0 1 0]
[1 1 1 1 1]
[1 0 0 1 1]
[0 1 0 0 0]]

a = [[[0, 1, 0, 1, 0]],
[[1, 1, 1, 1, 1]],
[[1, 0, 0, 1, 1]],
[[0, 1, 0, 0, 0]]]
[i[0] for i in a]
output
[[0, 1, 0, 1, 0], [1, 1, 1, 1, 1], [1, 0, 0, 1, 1], [0, 1, 0, 0, 0]]

Related

Selecting rows in 2-D numpy array based on subset of column values

Suppose I have the following numpy array:
a = np.array([[1, 1, 0, 0, 1],
[1, 1, 0, 0, 0],
[1, 0, 0, 1, 1],
[1, 1, 0, 0, 0],
[1, 1, 0, 0, 0],
[1, 1, 0, 0, 0],
[0, 0, 0, 1, 0],
[1, 1, 0, 0, 0],
[1, 1, 0, 0, 0],
[1, 1, 1, 0, 1],
[1, 1, 0, 0, 0],
[1, 1, 0, 0, 1],
[1, 1, 0, 0, 0],
[1, 0, 0, 1, 0],
[1, 0, 1, 1, 0]])
I want to select only the rows, where column with index 1 have value 1 and column with index 2 have value 0.
i tried the following:
evidence = {1:1,2:0}
mask = a[:,list(evidence.keys())] == list(evidence.values())
But after that i am stuck.
how can I do it in numpy 2-D array?
Try:
out = a[(a[:, 1] == 1) & (a[:, 2] == 0)]
Given a dictionary of column, value pairs, you could use:
evidence = {1:1,2:0}
out = a[ np.logical_and.reduce([a[:, c] == v for c, v in evidence.items()]) ]
which generalizes the above solution to a sequence of &.

Merge two arrays with the same dimension based on a condition

I have two arrays with the same dimension:
a = [
[1, 1, 1, 1],
[1, 0, 0, 1],
[1, 0, 0, 1],
[1, 1, 1, 1], ]
b = [
[0, 1, 1, 0],
[0, 0, 0, 0],
[2, 0, 0, 2],
[0, 0, 0, 0], ]
I would like to create a new one, only changing the values where B is not 0 and is different than A. The result would be:
c = [
[1, 1, 1, 1],
[1, 0, 0, 1],
[2, 0, 0, 2],
[1, 1, 1, 1], ]
How can I do this?
You can do assignment with boolean conditions:
a[b != 0] = b[b != 0]
a
array([[1, 1, 1, 1],
[1, 0, 0, 1],
[2, 0, 0, 2],
[1, 1, 1, 1]])
Here is one that I find easy to parse:
>>> np.where(b,b,a)
array([[1, 1, 1, 1],
[1, 0, 0, 1],
[2, 0, 0, 2],
[1, 1, 1, 1]])
This picks each value from either the third or second arguments based on whether the first argument is zero or not.
You don't need numpy. Here is a solution you can actually read line by line:
a = [
[1, 1, 1, 1],
[1, 0, 0, 1],
[1, 0, 0, 1],
[1, 1, 1, 1], ]
b = [
[0, 1, 1, 0],
[0, 0, 0, 0],
[2, 0, 0, 2],
[0, 0, 0, 0], ]
c = a[:]
#I would like to create a new one, only changing the values where B is not 0 and is different than A. The result would be:
for lineindex,line in enumerate(a):
for index,x in enumerate(line):
if x != b[lineindex][index] and b[lineindex][index] != 0:
c[lineindex][index] = b[lineindex][index]
print(c)

Present ndarray of interger elements as ndarray of arrays

I'm having problems with vectorized function application to ndarrays.
What is a good and working way to do this?
Input:
y_train
array([0, 0, 2, 1, 2, 0, 2, 1, 0, 0, 1, 2, 1, 0, 0, 0, 2, 0, 2, 1, 2, 2,
1, 2, 2, 0, 1, 2, 1, 1, 2, 1, 1, 2, 0, 2, 1, 2, 2, 2, 0, 2, 1, 0,
0, 0, 1, 2, 0, 2, 2, 1, 2, 2, 1, 2, 2, 2, 0, 1, 1, 1, 1, 2, 0, 0,
0, 1, 1, 1, 0, 2, 0, 1, 1, 2, 0, 2, 2, 2, 2, 0, 2, 2, 0, 0, 0, 1,
2, 0, 1, 0, 0, 1, 2, 2, 2, 0, 1, 1, 2, 0, 1, 0, 0, 2, 2, 0, 2, 0,
2, 1, 1, 0, 1, 0, 2, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0])
Desired Output:
array([[0,0],
[0,0],
[0,1],
[1,0],
...
..])
I have:
def func(x):
return np.array([int(x) for x in list(np.binary_repr(x,width=2,))])
func(y_train)
TypeError Traceback (most recent call last)
<ipython-input-178-ca45ba935147> in <module>
TypeError: only integer scalar arrays can be converted to a scalar index
Based on the additional chat conversation, it looks like you want to convert the (130,1) shaped array to a (130,2) shaped array where you want to replace 0 with [0,0], 1 with [1,0], and 2 with [0,1].
To do this, create a dictionary, then lookup the dictionary, and replace each element in y_train with the dictionary value.
The code is as follows:
y_train = [0, 0, 2, 1, 2, 0, 2, 1, 0, 0, 1, 2, 1, 0, 0, 0, 2, 0, 2, 1, 2, 2,
1, 2, 2, 0, 1, 2, 1, 1, 2, 1, 1, 2, 0, 2, 1, 2, 2, 2, 0, 2, 1, 0,
0, 0, 1, 2, 0, 2, 2, 1, 2, 2, 1, 2, 2, 2, 0, 1, 1, 1, 1, 2, 0, 0,
0, 1, 1, 1, 0, 2, 0, 1, 1, 2, 0, 2, 2, 2, 2, 0, 2, 2, 0, 0, 0, 1,
2, 0, 1, 0, 0, 1, 2, 2, 2, 0, 1, 1, 2, 0, 1, 0, 0, 2, 2, 0, 2, 0,
2, 1, 1, 0, 1, 0, 2, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0]
d = {0:[0,0],1:[1,0],2:[0,1]}
arr = [d[i] for i in y_train]
print (arr)
The output of this will be:
[[0, 0], [0, 0], [0, 1], [1, 0], [0, 1], [0, 0], [0, 1], [1, 0], [0, 0], [0, 0], [1, 0], [0, 1], [1, 0], [0, 0], [0, 0], [0, 0], [0, 1], [0, 0], [0, 1], [1, 0], [0, 1], [0, 1], [1, 0], [0, 1], [0, 1], [0, 0], [1, 0], [0, 1], [1, 0], [1, 0], [0, 1], [1, 0], [1, 0], [0, 1], [0, 0], [0, 1], [1, 0], [0, 1], [0, 1], [0, 1], [0, 0], [0, 1], [1, 0], [0, 0], [0, 0], [0, 0], [1, 0], [0, 1], [0, 0], [0, 1], [0, 1], [1, 0], [0, 1], [0, 1], [1, 0], [0, 1], [0, 1], [0, 1], [0, 0], [1, 0], [1, 0], [1, 0], [1, 0], [0, 1], [0, 0], [0, 0], [0, 0], [1, 0], [1, 0], [1, 0], [0, 0], [0, 1], [0, 0], [1, 0], [1, 0], [0, 1], [0, 0], [0, 1], [0, 1], [0, 1], [0, 1], [0, 0], [0, 1], [0, 1], [0, 0], [0, 0], [0, 0], [1, 0], [0, 1], [0, 0], [1, 0], [0, 0], [0, 0], [1, 0], [0, 1], [0, 1], [0, 1], [0, 0], [1, 0], [1, 0], [0, 1], [0, 0], [1, 0], [0, 0], [0, 0], [0, 1], [0, 1], [0, 0], [0, 1], [0, 0], [0, 1], [1, 0], [1, 0], [0, 0], [1, 0], [0, 0], [0, 1], [0, 0], [0, 0], [0, 0], [1, 0], [1, 0], [1, 0], [1, 0], [0, 0], [1, 0], [1, 0], [0, 0], [0, 0], [0, 0]]
You can also achieve this using list(map(d.get, y_train)) where d is the dictionary with the lookup values.
Looks like you want this to be a two 2 column array.
import numpy as np
y_train = [0, 0, 2, 1, 2, 0, 2, 1, 0, 0, 1, 2, 1, 0, 0, 0, 2, 0, 2, 1, 2, 2,
1, 2, 2, 0, 1, 2, 1, 1, 2, 1, 1, 2, 0, 2, 1, 2, 2, 2, 0, 2, 1, 0,
0, 0, 1, 2, 0, 2, 2, 1, 2, 2, 1, 2, 2, 2, 0, 1, 1, 1, 1, 2, 0, 0,
0, 1, 1, 1, 0, 2, 0, 1, 1, 2, 0, 2, 2, 2, 2, 0, 2, 2, 0, 0, 0, 1,
2, 0, 1, 0, 0, 1, 2, 2, 2, 0, 1, 1, 2, 0, 1, 0, 0, 2, 2, 0, 2, 0,
2, 1, 1, 0, 1, 0, 2, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0]
ln = len(y_train) #find the length of the list
arr = np.array(y_train) #convert y_train to numpy array
arr1 = arr.reshape(ln//2,2) #convert it to length/2 for rows, 2 for columns
print (arr1)
The output of this will be:
[[0 0]
[2 1]
[2 0]
[2 1]
[0 0]
[1 2]
......
[2 0]
[0 0]
[1 1]
[1 1]
[0 1]
[1 0]
[0 0]]

An elegant way to split specific items in a list (array)?

For example, I have a list which only contains zeroes, ones and twos
ls = [
[1, 1, 0, 2, 2],
[1, 1, 1, 2, 2, 2],
[0, 1, 0, 0, 0, 2, 0]
]
I want to split this list it into two list,
ls1 contains the ones, and ls2 contains the twos. I would like to keep the shape and use 0 to replace 2's in ls1 and 1's in ls2. The expected result is:
ls1 = [
[1, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0]
]
ls2 = [
[0, 0, 0, 2, 2],
[0, 0, 0, 2, 2, 2],
[0, 0, 0, 0, 0, 2, 0]
]
I know I can use a for loop to handle it, but is there an elegant way?
ls1, ls2 = ([[b & x for b in a] for a in ls] for x in (1, 2))
Using nested list comprehension:
ls1 = [[1 if e == 1 else 0 for e in l] for l in ls]
ls2 = [[2 if e == 2 else 0 for e in l] for l in ls]
# ls1
[[1, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0]]
# ls2
[[0, 0, 0, 2, 2],
[0, 0, 0, 2, 2, 2],
[0, 0, 0, 0, 0, 2, 0]]
Nothing especially "elegant" about it, but you could simply use a list comprehension:
ls = [
[1, 1, 0, 2, 2],
[1, 1, 1, 2, 2, 2],
[0, 1, 0, 0, 0, 2, 0]
]
def keep_only(val, lst):
return [[v if v==val else 0 for v in sublist] for sublist in lst]
ls1 = keep_only(1, ls)
ls2 = keep_only(2, ls)
Output:
print(ls1)
print(ls2)
# [[1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0]]
# [[0, 0, 0, 2, 2], [0, 0, 0, 2, 2, 2], [0, 0, 0, 0, 0, 2, 0]]

how to convert Node and Arc sets representation to an adjacency matrix in python

What am I doing wrong?
N=[1, 2, 3, 4, 5, 6]
A=[[1, 2], [1, 3], [1, 5], [2, 3], [2, 4], [3, 4], [3, 5], [4, 6], [5, 6]]
for i in range (len(N)):
for j in range (len(N)):
my_list1 = [i[0] for i in A]
my_list2 = [i[1] for i in A]
print my_list1
print my_list2
I am not getting this output instead im getting [1, 1, 1, 2, 2, 3, 3, 4, 5]
repeated multiply times
ADJ=[[0, 1, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0], \
[0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0]]
The simplest way to approach this requires building an empty adjacency matrix first, then populating it with a single pass through A. Here's a simple example that ignores the contents of N.
#!/usr/bin/env python
def show(matrix):
for row in matrix:
print row
print
N = [1, 2, 3, 4, 5, 6]
A = [[1, 2], [1, 3], [1, 5], [2, 3], [2, 4], [3, 4], [3, 5], [4, 6], [5, 6]]
adj_target = [
[0, 1, 1, 0, 1, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0]
]
show(adj_target)
size = len(N)
adj = [[0]*size for _ in range(size)]
#show(adj)
for u,v in A:
adj[u-1][v-1] += 1
show(adj)
print adj == adj_target
output
[0, 1, 1, 0, 1, 0]
[0, 0, 1, 1, 0, 0]
[0, 0, 0, 1, 1, 0]
[0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0]
[0, 1, 1, 0, 1, 0]
[0, 0, 1, 1, 0, 0]
[0, 0, 0, 1, 1, 0]
[0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0]
True

Categories