weighted-average

Calculating weighted average in Pandas using NumPy function

谁都会走 提交于 2021-01-27 23:12:00
问题 Assume we have a pandas dataframe like this: a b id 36 25 2 40 25 3 46 23 2 40 22 5 42 20 5 56 39 3 I would like to perform a operation (a div b), then group by id and finally calculate a weighted average, using "a" as weights. It work's when I only calculate the mean. import pandas as pd import numpy as np df = pd.read_csv('file', sep='\s+') a = (df['a'].div(df['b'])).groupby(df['id']).mean() # work fine b = (df['a'].div(df['b'])).groupby(df['dd']).apply(lambda x: np.average(x ??? ), weights

Exponential Moving Average in php

喜你入骨 提交于 2021-01-27 18:54:10
问题 I want to calculate the EMA (Exponential Moving Average) value in PHP. I've tried with following code but it's giving me 500 error. $real = array(12,15,17,19,21,25,28,12,15,16); $timePeriod = 3; $data = trader_ema($real,$timePeriod); var_dump($data); PHP: EMA calculation function trader-ema Tried with long time Googling but not getting any help on this in PHP. So, I've no clue what needs to be done to calculate the EMA value. Edit-1: Installed extensions I've installed all the necessary

Pandas/numpy weighted average ZeroDivisionError

末鹿安然 提交于 2021-01-03 07:10:05
问题 Creating a lambda function to calculate weighted average and sending that to a dictionary. wm = lambda x: np.average(x, weights=df.loc[x.index, 'WEIGHTS']) # Define a dictionary with the functions to apply for a given column: f = {'DRESS_AMT': 'max', 'FACE_AMT': 'sum', 'Other_AMT': {'weighted_mean' : wm}} # Groupby and aggregate with dictionary: df2=df.groupby(['ID','COL1'], as_index=False).agg(f) This code works but the weighted average lambda function fails if the weights add up to 0

Pandas/numpy weighted average ZeroDivisionError

只愿长相守 提交于 2021-01-03 07:07:27
问题 Creating a lambda function to calculate weighted average and sending that to a dictionary. wm = lambda x: np.average(x, weights=df.loc[x.index, 'WEIGHTS']) # Define a dictionary with the functions to apply for a given column: f = {'DRESS_AMT': 'max', 'FACE_AMT': 'sum', 'Other_AMT': {'weighted_mean' : wm}} # Groupby and aggregate with dictionary: df2=df.groupby(['ID','COL1'], as_index=False).agg(f) This code works but the weighted average lambda function fails if the weights add up to 0

Knn give more weight to specific feature in distance

六眼飞鱼酱① 提交于 2020-08-17 12:11:19
问题 I'm using the Kobe Bryant Dataset. I wish to predict the shot_made_flag with KnnRegressor. I've used game_date to extract year and month features: # covert season to years kobe_data_encoded['season'] = kobe_data_encoded['season'].apply(lambda x: int(re.compile('(\d+)-').findall(x)[0])) # add year and month using game_date kobe_data_encoded['year'] = kobe_data_encoded['game_date'].apply(lambda x: int(re.compile('(\d{4})').findall(x)[0])) kobe_data_encoded['month'] = kobe_data_encoded['game

Get weighted average summary data column in new pandas dataframe from existing dataframe based on other column-ID

只谈情不闲聊 提交于 2020-05-30 08:00:06
问题 Somewhat similar question to an earlier question I had here: Get summary data columns in new pandas dataframe from existing dataframe based on other column-ID However, instead of just taking the sum of datapoints, I wanted to have the weighted average in an extra column. I'll repeat and rephrase the question: I want to summarize the data in a dataframe and add the new columns to another dataframe. My data contains appartments with an ID-number and it has surfaces and U-values for each room in

Weighted Frequency Table in R

笑着哭i 提交于 2020-02-05 04:35:27
问题 I'm looking to create a frequency table in R for a certain variable (INTERVIEW_DAY) , but taking into account another variable as weight (WEIGHT). I've tried to do that with the package data.table . I would love to do it with the R-Base Package though. Below you'll find the type of table I want, but still not weighted, which is what I'm looking to learn. Data (variables TUCASEID, INTERVIEW_DAY, and WEIGHT): TUCASEID INTERVIEW_DAY WEIGHT 1 2.00301e+13 5 8155462.7 2 2.00301e+13 6 1735322.5 3 2

Apply sum product on columns of a dataframe in rolling windows

浪尽此生 提交于 2020-01-06 05:25:23
问题 I have a set of defined weights and I want to calculate the weighted sum of returns in rolling windows on a time series dataframe. I believe we would use rollapplyr here, but I am unsure how to perform rolling window function across each row of the dataframe. Find below dput output of a sample of the data: tempVar <- structure(c(NA, -0.0081833512947922, 0.00508150903899551, -0.0072202479734873, 0.00345258369231161, NA, 0, -0.00847462699097257, -0.00794638265247283, 0.00445091892889238, NA, NA