2016-04-13 38 views
1

我參考這篇文章,Get dot-product of dataframe with vector, and return dataframe, in Pandas,使用DataFrame.mul。

我的問題的代碼是這樣
使用DataFrame.mul時出錯,涉及到ndarray

df.mul(weight) 

其中重量是具有形狀(17L,1L) 'numpy.ndarray' 的數據類型,並且打印結果是

[[ 2.37005330e-07] 
[ 2.80515078e-07] 
[ 2.80267682e-07] 
[ 2.79124521e-07] 
[ 2.01799847e-07] 
[ 2.71495529e-07] 
[ 2.81640566e-07] 
[ 2.30099310e-07] 
[ 1.95221059e-07] 
[ 2.10244387e-07] 
[ 2.82483251e-07] 
[ 2.29050342e-07] 
[ 9.99996381e-01] 
[ 8.95340469e-08] 
[ 3.90767576e-08] 
[ 2.31231511e-07] 
[ 2.79852240e-07]] 

其中Df是一個形狀爲[20208 rows x 17列]的數據框對象,其打印結果類似於

     12&88 17&123 .... 
modified datetime       
2015-09-07 09:19:00 1.000000 1.000000 .... 
2015-09-07 09:30:00 1.000000 1.000000 .... 
2015-09-07 09:31:00 1.000000 0.974714 .... 
2015-09-07 09:32:00 1.000000 0.978203 .... 
2015-09-07 09:33:00 1.000000 0.978203 .... 
2015-09-07 09:34:00 1.000000 0.990576 .... 
.... 

但是,當我執行df.mul(重量),它發生

ValueError: Shape of passed values is (1, 17), indices imply (17, 20208) 

我試圖更簡單的陣列形狀(17L)並且沒有使用df.mul.so不知是否應該改變問題重量到ndarray到陣列,但對我來說很難。如何改變或者有沒有更好的主意來解決這個問題?非常感謝你的幫助!


這裏是我的原代碼

weight, means, stds = optimal_portfolio(result_framea.transpose()) 

    c , b= test.pairs_trade(load_path, sNo_list[0]) 
    result_frame = pd.DataFrame(index = c.index) 
    for i, sNo in enumerate(sNo_list): 
     c,b = test.pairs_trade(load_path, sNo) 
     result_frame[sNo[0]+'&'+sNo[1]] = c['returns'] 
    df=result_frame.fillna(method='pad') 

各地都很好,直到後df.mul(重量)的時刻。再次謝謝你!

+0

你可以試試'df.mul(weight,axis = 0)'基本上它使用次軸來對齊由於廣播規則 – EdChum

+0

我也嘗試axis = 1後axis = 0,仍然是相同的值錯誤。 –

+0

但是當我隨機設置一個像數組一樣的新「權重」([1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1]) ,它可以工作。 –

回答

0

help(pd.DataFrame.mul)

mul(self, other, axis='columns', level=None, fill_value=None) unbound pandas.core.frame.DataFrame method

Multiplication of dataframe and other , element-wise (binary operator mul).

Equivalent to dataframe * other , but with support to substitute a fill_value for missing data in one of the inputs.

這表明,在最簡單的情況df.mul只會執行相應陣列的numpy的風格乘法。所以,你試圖將形狀爲(20208,17)的數組與形狀(17,1)中的一個相乘。這不起作用。

array broadcasting在numpy中的工作方式是,具有某些奇異維度的數組可以通過numpy自動擴展,以便將它們與算術運算中的其他更大的數組進行匹配。值得注意的是,如果其中一個陣列具有較小的尺寸,則假定單體尺寸爲,其中前導單元爲

因此,例如,下面的陣列形狀可以成倍/添加/分/等一起沒有問題:

  • (1,17)(20208,17)因爲非單尺寸相符
  • (17,)(20208,17)因爲首先隱含地與(1,17)兼容(假定前導單獨尺寸)
  • (5,1,17)and(1,20208,17)(or just(20208,17)`)

以下不能播在一起:

  • (1,16)(20208,17)因爲有尺寸不符
  • (16,) and(20208,17)because the mismatch is there even after implicitly expanding the first one to shape(1,16)`
  • (17,1)(20208,17)現在顯而易見的原因

問題是,熊貓顯示

ValueError: Shape of passed values is (1, 17), indices imply (17, 20208)

同樣看起來像這樣在numpy的(嘗試np.random.rand(17,1)*np.random.rand(20208,17)):神祕的錯誤消息,你在你的問題引述

ValueError: operands could not be broadcast together with shapes (17,1) (20208,17)

後者的錯誤是清澈的,並會可能救你很多頭部劃傷。

解決方案很簡單:reshape您的形狀爲(17,1)(2d數組中的列向量)的重量數組形成(17,)(1d數組)。這可以與您的大型陣列廣播。要做到這一點,只需撥打reshape-1尺寸參數,告訴numpy的,以確定您的一維數組的長度:

df.mul(weight.reshape(-1)) 

注意,resut將是相同的shape的數據df,但每列將乘以從weight相應的元素。