# (apparently) same code steps result in different results (tags: 309)

tags:309

My Code:

def z_score(value, array, bessel = 0):
<snip -- provided by DQ -->

neighborhoods = ['NAmes','CollgCr','OldTown','Edwards','Somerst']

z_scores = {}
for n in neighborhoods:
z_scores[n] = abs(z_score(220000, houses[houses['Neighborhood']==n]['SalePrice'], bessel = 0))

print(z_scores)

Output:

{'Somerst': 0.16947327539358228, 'NAmes': 2.351975107016837, 'OldTown': 2.1704461921913354, 'CollgCr': 0.33643592854037785, 'Edwards': 1.8610564651451342}

My code is more compact than the solution, but should submit the same data, but the output is completely different.

Solution code:

north_ames = houses[houses['Neighborhood'] == 'NAmes']
clg_creek = houses[houses['Neighborhood'] == 'CollgCr']
old_town = houses[houses['Neighborhood'] == 'OldTown']
edwards = houses[houses['Neighborhood'] == 'Edwards']
somerset = houses[houses['Neighborhood'] == 'Somerst']

# Find the z-score for 200000 for every location
z_by_location = {}
for data, neighborhood in [(north_ames, 'NAmes'), (clg_creek, 'CollgCr'),
(old_town, 'OldTown'), (edwards, 'Edwards'),
(somerset, 'Somerst')]:

z_by_location[neighborhood] = z_score(200000, data['SalePrice'],
bessel = 0)

# Find the location with the z-score closest to 0
print(z_by_location)

Output:

{'Somerst': -0.5186390646965723, 'NAmes': 1.7239665910370232, 'OldTown': 1.7183080926865524, 'CollgCr': -0.03334366282705464, 'Edwards': 1.4435761938489418}

In both cases, the data being sent to the z_score procedure is the
houses[houses[‘Neighborhood’]==n][‘SalePrice’]
but the values returned are quite different.

What’s going on here?

Your code has “220000” as a value for value variable in z_score function, whereas Dataquest has “200000”. Update this value, your code gives the same answer as DQ’s.

The only other difference is abs method applied in your code.

Hope that helps.

1 Like

Thanks – I didn’t notice that the value had changed from earlier screens (which used 220000). Silly mistake on my part!