簡體   English   中英

Pearsonr:TypeError:沒有找到與指定簽名匹配的循環,並且為 ufunc add 找到了轉換

[英]Pearsonr: TypeError: No loop matching the specified signature and casting was found for ufunc add

我有一個名為“df”的時間序列 Pandas dataframe。 它具有一列和以下形狀:(2000, 1)。 下面 dataframe 的頭部顯示了它的結構:

            Weight
Date    
2004-06-01  1.9219
2004-06-02  1.8438
2004-06-03  1.8672
2004-06-04  1.7422
2004-06-07  1.8203

目標

我正在嘗試使用“for 循環”來計算“權重”變量在不同時間范圍或時間滯后的百分比變化之間的相關性。 這樣做是為了評估在不同時間段內飼養牲畜的影響。

循環可以在下面找到:

from scipy.stats.stats import pearsonr

# Loop for producing combinations of different timelags and holddays 
# and calculating the pearsonr correlation and p-value of each combination 

for timelags in [1, 5, 10, 25, 60, 120, 250]:
    for holddays in [1, 5, 10, 25, 60, 120, 250]:
        weight_change_lagged = df.pct_change(periods=timelags)
        weight_change_future = df.shift(-holddays).pct_change(periods=holddays)

        if (timelags >= holddays):
            indepSet=range(0, weight_change_lagged.shape[0], holddays)
        else:
            indepSet=range(0, weight_change_lagged.shape[0], timelags)

        weight_change_lagged = weight_change_lagged.iloc[indepSet]
        weight_change_future = weight_change_future.iloc[indepSet]

        not_na = (weight_change_lagged.notna() & weight_change_future.notna()).values

        (correlation, p-value)=pearsonr(weight_change_lagged[not_na], weight_change_future[not_na])
        print('%4i %4i %7.4f %7.4f' % (timelags, holddays, correlation, p-value))

循環執行良好,但是,在計算 pearsonr 相關性和 p 值時失敗,即在本節中:

(correlation, p-value)=pearsonr(weight_change_lagged[not_na], weight_change_future[not_na])

它生成此錯誤:

TypeError:沒有為 ufunc add 找到匹配指定簽名和轉換的循環

有沒有人知道如何解決我的問題? 瀏覽了論壇,沒有找到符合我確切要求的答案。

通過隨機修補,我設法解決了我的問題,如下所示:

scipy 的 pearsonr package 僅接受 arrays 或類似數組的輸入。 這意味着:

  • Numpy arrays 的輸入變量工作。
  • Pandas系列的輸入變量工作。

但是,完整的 Pandas 變量數據幀,即使它們包含一列,也不起作用。

因此,我將有問題的代碼段編輯如下:

# Define an object containing observations that are not NA
not_na = (weight_change_lagged.notna() & weight_change_future.notna()).values

# Remove na values before inputting the data into the peasonr function (not within the function as I had done):
weight_change_lagged = weight_change_lagged[not_na]
weight_change_future = weight_change_future[not_na]

# Input Pandas Series of the Future and Lagged Variables into the function
(correlation, p-value)=pearsonr(weight_change_lagged['Weight'], weight_change_future['Weight'])

只需稍作修改,代碼就可以順利執行。

筆記:

如果使用雙方括號,如下所示,您輸入的是 pandas dataframe 不是系列,並且 pearsonr function 將拋出錯誤:

weight_change_future[['Weight']]

感謝所有試圖提供幫助的人,您的問題使我得到了答案。

就我而言,這不是數據類型問題,而是因為維度錯誤。 感謝文章https://programmersought.com/article/67803965109/

即使您在 function 中輸入 numpy arrays 也可能會遇到此錯誤。原來是引入了“額外”維度 numpy 數組導致此問題。 W

np_data.shape
>> (391, 1)

這 (.., 1 ) 是問題的根源。 您可以使用 np.squeeze(np_data) 刪除此維度以僅提取數組的值,因為

np.squeeze(np_data).shape
>> (391,)

總而言之,解決方案是使用:

pearson, pvalue = pearsonr(np.squeeze(np_data_a), np.squeeze(np_data_b))

TypeError: ufunc 'add' 不包含簽名匹配類型 dtype(' <u1') dtype('<u1') dtype('<u1')< div><div id="text_translate"><p> 我是 Python 用戶的初學者。 當我嘗試在下面編寫代碼時發生錯誤</p><pre>import numpy as np np.array(['a', 'b', 'c']) + np.array(['d','e', 'f'])</pre><pre> TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('&lt;U1') dtype('&lt;U1') dtype('&lt;U1')</pre><p> 所以我嘗試設置dtype = '&lt;U1' ,但它沒有用</p><pre>import numpy as np np.array(['a', 'b', 'c'], dtype='&lt;U1') + np.array(['d','e', 'f'], dtype='&lt;U1')</pre><p> 如何無錯誤地連接那些 np.arrays ?</p></div></u1')>

[英]TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U1') dtype('<U1') dtype('<U1')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 調用 func pearsonr 和 Got TypeError: 沒有找到匹配指定簽名和轉換的循環用於 ufunc add Seaborn TypeError: No loop match the specified signature and cast was found for ufunc add when using hue np.linalg.lstsq(X,Y)[0] - TypeError: No loop match the specified signature and cast was found for ufunc lstsq_n 類型錯誤:ufunc &#39;add&#39; 不包含具有簽名匹配類型的循環 獲取沒有匹配指定簽名和轉換錯誤的循環 ufunc &#39;add&#39; 不包含簽名匹配類型 dtype 的循環 TypeError:ufunc&#39;add&#39;不包含帶有ARIMA模型簽名的循環 TypeError: ufunc 'add' 不包含簽名匹配類型 dtype(' <u1') dtype('<u1') dtype('<u1')< div><div id="text_translate"><p> 我是 Python 用戶的初學者。 當我嘗試在下面編寫代碼時發生錯誤</p><pre>import numpy as np np.array(['a', 'b', 'c']) + np.array(['d','e', 'f'])</pre><pre> TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('&lt;U1') dtype('&lt;U1') dtype('&lt;U1')</pre><p> 所以我嘗試設置dtype = '&lt;U1' ,但它沒有用</p><pre>import numpy as np np.array(['a', 'b', 'c'], dtype='&lt;U1') + np.array(['d','e', 'f'], dtype='&lt;U1')</pre><p> 如何無錯誤地連接那些 np.arrays ?</p></div></u1')> Numpy polyfit ufunc中的Python TypeError不包含具有匹配簽名類型的循環 類型錯誤:ufunc &#39;add&#39; 不包含具有簽名匹配類型的循環 (dtype(&#39; <U21'), dtype('<U21')) -> 數據類型(&#39;
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM