CYBER WEEK - EXTRA SAVINGS EVENT
TRY A FREE LESSON

Alternate solution , generalization 309 locating-values-in-different-distributions

Generalization , alternate solution zscore

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
    if st_dev > 0:
        z = distance / st_dev
    else:
        z=0
    
    
    return z

zscore_dict = {}
for neighborhood in houses["Neighborhood"].unique():
    house_neighbourhood=houses[houses["Neighborhood"]==neighborhood]
    zscore=z_score(200000,house_neighbourhood["SalePrice"])
    zscore_dict[neighborhood] = zscore
    

temp_zscore=list(zscore_dict.values())
temp_zscore.remove(0)
minimum_z=min(temp_zscore, key=abs)
best_investment=""
for key,value in zscore_dict.items():
    if value==minimum_z:
        best_investment=key
        break

Number closet to 0

So in my solution I handled divide by 0 case by modifying the function
Also I removed those entries which has zscore = 0 and finally from long list of zscore which is harder to analyze visually , I used the trick to get the number closet to 0.

(I removed 0 from list because min function gives 0 instead of closet to 0)

Below is list for all zscores :

zscore_dictdict (<class 'dict'>)
{'Blmngtn': 0.11595732289141979,
 'Blueste': 1.971627039978237,
 'BrDale': 7.90507238659071,
 'BrkSide': 2.115068825122695,
 'ClearCr': -0.17086968683508777,
 'CollgCr': -0.03334366282705464,
 'Crawfor': -0.11632281541693103,
 'Edwards': 1.4435761938489418,
 'Gilbert': 0.2838695620024792,
 'Greens': 0.3143563250918371,
 'GrnHill': -1.6,
 'IDOTRR': 2.6516413941458885,
 'Landmrk': 0,
 'MeadowV': 5.249799195106243,
 'Mitchel': 0.9173843934983515,
 'NAmes': 1.7239665910370232,
 'NPkVill': 6.490342146128624,
 'NWAmes': 0.30878959216719754,
 'NoRidge': -1.2937760641863747,
 'NridgHt': -1.2757683240933695,
 'OldTown': 1.7183080926865524,
 'SWISU': 2.136781288943868,
 'Sawyer': 2.7435750384892814,
 'SawyerW': 0.32643649035699374,
 'Somerst': -0.5186390646965723,
 'StoneBr': -1.051917166168375,
 'Timber': -0.6768923754405425,
 'Veenker': -0.7537802756492855}
4 Likes