What is the meaning of a[..., 4] in python? [duplicate] - python

This question already has answers here:
What does the Ellipsis object do?
(14 answers)
Closed 6 months ago.
I am sorry if this looks easy but I can't find the meaning of this online.
Below is the original code line in the Non-max suppression function for yoloV5 in general.py:
xc = prediction[..., 4] > conf_thres #candidates

I have understood it now. Below is a code snippet
import numpy as np
n = np.random.randint(9, size=(5,6))
print(n)
check =5
a = n[...,4]>check
print(a)
Output for above code is :
[[1 2 2 2 4 3]
[1 5 4 8 2 0]
[6 6 3 0 7 6]
[4 2 5 1 3 1]
[3 5 2 6 4 2]]
[False False True False False]

Related

How to derive the following pattern with Python

How can I make the following pattern with Python, I thought about everything, but I didn't get anywhere
Problem solved, thanks to all contributors
Why negativity?
Well, if stackoverflow is not for asking questions, then what is it for???
The displayed numbers are always the maximum of the row number and column number they appear at.
So, to print that pattern, you could do something line this:
def solve(n):
for row in range(1, n + 1):
for col in range(1, n + 1):
print(max(row, col), end=" ")
print("\n")
solve(5)
As you posted an image, it is not clear whether there should be empty lines between the lines with numbers, and whether at the end it is fine to have trailing newline characters or not.
Also check whether there are requirements as to how the pattern should look when the size of the matrix is more than 9.
But it should not be hard to adapt the code accordingly.
Here's another approach:
numbers = [1, 2, 3, 4, 5]
for i in range(len(numbers)):
for j in range(i):
numbers[j] += 1
print(*numbers)
Output:
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
import numpy as np #Just to format as matrix when printing
a=[[max(r,c) for r in range(1,6)] for c in range(1,6)]
print(np.array(a))
>>> [[1 2 3 4 5]
>>> [2 2 3 4 5]
>>> [3 3 3 4 5]
>>> [4 4 4 4 5]
>>> [5 5 5 5 5]]

Mean values inside matrix [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 2 years ago.
Improve this question
Suppose we have a 6x6 matrix (numpy ndarray) like:
[1 1 2 2 3 3
1 1 2 2 3 3
4 4 5 5 6 6
4 4 5 5 6 6
7 7 8 8 9 9
7 7 8 8 9 9]
How would I calculate the mean value of each of the 2x2 submatrices containing the same number, and get as an output a 3x3 matrix?
Not sure whether this is what you want
import numpy as np
x = np.arange(1,10).reshape(3,3)
y = np.ones((2,2),dtype=np.int32)
z = np.kron(x,y)
z
def meanpool2d(x):
h,w = x.shape
out = np.zeros((h//2,w//2))
for i in range(h//2):
for j in range(w//2):
out[i][j] = np.mean(x[2*i:2*i+2,2*j:2*j+2])
return out
meanpool2d(z)
Construct the input array
import numpy as np
x = np.arange(1,10).reshape(3,3)
x = np.repeat(x, 2, 0)
x = np.repeat(x, 2, 1)
print(x)
Out:
[[1 1 2 2 3 3]
[1 1 2 2 3 3]
[4 4 5 5 6 6]
[4 4 5 5 6 6]
[7 7 8 8 9 9]
[7 7 8 8 9 9]]
Compute the means of all 2x2 blocks
m = (x.reshape(3, 2, -1, 2)
.swapaxes(1,2)
.mean((2,3)))
print(m)
Out:
[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]

Delete specific Values of an Array: Python

I have an array of the shape (1179648, 909).
The problem is that some rows are filled with 0's only. I am checking for this as follows:
for i in range(spectra1Only.shape[0]):
for j in range(spectra1Only.shape[1]):
if spectra1Only[i,j] == 0:
I now want to remove the whole row of [i] if there is any 0 appearing to get a smaller amount of only the data needed.
My question is: what would be the best method to do so? Remove? Del? numpy.delete? Or any other method?
You can use Boolean indexing with np.any along axis=1:
spectra1Only = spectra1Only[~(spectra1Only == 0).any(1)]
Here's a demonstration:
A = np.random.randint(0, 9, (5, 5))
print(A)
[[5 0 3 3 7]
[3 5 2 4 7]
[6 8 8 1 6]
[7 7 8 1 5]
[8 4 3 0 3]]
print(A[~(A == 0).any(1)])
[[3 5 2 4 7]
[6 8 8 1 6]
[7 7 8 1 5]]

Python numpy: reshape list into repeating 2D array

I'm new to python and I have a question about numpy.reshape. I currently have 2 lists of values like this:
x = [0,1,2,3]
y = [4,5,6,7]
And I want them to be in separate 2D arrays, where each item is repeated for the length of the original lists, like this:
xx = [[0,0,0,0]
[1,1,1,1]
[2,2,2,2]
[3,3,3,3]]
yy = [[4,5,6,7]
[4,5,6,7]
[4,5,6,7]
[4,5,6,7]]
Is there a way to do this with numpy.reshape, or is there a better method I could use? I would very much appreciate a detailed explanation. Thanks!
numpy.meshgrid will do this for you.
N.B. From your requested output, it looks like you want ij indexing, not the default xy
from numpy import meshgrid
x = [0,1,2,3]
y = [4,5,6,7]
xx,yy=meshgrid(x,y,indexing='ij')
print xx
>>> [[0 0 0 0]
[1 1 1 1]
[2 2 2 2]
[3 3 3 3]]
print yy
>>> [[4 5 6 7]
[4 5 6 7]
[4 5 6 7]
[4 5 6 7]]
For reference, here's xy indexing
xx,yy=meshgrid(x,y,indexing='xy')
print xx
>>> [[0 1 2 3]
[0 1 2 3]
[0 1 2 3]
[0 1 2 3]]
print yy
>>> [[4 4 4 4]
[5 5 5 5]
[6 6 6 6]
[7 7 7 7]]

How to filter a numpy array based on indices? [duplicate]

This question already has answers here:
How to apply a disc shaped mask to a NumPy array?
(7 answers)
Closed 8 years ago.
I've a square numpy array and I would like to extract the values from an annulus region around the central point of the array. I would like to set the radii of the annulus based on the distance of the points from the center. I retrieved the array indices by using numpy.indices but could not mange to find an efficient way to construct the filter. I'll appreciate if you share your comments/suggestions.
indices = numpy.indices((5, 5))
print indices
[[[0 0 0 0 0]
[1 1 1 1 1]
[2 2 2 2 2]
[3 3 3 3 3]
[4 4 4 4 4]]
[[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]]]
Now I want to extract the values of those points whose indices are at a distance of say, 1 from the central point i.e. (2,2).
pt = (2, 2)
distance = 1
mask = (indices[0] - pt[0]) ** 2 + (indices[1] - pt[1]) ** 2 <= distance ** 2
result = my_array[mask]

Categories