# How to calculate mean and standard deviation of all features in a class identified by k-nearest neighbors?

I have classified my data into several neighborhoods using k nearest neighbors. I need to efficiently calculate the mean and standard deviation for all features of data points belonging to a particular neighborhood. I am using sklearn.kneighbors.

• Do you want the spatial mean and standard deviation? Like the average in each dimension of the position? Mar 12 '19 at 23:42

If you append the predicted neighbourhood onto your data df (let's call this neighbourhood), then using groupby and transform within a loop should do the trick.
features = [var_1,var_2,...] # a list of the features to run over