I have a dataframe with a column of weights and one of values. I'd need:
Is there an easy way to achieve this?I have found a way, but it seems a bit cumbersome:
Basically I'm looking for a better way to produce a more smoothed curve.
and my code, with some random data, is:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.interpolate import make_interp_spline, BSpline
n=int(1e3)
df=pd.DataFrame()
np.random.seed(10)
df['w']=np.arange(0,n)
df['v']=np.random.randn(n)
df['ranges']=pd.cut(df.w, bins=50)
df['one']=1.
def func(x, df):
# func() gets called within a lambda function; x is the row, df is the entire table
b1= x['one'].sum()
b2 = x['w'].mean()
b3 = x['v'].mean()
b4=( x['w'] * x['v']).sum() / x['w'].sum() if x['w'].sum() >0 else np.nan
cols=['# items','avg w','avg v','weighted avg v']
return pd.Series( [b1, b2, b3, b4], index=cols )
summary = df.groupby('ranges').apply(lambda x: func(x,df))
sns.set(style='darkgrid')
fig,ax=plt.subplots(2)
sns.lineplot(summary['avg w'], summary['weighted avg v'], ax=ax[0])
ax[0].set_title('line plot')
xnew = np.linspace(summary['avg w'].min(), summary['avg w'].max(),100)
spl = make_interp_spline(summary['avg w'], summary['weighted avg v'], k=5) #BSpline object
power_smooth = spl(xnew)
sns.lineplot(xnew, power_smooth, ax=ax[1])
ax[1].set_title('not-so-interpolated plot')
The first part of your question is rather easy to do.
I'm not sure what you mean with the second part. Do you want a (simplified) reproduction of your code or a new approach that better fits your need?
Anyway i had to look at your code to understand what you mean by weighting the values. I think people would normally expect something different from the term (just as a warning).
Here's the simplified version of your approach:
df['prod_v_w'] = df['v']*df['w']
weighted_avg_v = df.groupby(pd.cut(df.w, bins=50))[['prod_v_w','w']].sum()\
.eval('prod_v_w/w')
print(np.allclose(weighted_avg_v, summary['weighted avg v']))
Out[18]: True
I think you're using few values for the interpolation, by changing xnew = np.linspace(summary['avg w'].min(), summary['avg w'].max(),100)
to xnew = np.linspace(summary['avg w'].min(), summary['avg w'].max(),500)
I get the following:
And changint the spline degree to k=2
i get the following:
I think a good starting point for the interpolation could be n/2
and k=2
as it presents less data deformation. Hope it helps.
If I'm understanding correctly, you're trying to recreate a rolling average.
This is already a capability of Pandas dataframes, using the rolling
function:
dataframe.rolling(n).mean()
where n
is the number of adjacent points used in the 'window' or 'bin' for the average, so you can tweak it for different degrees of smoothness.
You can find examples here:
I think this is a solution to what you are seeking. It uses rolling window as others have suggested. a little bit more work was needed to get it working properly.
df["w*v"] = df["w"] * df["v"]
def rolling_smooth(df,N):
df_roll = df.rolling(N).agg({"w":["sum","mean"],"v":["mean"],"w*v":["sum"]})
df_roll.columns = [' '.join(col).strip() for col in df_roll.columns.values]
df_roll['weighted avg v'] = np.nan
cond = df_roll['w sum'] > 0
df_roll.loc[cond,'weighted avg v'] = df_roll.loc[cond,'w*v sum'] / df_roll.loc[cond,'w sum']
return df_roll
df_roll_100 = rolling_smooth(df,100)
df_roll_200 = rolling_smooth(df,200)
plt.plot(summary['avg w'], summary['weighted avg v'],label='original')
plt.plot(df_roll_100["w mean"],df_roll_100["weighted avg v"],label='rolling N=100')
plt.plot(df_roll_200["w mean"],df_roll_200["weighted avg v"],label='rolling N=200')
plt.legend()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.