Devon  Oliver
Devon Oliver

Reputation: 345

Python Pandas: bootstrap confidence limits by row rather than entire dataframe

What I am trying to do is to get bootstrap confidence limits by row regardless of the number of rows and make a new dataframe from the output.I currently can do this for the entire dataframe, but not by row. The data I have in my actual program looks similar to what I have below:

    0   1   2
0   1   2   3
1   4   1   4
2   1   2   3
3   4   1   4

I want the new dataframe to look something like this with the lower and upper confidence limits:

    0   1   
0   1   2   
1   1   5.5 
2   1   4.5 
3   1   4.2 

The current generated output looks like this:

     0   1
 0  2.0 2.75

The python 3 code below generates a mock dataframe and generates the bootstrap confidence limits for the entire dataframe. The result is a new dataframe with just 2 values, a upper and a lower confidence limit rather than 4 sets of 2(one for each row).

import pandas as pd
import numpy as np
import scikits.bootstrap as sci

zz = pd.DataFrame([[[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]],
               [[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]]])
print(zz)

x= zz.dtypes
print(x)

a = pd.DataFrame(np.array(zz.values.tolist())[:, :, 0],zz.index, zz.columns)
print(a)
b = sci.ci(a)
b = pd.DataFrame(b)
b = b.T
print(b)

Thank you for any help.

Upvotes: 1

Views: 1522

Answers (2)

cge
cge

Reputation: 9890

scikits.bootstrap operates by assuming that data samples are arranged by row, not by column. If you want the opposite behavior, just use the transpose, and a statfunction that doesn't combine columns.

import pandas as pd
import numpy as np
import scikits.bootstrap as sci

zz = pd.DataFrame([[[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]],
               [[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]]])
print(zz)

x= zz.dtypes
print(x)

a = pd.DataFrame(np.array(zz.values.tolist())[:, :, 0],zz.index, zz.columns)
print(a)
b = sci.ci(a.T, statfunction=lambda x: np.average(x, axis=0))
print(b.T)

Upvotes: 1

Devon  Oliver
Devon Oliver

Reputation: 345

Below is the answer I ended up figuring out to create bootstrap ci by row.

import pandas as pd
import numpy as np
import numpy.random as npr

zz = pd.DataFrame([[[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]],
                  [[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]]])

x= zz.dtypes

a = pd.DataFrame(np.array(zz.values.tolist())[:, :, 0],zz.index, zz.columns)
print(a)

def bootstrap(data, num_samples, statistic, alpha):
    n = len(data)
    idx = npr.randint(0, n, (num_samples, n))
    samples = data[idx]
    stat = np.sort(statistic(samples, 1))
    return (stat[int((alpha/2.0)*num_samples)],
            stat[int((1-alpha/2.0)*num_samples)])

cc = list(a.index.values) # informs generator of the number of rows

def bootbyrow(cc):
    for xx in range(1):
            xx = list(a.index.values)
            for xx in range(len(cc)):
                k = a.apply(lambda y: y[xx])
                k = k.values
                for xx in range(1):
                    kk = list(bootstrap(k,10000,np.mean,0.05))   
                    yield list(kk)


abc = pd.DataFrame(list(bootbyrow(cc))) #bootstrap ci by row

# the next 4 just show that its working correctly
a0 = bootstrap((a.loc[0,].values),10000,np.mean,0.05)   
a1 = bootstrap((a.loc[1,].values),10000,np.mean,0.05)
a2 = bootstrap((a.loc[2,].values),10000,np.mean,0.05)  
a3 = bootstrap((a.loc[3,].values),10000,np.mean,0.05)  

print(abc)
print(a0)
print(a1)
print(a2)
print(a3)

Upvotes: 0

Related Questions