python - Normalize adjency matrix (in pandas) with MinMaxScaler -
i have adjency matrix (dm) of items vs items; value between 2 items (e.g., item0,item1) refers number of times these items appear together. how can scale values in pandas between 0 1?
from sklearn import preprocessing scaler = preprocessing.minmaxscaler()
however, not sure how apply scaler pandas data frame.
you can assign resulting array dataframe loc:
df = pd.dataframe(np.random.randint(1, 5, (5, 5))) df out[277]: 0 1 2 3 4 0 2 3 2 3 1 1 2 3 4 4 2 2 2 3 4 3 2 3 1 1 2 1 4 4 4 2 2 3 1 df.loc[:,:] = scaler.fit_transform(df) df out[279]: 0 1 2 3 4 0 0.333333 1.0 0.0 0.666667 0.000000 1 0.333333 1.0 1.0 1.000000 0.333333 2 0.333333 1.0 1.0 0.666667 0.333333 3 0.000000 0.0 0.0 0.000000 1.000000 4 1.000000 0.5 0.0 0.666667 0.000000
you can same (df - df.min()) / (df.max() - df.min())
.
Comments
Post a Comment