vaex icon indicating copy to clipboard operation
vaex copied to clipboard

percentile_approx not working properly with binby

Open foleor opened this issue 7 years ago • 5 comments

Using percentile_approx on a data frame is not working correctly when combined with binby. For example df = vx.example() result = df.percentile_approx('x',percentage=50,binby=df.z,limits='minmax',shape=100) works, but result = df.percentile_approx('x',percentage=80,binby=df.z,limits='minmax',shape=100) gives IndexError: index 16736 is out of bounds for axis 1 with size 1026 while result = df.percentile_approx('x',percentage=90,binby=df.z,limits='minmax',shape=100) even gives a segmentation fault. Different combinations of 'percentile_limits' and 'percentile_shape' result in going over array boundary by different values, so clearly something is wrong with how the percentile sublength of the cumulative array is calculated when binby is used. All seems to be fine when binby is not used.

foleor avatar Apr 12 '19 11:04 foleor

Hey, thanks for the report, and giving me an (hopefully) reproducible example. That part needs a refactor anyway, and I have some new ideas for it.

maartenbreddels avatar Apr 12 '19 11:04 maartenbreddels

Thanks Roelof,

I hope to take a look at it today, I'll let you know how it goes.

cheers,

Maarten

maartenbreddels avatar Apr 12 '19 12:04 maartenbreddels

Hi Roelof,

I have made sure it does not crash anymore in #216, could you try out that branch (if you are comfortable with working from the dev version). I really need to look into it, I believe there is a cornercase where the algorithm does not work properly.

maartenbreddels avatar Apr 16 '19 08:04 maartenbreddels

Example showing the Pandas vs Vaex quantile/percentile results. Mean from Pandas and Vaex give the same output but quantiles are different.

import numpy as np
import pandas as pd
import vaex as vx

yChannel = 'y'
samplePoints = 50
start = 0
end = 250
selectionName = None
binbyChannelx = 'x_distrib'

# Create sample data in Pandas
x = np.arange(samplePoints*5)+.5
y = np.random.rand(samplePoints*5)
pandas_df = pd.DataFrame()
pandas_df[binbyChannelx] = x
pandas_df[yChannel] = y

grpby = pandas_df.groupby( pd.cut(pandas_df[binbyChannelx], samplePoints))

output_df = grpby[yChannel].quantile([0.25, 0.75]).unstack()
output_df['pandasMean'] = grpby[yChannel].mean()

vaex_df = vx.from_pandas(pandas_df)
output_df['vaex_percentile25']  = vaex_df.percentile_approx(yChannel, percentage=25.0 , binby=binbyChannelx, selection = selectionName, shape=(samplePoints,), limits=[start,end])
output_df['vaex_percentile75'] = vaex_df.percentile_approx(yChannel, percentage=75.0 , binby=binbyChannelx, selection = selectionName, shape=(samplePoints,), limits=[start,end])
output_df['vaex_mean'] = vaex_df.mean(yChannel , binby=binbyChannelx, selection = selectionName, shape=(samplePoints,), limits=[start,end])

image

DougRzz avatar Apr 24 '19 09:04 DougRzz

Interesting, using the fixed version and rewriting this a bit such that I take 500 samples of 5 points and then calculate the mean and std of the columns:

import numpy as np
import pandas as pd
import vaex as vx
yChannel = 'y'
samplePoints = 500
nsamples = 5
start = 0
end = samplePoints*nsamples
selectionName = None
binbyChannelx = 'x_distrib'

# Create sample data in Pandas
x = np.arange(samplePoints*nsamples)+.5
y = np.random.rand(samplePoints*nsamples)
pandas_df = pd.DataFrame()
pandas_df[binbyChannelx] = x
pandas_df[yChannel] = y

grpby = pandas_df.groupby( pd.cut(pandas_df[binbyChannelx], samplePoints))

output_df = grpby[yChannel].quantile([0.25, 0.75]).unstack()
output_df['pandasMean'] = grpby[yChannel].mean()

vaex_df = vx.from_pandas(pandas_df)
output_df['vaex_percentile25']  = vaex_df.percentile_approx(yChannel, percentage=25.0 ,
                binby=binbyChannelx, selection = selectionName, shape=(samplePoints,), limits=[start,end])
output_df['vaex_percentile75'] = vaex_df.percentile_approx(yChannel, percentage=75.0 ,
                binby=binbyChannelx, selection = selectionName, shape=(samplePoints,), limits=[start,end])
output_df['vaex_mean'] = vaex_df.mean(yChannel , binby=binbyChannelx,
                selection = selectionName, shape=(samplePoints,), limits=[start,end])

print(output_df.mean())
print(output_df.std())

I get values for panda that are distinctly weird: 0.25 0.332439 0.75 0.663000 pandasMean 0.496723 vaex_percentile25 0.246955 vaex_percentile75 0.748598 vaex_mean 0.496723 dtype: float64 0.25 0.176179 0.75 0.186822 pandasMean 0.132335 vaex_percentile25 0.142420 vaex_percentile75 0.152505 vaex_mean 0.132335 dtype: float64

Not sure why the quantiles with just 5 points should be 0.33 and 0.66.

Increasing to take 500 samples of 10 points still has significant biases for pandas: 0.25 0.293722 0.75 0.705728 pandasMean 0.499129 vaex_percentile25 0.248887 vaex_percentile75 0.752057 vaex_mean 0.499129 dtype: float64 0.25 0.127419 0.75 0.124582 pandasMean 0.093972 vaex_percentile25 0.119342 vaex_percentile75 0.117979 vaex_mean 0.093972

With 100 points pandas starts to look better, but vaex still gets closer to what I would call the expected answer: 0.25 0.256637 0.75 0.745420 pandasMean 0.501023 vaex_percentile25 0.251827 vaex_percentile75 0.751010 vaex_mean 0.501023 dtype: float64 0.25 0.042248 0.75 0.043429 pandasMean 0.029133 vaex_percentile25 0.041859 vaex_percentile75 0.043186 vaex_mean 0.029133 dtype: float64

Not sure what the different routines are expected to do under the hood (I have never worked with the pandas groupby routine), but these results are giving me at least some confidence that vaex is now giving me the results I need (but not having tested yet what happens when I add a selection or have NaN values).

foleor avatar Apr 24 '19 14:04 foleor