  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # Python Numpy: Coalesce and return first nonzero observation  » python » Python Numpy: Coalesce and return first nonzero observation

By : Caio César
Date : November 20 2020, 11:01 PM
Hope that helps "Coalesce": use putmask to replace values equal to zero with values from array2: code :
``````want = array1.copy()
``````
``````where_nonzero = np.where(np.tril(want) != 0)

"""For the where array, get the indices of only
the first index for each column"""
first_indices = np.unique(where_nonzero, return_index=True)

# Get the values from want for those indices
want2 = want[(where_nonzero[first_indices], where_nonzero[first_indices])]
`````` ## Explanation of python code snippet >>numpy.nonzero(row == max(row))<< 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))
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 : import numpy as np

In : old_arr = np.array([ 0,-1, 0,-1, 1, 1, 0, 1])

In : new_arr = np.array([ 1, 1,-1,-1,-1,-1, 1, 1])

In : np.nonzero((new_arr < 0) & (old_arr > 0))
Out: (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 : import numpy as np

In : X = np.zeros((42, 2))

In : X[:, 0] = 1

In : X[0, 1] = 1

In : len(X)
Out: 42

In : len(X[np.nonzero(X)])
Out: 43
``````
``````In : X[np.nonzero(X)].shape
Out: (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 yellow-orange, 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
ySize=array.shape
zSize=array.shape

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

sa+=(6-neighbors)

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[i-1]) or i==0) else 0 for i in range(len(b))]
`````` 