# [SOLVED] Chi square test with different sample sizes in Python

## Issue

I have two sets of data as shown below. Each data set have a different length

`X_data1` and `Y_data1` (black binned data) have a length of 40 whereas `X_data2` and `Y_data2` (red) have a length of 18k.

I would like to perform a Chi-Square Goodness of Fit Test on these two data as follows

``````from scipy import stats
stats.chisquare(f_obs=Y_data1, f_exp=Y_data2)
``````

But I can not since the vector size is not the same and I receive an error.

~/opt/miniconda3/lib/python3.9/site-packages/scipy/stats/stats.py in chisquare(f_obs, f_exp, ddof, axis) 6850 6851 """
-> 6852 return power_divergence(f_obs, f_exp=f_exp, ddof=ddof, axis=axis, 6853 lambda_="pearson")
6854

~/opt/miniconda3/lib/python3.9/site-packages/scipy/stats/stats.py in
power_divergence(f_obs, f_exp, ddof, axis, lambda_) 6676 if
f_exp is not None: 6677 f_exp = np.asanyarray(f_exp)
-> 6678 bshape = _broadcast_shapes(f_obs_float.shape, f_exp.shape) 6679 f_obs_float =

~/opt/miniconda3/lib/python3.9/site-packages/scipy/stats/stats.py in
184 n = n1
185 else:
–> 186 raise ValueError(f’shapes {shape1} and {shape2} could not be ‘
188 shape.append(n)

ValueError: shapes (40,) and (18200,) could not be broadcast together

Is there a way in Python that I can compare these two data? ## Solution

You can’t do this unless both `f_exp` and `f_obs` have the same length. You can achieve your goal by interpolating `Y_data2` on the x-axis of `Y_data1`. You can do it as follows:

``````from scipy.interpolate import InterpolatedUnivariateSpline
spl = InterpolatedUnivariateSpline(X_data2, Y_data2)
new_Y_data2 = spl(X_data1)
``````

As both `Y_data1` and `new_Y_data2` have same lengths now, you can use them in `stats.chisquare` as follows:

``````from scipy import stats
stats.chisquare(f_obs=Y_data1, f_exp=new_Y_data2)
``````