Related
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]
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]]
This question already has answers here:
Resizing and stretching a NumPy array
(3 answers)
Closed 2 years ago.
Suppose I have the following feature matrix X (ie. with 4 rows and 3 features):
X = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
(array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]]),
How do I duplicate say, the 1st and 2nd row twice, the 3rd row 3 times and no duplication in the 4th row, ie. I want to get this:
(array([[ 1, 2, 3],
[ 1, 2, 3]
[ 4, 5, 6],
[ 4, 5, 6]
[ 7, 8, 9],
[ 7, 8, 9]
[ 7, 8, 9]
[10, 11, 12]]),
Is there a way for me to multiply the features matrix X with an array of weights, say something like this:
np.array([2,2,3,1])
Thanks in advance.
You can do it through indexing:
repeats = np.array([2,2,3,1])
indices = np.repeat(range(len(X)), repeats)
X[indices]
Where indices is
array([0, 0, 1, 1, 2, 2, 2, 3])
np.repeat(X, [2, 2, 3, 1], axis=0)
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]
This question already has answers here:
Create a 2D list out of 1D list
(3 answers)
Closed 1 year ago.
How to create 2D list from 1D list with given row and cols?
Given:
sample_list=[5, 2, 3, 4, 1, 6, 1, 6, 7, 2, 3]
row=2
cols=4
It should return this:
[[5, 2, 3, 4],[1, 6, 1, 6]]
I don't need other numbers = 7, 2, 3.
I just want a list that has row and cols which user gives.
My solution does not return what i want,
My solution:
def 1D_to_2D(sample_list, row, cols):
return [sample_list[i:i+cols] for i in range(0, len(sample_list), rows)]
returns:
[[5, 2, 3, 4], [3, 4, 1, 6], [1, 6, 1, 6], [1, 6, 7, 2], [7, 2, 3], [3]]
Anyone can help?
Just slice your list using a list comprehension with range and a step of cols (not rows as you used), and limit the number of items using external slicing with rows:
sample_list=[5, 2, 3, 4, 1, 6, 1, 6, 7, 2, 3]
rows=2
cols=4
result = [sample_list[x:x+cols] for x in range(0,len(sample_list),cols)][:rows]
result:
[[5, 2, 3, 4], [1, 6, 1, 6]]
def D_to_2D(sample_list, rows, cols):
return [sample_list[cols*i: cols*(i+1)] for i in range(rows)]
>>> D_to_2D([5, 2, 3, 4, 1, 6, 1, 6, 7, 2, 3], 2, 4)
[[5, 2, 3, 4], [1, 6, 1, 6]]
f = lambda x, row, cols: [x[i:i+cols] for i in range(0, cols*row, cols)]
f(x, row, cols)