Help on Sampling Missions 8/14

Screen Link:
https://app.dataquest.io/m/283/sampling/8/proportional-stratified-sampling

My Code:

create three statums to sample based on games played

under_12 = wnba[wnba[‘Games Played’] <= 12]
btw_13_22 = wnba[(wnba[‘Games Played’] > 12) & (wnba[‘Games Played’] <= 22)]
over_23 = wnba[wnba[‘Games Played’] > 22]

proportional_sampling_means =

for i in range(100):
sample_under_12 = under_12[‘PTS’].sample(1, random_state = i)
sample_btw_13_22 = btw_13_22[‘PTS’].sample(2, random_state = i)
sample_over_23 = over_23[‘PTS’].sample(7, random_state = i)

final_sample = pd.concat([sample_under_12, sample_btw_13_22, sample_over_23])

proportional_sampling_means.append(final_sample.mean())

plt.scatter(range(1,101), proportional_sampling_means)
plt.axhline(wnba[‘PTS’].mean())


What I expected to happen:
I expected it to work. Looks to match the code from the answer.

What actually happened:
I am getting an error on the scatter plot about x and y not being the same size.

Replace this line with the output/error
```alueErrorTraceback (most recent call last)
<ipython-input-1-37e4b997122f> in <module>()
     18 proportional_sampling_means.append(final_sample.mean())
     19 # create scatter plot
---> 20 plt.scatter(range(1,101), proportional_sampling_means)
     21 plt.axhline(wnba['PTS'].mean())
     22 

/dataquest/system/env/python3/lib/python3.4/site-packages/matplotlib/pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, hold, data, **kwargs)
   3249                          vmin=vmin, vmax=vmax, alpha=alpha,
   3250                          linewidths=linewidths, verts=verts,
-> 3251                          edgecolors=edgecolors, data=data, **kwargs)
   3252     finally:
   3253         ax.hold(washold)

/dataquest/system/env/python3/lib/python3.4/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1810                     warnings.warn(msg % (label_namer, func.__name__),
   1811                                   RuntimeWarning, stacklevel=2)
-> 1812             return func(ax, *args, **kwargs)
   1813         pre_doc = inner.__doc__
   1814         if pre_doc is None:

/dataquest/system/env/python3/lib/python3.4/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   3838         y = np.ma.ravel(y)
   3839         if x.size != y.size:
-> 3840             raise ValueError("x and y must be the same size")
   3841 
   3842         s = np.ma.ravel(s)  # This doesn't have to match x, y in size.

ValueError: x and y must be the same size

<!--Enter other details below: -->

Hey @johnkcavers. It’s a little hard to tell exactly because the code was pasted without any formatting. My best guess is that perhaps one or both of these lines isn’t indented in the loop.

final_sample = pd.concat([sample_under_12, sample_btw_13_22, sample_over_23])
proportional_sampling_means.append(final_sample.mean())

If that’s the case, then proportional_sampling_means would only have 1 element instead of 100, which would explain why the x and y must be the same size error would show up.

1 Like

That fixed it. Both lines were not indented properly. Thank you.