我想创建一个高斯模糊矩阵。我从http://www.labri.fr/perso/nrougier/teaching/numpy/numpy.html向量化与numpy
修改代码dev_data具有784个像素特征的行,并且我希望模糊像素周围的邻居以及像素本身。当我们沿着外边缘(第1行,-1,第1列,第-1行)时,丢弃任何超出边界的邻居。我不太清楚如何做这个丢弃。
代码:
# Initialize a new feature array with the same shape as the original data.
blurred_dev_data = np.zeros(dev_data.shape)
#we will reshape the 784 feature-long rows into 28x28 matrices
for i in range(dev_data.shape[0]):
reshaped_dev_data = np.reshape(dev_data[i], (28,28))
#the purpose of the reshape is to use the average of the 8 pixels + the pixel itself to blur
for idx, pixel in enumerate(reshaped_dev_data):
pixel = np.mean(reshaped_dev_data[idx-1:idx-1,idx-1:idx-1] + reshaped_dev_data[idx-1:idx-1,idx:idx] + reshaped_dev_data[idx-1:idx-1,idx+1:] +
reshaped_dev_data[idx:idx,idx-1:idx-1] + reshaped_dev_data[idx:idx,idx:idx] + reshaped_dev_data[idx:idx,idx+1:] +
reshaped_dev_data[idx+1: ,idx-1:idx-1] + reshaped_dev_data[idx+1: ,idx:idx] + reshaped_dev_data[idx+1: ,idx+1:])
blurred_dev_data[i,:] = reshaped_dev_data.ravel()
我得到一个错误与指数:
ValueError: operands could not be broadcast together with shapes (0,0) (0,27)
这不是一个indexerror,所以我不太清楚我在做什么错在这里/如何修理它。
编辑'reshaped_dev_data [idx-1:idx-1,idx-1:idx-1]'到'reshaped_dev_data [idx-1,idx-1]'等等。 – Divakar
谢谢。现在我得到了我期待的界限错误。你知道一个忽略出界指数的好方法吗? –
我建议使用高斯模糊滤镜,而不是 - https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.filters.gaussian_filter.html – Divakar