3 examples of 'pandas groupby weighted average' in Python

Every line of 'pandas groupby weighted average' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
236def mean(self):
237 # TODO, there is a lot of copy-paste with the code above
238 # TODO, we should probably define groupby.aggregate
239 func = _accumulate_groupby_mean
240 start = (0, 0)
241 if isinstance(self.grouper, Streaming):
242 func = partial(func, index=self.index)
243 example = self.root.example.groupby(self.grouper.example)
244 if self.index is not None:
245 example = example[self.index]
246 example = example.mean()
247 stream = self.root.stream.zip(self.grouper.stream)
248 stream = stream.accumulate(func, start=start, returns_state=True)
249 else:
250 func = partial(func, grouper=self.grouper, index=self.index)
251 example = self.root.example.groupby(self.grouper)
252 if self.index is not None:
253 example = example[self.index]
254 example = example.mean()
255 stream = self.root.stream.accumulate(func, start=start,
256 returns_state=True)
257 if isinstance(example, pd.DataFrame):
258 return StreamingDataFrame(stream, example)
259 else:
260 return StreamingSeries(stream, example)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

202def aggregate(group_reports):
203 summary = {}
204 top_model = max(group_reports, key=lambda r: r['Dev First Sentence'])
205 summary['Avg First Sentence'] = np.mean([r['First Sentence'] for r in group_reports])
206 summary['Std First Sentence'] = np.std([r['First Sentence'] for r in group_reports])
207 summary['Avg Full Question'] = np.mean([r['Full Question'] for r in group_reports])
208 summary['Std Full Question'] = np.std([r['Full Question'] for r in group_reports])
209 summary['First Sentence'] = top_model['First Sentence']
210 summary['Full Question'] = top_model['Full Question']
211 summary['Dev First Sentence'] = top_model['Dev First Sentence']
212 summary['Dev Full Question'] = top_model['Dev Full Question']
213 summary['first_df'] = top_model['first_df']
214 summary['full_df'] = top_model['full_df']
215 summary['char_df'] = top_model['char_df']
216 summary['fold'] = top_model['fold']
217 summary['guesser_name'] = top_model['guesser_name']
218 summary['random_seed'] = top_model['random_seed']
219 summary['wiki'] = top_model['wiki']
220 summary['training_time'] = top_model['training_time']
221
222 stable_scores = []
223 eager_scores = []
224 for _, group in top_model['char_df'].sort_values('score', ascending=False).groupby('qanta_id'):
225 group = group.groupby(['char_index']).first().reset_index()
226 stable, eager = compute_curve_score(group)
227 stable_scores.append(stable)
228 eager_scores.append(eager)
229 summary['curve_score_stable'] = np.mean(stable_scores)
230 summary['curve_score_eager'] = np.mean(eager_scores)
231 return summary
59def average(average_window, data):
60 window = []
61 newdata = []
62 for v in data:
63 window.append(v)
64 if len(window) == average_window:
65 newdata.append(sum(window)/average_window)
66 del window[0]
67 return newdata

Related snippets