2016-07-06 17 views
0

我有一個項目vs項目的相關矩陣(dm)兩個項目(例如,item0,item1)之間的值是指這些項目一起出現的次數。我如何在0到1之間調整大熊貓的所有值?用MinMaxScaler標準化相鄰矩陣(在pandas中)

from sklearn import preprocessing 
scaler = preprocessing.MinMaxScaler() 

但是,我不知道如何將比例因子應用到熊貓數據框。

enter image description here

回答

0

可以與祿的結果數組分配回數據框:

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 

你可以做同樣的(df - df.min())/(df.max() - df.min())

+0

This works too too:df.apply(lambda x:scaler.fit_transform(x)) – kitchenprinzessin