RuntimeWarning: divide by zero encountered in double_scalars

Screen Link:


My Code:

def z_score(value, array, bessel = 0):
    mean = sum(array) / len(array)
    
    from numpy import std
    st_dev = std(array, ddof = bessel)
    
    distance = value - mean
    z = distance / st_dev
    
    return z

zscore_by_neighborhood = {}
#Segment houses by neighborhood
for neighborhood in houses['Neighborhood'].unique():
        houses_by_neighborhood = houses[houses['Neighborhood'] == neighborhood]
        zscore_by_neighborhood[neighborhood] = z_score(200000, houses_by_neighborhood['SalePrice'], bessel = 0)

What I expected to happen:

Houses to be segmented by neighborhood and calculation of z-score for each neighborhood.
What actually happened:

RuntimeWarning:

divide by zero encountered in double_scalars

Hello, the warning is generated because something is being divided by zero.

Z = \frac{x-\mu}{\sigma}

Just follow the instructions step bu step to avoid this.

1 Like

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)