2013-07-03 23 views
1
from scipy import ndimage 
import numpy as np 

with open("Data.dat", "r+") as f: 
    content = f.readlines() 
    content = [s.strip() for s in content] 
    content = np.asarray(content) 
    weights = np.array([1, 2, 4, 2, 1]) 
    final = np.empty(content) 
    ndimage.filters.convolve1d(content, weights, -1, final, 'reflect', 0.0, 0.0) 

這裏npnumpy包。 content的長度是750,所以試圖用np.empty()初始化輸出數組的形狀,因爲我不想在那裏有任何值。ValueError異常,而使用計算卷積SciPy的

但是當我跑我得到這個錯誤:

Traceback (most recent call last): 
File "C:\Users\Animesh\Desktop\Smoothing.py", line 18, in <module> 
final = np.empty(content) 
ValueError: sequence too large; must be smaller than 32 

應該怎樣做?

回答

2

爲了使final相同的形狀和D型細胞作爲content的一個空數組,使用:

final = np.empty_like(content) 

關於TypeError: integer argument expected, got float

雖然convolve1d文檔字符串說

origin : scalar, optional 
    The `origin` parameter controls the placement of the filter. 
    Default 0.0. 

origin參數必須是整數,而不是浮點數。

這裏是其運行沒有錯誤的示例:

import scipy.ndimage as ndimage 
import numpy as np 

# content = np.genfromtxt('Data.dat') 
content = np.asarray(np.arange(750)) 
weights = np.array([1, 2, 4, 2, 1]) 
final = np.empty_like(content) 
ndimage.convolve1d(content, weights, axis=-1, output=final, mode='reflect', cval=0.0, 
        origin=0 
        # origin=0.0 # This raises TypeError 
        ) 
print(final) 

取消註釋origin=0.0引發類型錯誤。


關於

with open("Data.dat", "r+") as f: 
    content = f.readlines() 
    content = [s.strip() for s in content] 
    content = np.asarray(content) 

這使得content字符串數組。既然你正在卷積,你必須要一個數字數組。因此,不是用

content = np.genfromtxt('Data.dat') 
+0

替換上面我想,但我得到這個錯誤'類型錯誤:預期的整數參數,得到了float' –

+0

我正在編輯的問題,以準確顯示我在做什麼! –

+0

將最後一個參數從「0.0」更改爲「0」。原點參數必須是整數。 – unutbu