python - NumPy : normalize column B according to value of column A -
given numpy array [a b], a different indexes , b count values. how can normalize b values according a value?
i tried:
def normalize(np_array): normalized_array = np.empty([1, 2]) indexes= np.unique(np_array[:, 0]).tolist() index in indexes: index_array= np_array[np_array[:, 0] == index] mean_id = np.mean(index_array[:, 1]) std_id = np.std(index_array[:, 1]) if mean_id * std_id > 0: index_array[:, 1] = (index_array[:, 1] - mean_id) / std_id normalized_array = np.concatenate([normalized_array, index_array]) return np.delete(normalized_array, 0, 0) # apologies which doing job, i'm looking more noble way achieve this.
any input warmly welcome.
looks pandas can of here:
import pandas pd df = pd.dataframe({'id': [1, 1, 2, 2, 1], 'value': [10, 20, 15, 100, 12]}) byid = df.groupby('id') mean = byid.mean() std = byid.std() df['normalized'] = df.apply(lambda x: (x.value - mean.ix[x.id]) / std.ix[x.id], axis=1) print(df) output:
id value normalized 0 1 10 -0.755929 1 1 20 1.133893 2 2 15 -0.707107 3 2 100 0.707107 4 1 12 -0.377964 coming numpy array:
>>> array([[ 1, 10], [ 1, 20], [ 2, 15], [ 2, 100], [ 1, 12]]) you can create dataframe this:
>>> df = pd.dataframe({'id': a[:, 0], 'value': a[:, 1]}) >>> df id value 0 1 10 1 1 20 2 2 15 3 2 100 4 1 12
Comments
Post a Comment