I am trying to create a Gaussian blurred matrix. I am modifying code from http://www.labri.fr/perso/nrougier/teaching/numpy/numpy.html
dev_data has rows of 784 pixel features, and I would like to blur with the neighbors around the pixel in question along with the pixel itself. When we're along the outer edges (rows 1,-1, columns 1,-1), discard any out of bounds neighbors. I am not quite sure how to do this discarding.
Code:
# 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()
I get an error with the index:
ValueError: operands could not be broadcast together with shapes (0,0) (0,27)
It's not an indexerror, so I'm not quite sure what I'm doing wrong here/how to fix it.