Mean values inside matrix [closed] - python

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.]]

Related

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

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]

How can I generate an array based on the grouped values of another array in Numpy? [duplicate]

This question already has answers here:
Taking subarrays from numpy array with given stride/stepsize
(3 answers)
Closed 2 years ago.
Lets say I have the following array:
`Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]`
I want to generate an array that looks like this:
R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]
How can this be done with NumPy?
How about this?
from numpy.lib import stride_tricks
Z = np.arange(1,15,dtype=np.uint32)
R = stride_tricks.as_strided(Z,(11,4),(4,4))
print(R)
Output:
[[ 1 2 3 4]
[ 2 3 4 5]
[ 3 4 5 6]
[ 4 5 6 7]
[ 5 6 7 8]
[ 6 7 8 9]
[ 7 8 9 10]
[ 8 9 10 11]
[ 9 10 11 12]
[10 11 12 13]
[11 12 13 14]]
As navneethc righly pointed out this function should be used with caution.

Calculate mean of each image block - Python [duplicate]

This question already has answers here:
How to evaluate the sum of values within array blocks
(3 answers)
Closed 3 years ago.
I have a matrix like below
c = [[ 1 2 3 4 5 6 7 8 9 1]
[ 2 3 4 5 6 7 8 9 1 2]
[ 3 4 5 6 7 8 9 1 2 3]
[ 4 5 6 7 8 9 1 2 3 4]]
From the given SO ANSWERS in this post, I used it to divide the matrix into blocks (2*5) like below
def blockshaped(arr, nrows, ncols):
"""
Return an array of shape (n, nrows, ncols) where
n * nrows * ncols = arr.size
If arr is a 2D array, the returned array should look like n subblocks with
each subblock preserving the "physical" layout of arr.
"""
h, w = arr.shape
assert h % nrows == 0, "{} rows is not evenly divisble by {}".format(h, nrows)
assert w % ncols == 0, "{} cols is not evenly divisble by {}".format(w, ncols)
return (arr.reshape(h//nrows, nrows, -1, ncols)
.swapaxes(1,2)
.reshape(-1, nrows, ncols))
print(blockshaped(c, 2, 5))
Result:
[[[ 1 2 3 4 5 ]
[ 2 3 4 5 6 ]]
[[ 6 7 8 9 1 ]
[ 7 8 9 1 2]]
[[ 3 4 5 6 7 ]
[ 4 5 6 7 8 ]]
[[ 8 9 1 2 3 ]
[ 9 1 2 3 4]]]
I got 4 blocks of matrix, and now I need the mean value of each block. How to calculate the mean of this each block?
When I try to use the mean() it is going to calculate the mean for the whole matrix but not for each block.
1. Half line solution with List comprehension
results = blockshaped(c, 2, 5)
block_means = [np.mean(results[block,:,:]) for block in range(results.shape[0])]
print(block_means)
# [3.5, 5.8, 5.5, 4.2]
Version 2 -- shorter code:
results = blockshaped(c, 2, 5)
block_means = [np.mean(block) for block in results]
# [3.5, 5.8, 5.5, 4.2]
In [15]: %timeit [np.mean(results[block,:,:]) for block in range(results.shape[0])]
10000 loops, best of 3: 35.9 µs per loop
In [16]: %timeit [np.mean(block) for block in results]
10000 loops, best of 3: 33.4 µs per loop
P.S: The second solution will only work if the blocks are in the first (0) dimension of results.
Another option is to use the map function:
means = np.round(map(np.mean, r),3)
print(means)
which results in:
[ 3.5 5.8 5.5 4.2]

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]]

Write multidimensional numpy array to csv

I have a multidimensional numpy array containing function values, and I'd like to write it to a long csv. How can I do that cleanly? I couldn't find a numpy function but maybe I was googling the wrong terms. An example:
#!/usr/bin/python
import csv
import numpy as np
x = np.array([1, 2, 3, 4])
y = np.array([50, 51])
z = np.array([99, 100, 101])
f = np.arange(24).reshape((4, 2, 3)) # Contains f(x, y, z)
assert f.shape == (x.size, y.size, z.size)
## I'd like to create a csv file whose columns are x, y, z, f
## How can I do that?
## np.savetxt("test.csv", a, delimiter=",")
## TypeError: float argument required, not numpy.ndarray
## Works, but does numpy already have a function that does this?
with open("test.csv", "wb") as csvfile:
writer = csv.writer(csvfile, delimiter=",", quotechar="'", quoting=csv.QUOTE_MINIMAL)
writer.writerow(["x", "y", "z", "f"])
for x_index in range(x.size):
for y_index in range(y.size):
for z_index in range(z.size):
writer.writerow([x[x_index], y[y_index], z[z_index],
f[x_index, y_index, z_index]])
I have three vectors x, y, z and an X-by-Y-by-Z array containing function values f(x, y, z). In other words, f[i, j, k] contains the function value f that corresponds to x[i], y[j] and z[k]. Is there a cleaner way to write a long csv with columns x,y,z,f?
Here's head test.csv:
x,y,z,f
1,50,99,0
1,50,100,1
1,50,101,2
1,51,99,3
1,51,100,4
1,51,101,5
2,50,99,6
2,50,100,7
2,50,101,8
Edit: This seems to work as well:
x_y_z = np.array([x for x in itertools.product(x, y, z)])
assert x_y_z.shape[0] == f.size
output_array = np.hstack((x_y_z, f.flatten().reshape((f.size, 1)))
np.savetxt("test2.csv", output_array, comments="", delimiter=",", fmt="%i",
header="x,y,z,f")
Am I reinventing the wheel?
In fact, yes it's lightly more complicated than what it should be.
Given 3 lists x,y and z
import numpy as np
x = [1,2,3]
y = [4,5]
z = [6,7,8]
You need to modify this lists in order to get all possible combinations, use numpy.repeat this way:
new_x = np.array(x).repeat(len(y)*len(z))
print new_x
>> [1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3]
new_y = np.array([y]).repeat(len(z),axis=0).repeat(len(x),axis=1)
print new_y
>> [4 4 4 5 5 5 4 4 4 5 5 5 4 4 4 5 5 5]
new_z = np.array([z]).repeat(len(x)*len(y),axis=0)
print new_z
>> [6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8]
# reshape y and z just like new_x
new_y = new_y.reshape(new_x.shape)
new_z = new_z.reshape(new_x.shape)
just concatenate them!
# suppose that your vector f
f = np.array(range(len(x)*len(y)*len(z)))
matrix = np.array([new_x,new_y,new_z,f]).T
# or matrix = np.concatenate((np.concatenate((new_x,new_y),axis=1),np.concatenate((new_z,f),axis=1)),axis=1).T
print matrix
>>
[[ 1 4 6 0]
[ 1 4 7 1]
[ 1 4 8 2]
[ 1 5 6 3]
[ 1 5 7 4]
[ 1 5 8 5]
[ 2 4 6 6]
[ 2 4 7 7]
[ 2 4 8 8]
[ 2 5 6 9]
[ 2 5 7 10]
[ 2 5 8 11]
[ 3 4 6 12]
[ 3 4 7 13]
[ 3 4 8 14]
[ 3 5 6 15]
[ 3 5 7 16]
[ 3 5 8 17]]
finally, save the array as csv
np.savetxt('file_name.csv',matrix)

Categories