Explanation of python code snippet >>numpy.nonzero(row == max(row))[0][0]<< from a script using numpy
By : GalS
Date : March 29 2020, 07:55 AM
will be helpful for those in need What does this peace of python code , Perhaps an example will help: code :
In []: R= arange(12).reshape(4, 3)
In []: R
Out[]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
In []: i= 1
In []: row= R[i, ]
In []: row
Out[]: array([3, 4, 5])
In []: j= nonzero(row== max(row))[0][0]
In []: j
Out[]: 2
In []: R[i, j]
Out[]: 5

Nonzero function help, Python Numpy
By : user3357592
Date : March 29 2020, 07:55 AM
hop of those help? I have two arrays, and I have a complex condition like this: new_arr<0 and old_arr>0 I am using nonzero but I am getting an error. The code I have is this: , Try indices = nonzero((new_arr < 0) & (old_arr > 0)): code :
In [5]: import numpy as np
In [6]: old_arr = np.array([ 0,1, 0,1, 1, 1, 0, 1])
In [7]: new_arr = np.array([ 1, 1,1,1,1,1, 1, 1])
In [8]: np.nonzero((new_arr < 0) & (old_arr > 0))
Out[8]: (array([4, 5]),)

Python Numpy nonzero
By : user1989575
Date : March 29 2020, 07:55 AM
I wish did fix the issue. This may be due to the fact that len(X) only returns X's length along the first axis. When you do code :
X = X[np.nonzero(X)]
In [1]: import numpy as np
In [2]: X = np.zeros((42, 2))
In [3]: X[:, 0] = 1
In [4]: X[0, 1] = 1
In [5]: len(X)
Out[5]: 42
In [6]: len(X[np.nonzero(X)])
Out[6]: 43
In [7]: X[np.nonzero(X)].shape
Out[7]: (43,)
X = X[ X[:, 0] != 0 ]

Python / Cython / Numpy optimization of np.nonzero
By : user2531088
Date : March 29 2020, 07:55 AM
I wish this helpful for you Jaime may have given a good answer, but I will comment on improving the Cython code and add a performance comparison. First, you should use the 'annotate' feature, cython a filename.pyx, this will generate an HTML file. Load that in a browser and it highlights 'slow' lines with yelloworange, this indicates where improvements can be made. code :
for xDiff in [1,0,1]:
for yDiff in [1,0,1]:
for zDiff in [1,0,1]:
for xDiff in range(1, 2):
for yDiff in range(1, 2):
for zDiff in range(1, 2):
if array[x1,y1,z1]:
def sphericity(np.ndarray[np.uint8_t, ndim=3] array):
cdef np.uint8_t array_view [:, :, :] = array
...
if array_view[x1,y1,z1]:
for x in range(0, xSize):
for y in range(0, ySize):
for z in range(0, zSize):
if array[x,y,z] == 0:
continue
...
#cython: boundscheck=False, nonecheck=False, wraparound=False
import numpy as np
cimport numpy as np
def sphericity2(np.uint8_t [:, :, :] array):
#Pass an mask array (1's are marked, 0's ignored)
cdef int xSize,ySize,zSize
xSize=array.shape[0]
ySize=array.shape[1]
zSize=array.shape[2]
cdef int sa,vol,x,y,z,neighbors,xDiff,yDiff,zDiff,x1,y1,z1
cdef float onethird,twothirds,sp
sa=vol=0 #keep running tally of volume and surface area
for x in range(0, xSize):
for y in range(0, ySize):
for z in range(0, zSize):
if array[x,y,z] == 0:
continue
neighbors=0
vol+=1
for xDiff in range(1, 2):
for yDiff in range(1, 2):
for zDiff in range(1, 2):
if abs(xDiff)+abs(yDiff)+abs(zDiff)==1:
x1=x+xDiff
y1=y+yDiff
z1=z+zDiff
if x1>=0 and y1>=0 and z1>=0 and x1<xSize and y1<ySize and z1<zSize:
#print '',x1,y1,z1,array[x1,y1,z1]
if array[x1,y1,z1]:
#print '',x1,y1,z1,array[x1,y1,z1]
neighbors+=1
#print 'had this many neighbors',neighbors
sa+=(6neighbors)
onethird=float(1)/float(3)
twothirds=float(2)/float(3)
sph = ((np.pi**onethird)*((6*vol)**twothirds)) / sa
#print 'sphericity',sphericity
return sph
Original : 2.123s
Jaime's : 1.819s
Optimized Cython : 0.136s
@ moarningsun : 0.090s

Python numpy nonzero cumsum
By : Θεοδωρος Παντος
Date : March 29 2020, 07:55 AM
To fix the issue you can do I want to do nonzero cumsum with numpy array. Simply skip zeros in array and apply cumsum. Suppose I have a np. array , Just trying to simplify it:) code :
b=np.cumsum(a)
[b[i] if ((i > 0 and b[i] != b[i1]) or i==0) else 0 for i in range(len(b))]

