Swap/Transpose matrix columns - python

I have a 2d array like this:
m = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
And I want to swap the first and second columns, which would look like this:
[2, 1, 3]
[5, 4, 6]
[8, 7, 9]
I've looked around, but all I can find is stuff about turning rows into columns. I tried the zip function for example, but again it just does this:
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
Anyone know how to do this?

I think if you just want the first and second columns swapped you should change the code to be something like this:
for i in m:
i[1],i[0] = i[0],i[1]

for i in m:
i[0],i[1] = i[1],i[0]
output
[[2, 1, 3],
[5, 4, 6],
[8, 7, 9]]

Related

converting a normal list into a nested one using a condition

I want to convert a normal list(main_list) into a nested one(ans_list) but I don't know how.
main_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
ans_list = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Use a comprehension over appropriate slices:
n = 3
ans_list = [main_list[i::n] for i in range(n)]
# [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
if you are open to using NumPy and there are no other conditions other than how many rows you want to split in to then try this
import numpy as np
np.array(main_list).reshape(-1,3).tolist()

how to delete the short lists from a long nested list even the items are not continuous using Python?

For example:
t=[[1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [3, 5], [4, 5, 6],[4,5,6],[6,7], [6], [1]]
I want to delete the short lists if the items are included in a long one, even the items are not continuous. So, I expect the result to be:
[[1, 2, 3, 4, 5, 6],[6,7]]
I might figure out this by myself, but my way is not smart enough. Could anyone help me here?
Since all the elements in a list is unique, AND I like using sets
here's my code. Haven't checked it's efficiency but it looks cleaner :D
t = [[1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [3, 5], [4, 5, 6],[4,5,6],[6,7], [6], [1]]
t = [set(l) for l in t]
t = [list(x) for x in t if not any([x.issubset(y) for y in t if x != y])]
Sort from small to large, make them sets then pop them off the list to reduce the list size for every computation.
t=[[1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [3, 5], [4, 5, 6],[4,5,6],[6,7], [6], [1]]
t = sorted(t, key=lambda x: len(x))
t = [set(x) for x in t]
for i in range(len(t)):
a = t.pop(0)
if not any([a.issubset(x) for x in t]):
print(a)
My approach is very simple
I check the last element is already present in our longer list. If we present then we don't need to add to the longer list if it is not the case then we will add to the longerlists
sorted_lists=[[1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [3, 5], [4, 5, 6],[4,5,6],[6,7], [6], [1]]
sorted_big_lists =[]
for sorted_list in sorted_lists:
for test_list in sorted_big_lists:
if sorted_list[-1] in test_list:
break
else:
sorted_big_lists.append(sorted_list)
print(sorted_big_lists)

How to find a minimum value in a 2D array without using numpy or flattened in Python?

Let's say that:
atable = [[6, 2, 3, 1], [3, 4, 2, 1], [4, 8, 7, 6], [8, 9, 3, 7]]
without using numpy or flattened, how would you find the minimum value of the entire list?
I assume you can use a list but I am not sure.
Here are several approaches:
from itertools import chain
atable = [[6, 2, 3, 1], [3, 4, 2, 1], [4, 8, 7, 6], [8, 9, 3, 7]]
# Flatten the sublists into a single list
result = min(chain.from_iterable(atable))
# Find the min of each list, the find the min of mins
result = min(map(min, atable))
# Use a generator expression with nested loops
result = min(i for lst in atable for i in lst)
Here is a brute force approach:
atable = [[6, 2, 3, 1], [3, 4, 2, 1], [4, 8, 7, 6], [8, 9, 3, 7]]
min_list = []
for l in atable:
min_list.append(min(l))
min_val = min(min_list)
For your specific problem...
min(min(a_table))
As noted by #Prune this does not work. In fact min(a_table) returns the sublist with the smallest first element.

associate values to classes in Keras

How can I associate values to classes in Keras?
Input:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Expected:
[1, 2, 3], [4, 5, 6] associated with class 1
[7, 8, 9] associated with class 2
The problem is that [1, 2, 3], [4, 5, 6] come from one file and [7, 8, 9] from another. I have read an example with iris.csv but the samples have same size.
One suggestion would be to merge the two datasets into one like this:
X (shape=(3,3))
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
y (shape = (3,1))
[1, 1, 2]
Many classifiers support labels in that format out of the box.
If you need to further process the labels, you can use scikit-learn for that. Have a look at these:
LabelEncoder
OneHotEncoder

Concatenate 3 unidimensional arrays together in numpy

I'm leaving MatLab for numpy and in general it's going ok, but I'm having a nightmare finding a nice pythonic way to do what would have done this in MatLab:
A=[1.0;2.0;3.0;4.0] %Column vector
B=[5.0;6.0;7.0;8.0] %Another one
C=[A,B,B] %4 x 3 matrix
In Python, setting up A like so:
A=np.array([1,2,3,4])
B=np.array([5,6,7,8])
And concatenating like so:
C=np.concatenate((A,B,B),axis=1)
Stacks them one on top of the other, and _C, hstack etc fail as well. I'm guessing I need a nice pyythonic way of turning a (4,) numpy array into a (4,1) array. In my code these vectors are much bigger than this and are created dynamically so I can't just type:
A=np.array([[1],[2],[3],[4]])
Thanks in advance for any help!
I would use dstack
>>> A=np.array([1,2,3,4])
>>> B=np.array([5,6,7,8])
>>> np.dstack((A, B, B))
array([[[1, 5, 5],
[2, 6, 6],
[3, 7, 7],
[4, 8, 8]]])
You can use np.c_[A,B,B], which gives
array([[1, 5, 5],
[2, 6, 6],
[3, 7, 7],
[4, 8, 8]])
>>> C=np.array([A,B,B])
>>> C
array([[1, 2, 3, 4],
[5, 6, 7, 8],
[5, 6, 7, 8]])
or:
>>> C=np.array([A,B,B]).swapaxes(1,0)
>>> C
array([[1, 5, 5],
[2, 6, 6],
[3, 7, 7],
[4, 8, 8]])

Categories

Resources