Python. Delete rows from matrix without Numpy [duplicate] - python

This question already has answers here:
How to remove an element from a list by index
(18 answers)
Closed 2 years ago.
so I have this matrix:
matrix = [ [1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
And I want to remove the last row, so it return me something like this:
matrix = [ [1, 2, 3],
[4, 5, 6]
]
P.S. I cant use Numpy.

matrix = [ [1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix[:-1]
This gives [[1, 2, 3], [4, 5, 6]] back
So what you can do is:
matrix = matrix[:-1]

Related

Count Elements of a Row above a Threshold in Python [duplicate]

This question already has answers here:
Count of specific case per row in matrix
(2 answers)
Closed 1 year ago.
I have an numpy array:
data = np.array([[1, 2, 3, 4, 5],
[9, 7, 3, 5, 8],
[1, 2, 3, 9, 6]])
I want to get the count of elements above 3 in each row:
[2, 4, 2]
Try this:
arr = (data > 3).sum(axis=1)
Output:
>>> arr
array([2, 4, 2])

Conversion 2d array in python [duplicate]

This question already has answers here:
Transpose list of lists
(14 answers)
Closed 2 years ago.
We have an array
a = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
and looking for array:
b = [[1, 4, 7],[2, 5, 8],[3, 6, 9]]
Thank you!
Try with numpy.transpose(). See below:
import numpy as np
a = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
[list(i) for i in np.array(a).transpose()]
Output:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

How to swap columns and rows of a multi-dimensional list in Python without using external libraries? [duplicate]

This question already has answers here:
Matrix Transpose in Python [duplicate]
(19 answers)
Closed 2 years ago.
Let's say that:
>>> data
[[1, 2],
[3, 4],
[5, 6],
[7, 8]]
When rows and columns are swapped:
>>> data
[[1, 3, 5, 7],
[2, 4, 6, 8]]
How do I achieve the following without the use of external libraries such as pandas or numpy?
data = [[1, 2],
[3, 4],
[5, 6],
[7, 8]]
print([*map(list, zip(*data))])
Prints:
[[1, 3, 5, 7], [2, 4, 6, 8]]

Count unique slices in a ndarray [duplicate]

This question already has answers here:
How to count RGB or HSV channel combination in an image?
(1 answer)
Count occurrences of unique arrays in array
(5 answers)
Closed 3 years ago.
I have a 3D integer tensor X with X.shape=(m, n, k)
I'd like to treat X as a (m, n) matrix with entries that are k sized integer vectors and count how many such unique entries are in each row.
So for example
>>> X
array([[[0, 1, 2],
[0, 1, 2],
[1, 2, 3],
[1, 2, 3]],
[[3, 4, 5],
[4, 5, 6],
[5, 6, 7],
[6, 7, 8]]])
>>> X.shape
(2, 4, 3)
>>> count_unique(X)
[2, 4]
Since in the first row of the tensor there are 2 unique vectors and in the second row there are 4
Bonus points for returning the actual unique vectors, e.g.
>>> get_unique(X)
[[[0, 1, 2], [1, 2, 3]],\
[3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8]]]
My solution (partially vectorized) for the first question
count_unique = lambda X: [len(np.unique(row, axis=0)) for row in X]
unique_list = []
for sublist in X:
tmp_unique_list = []
for element in sublist:
if element not in tmp_unique_list:
tmp_unique_list.append(element)
unique_list.append(tmp_unique_list)
Output:
> unique list
[[[0, 1, 2], [1, 2, 3]], [[3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8]]]
And the count:
> [len(elem) for elem in unique_list]
[2, 4]

Find first occurence of index and keep that row [duplicate]

This question already has answers here:
how to search for unique elements by the first column of a multidimensional array
(2 answers)
Closed 6 years ago.
I have an ndarray with the following content:
[0, 1]
[0, 5]
[1, 7]
[2, 9]
[2, 4]
[2, 4]
[3, 8]
[4, 2]
[4, 7]
Now I'd like to keep only the first row when the first element is the same for multiple rows. Would result in:
[0, 1]
[1, 7]
[2, 9]
[3, 8]
[4, 2]
How can I achieve this with numpy?
Given an input data as:
x = np.array([
[0, 1],
[0, 5],
[1, 7],
[2, 9],
[2, 4],
[2, 4],
[3, 8],
[4, 2],
[4, 7],
])
Then you could use numpy.unique with the return_index set to true (as #divakar mentioned in the commend) in order to find the unique indices of the first elements.
idx = numpyp.unique(x[:,0], return_index=True)[1]
Then you can just access them as:
x[idx]
Hope this helps.

Categories

Resources