Skip to content

Instantly share code, notes, and snippets.

@jjone36
Created January 17, 2019 07:34
Show Gist options
  • Save jjone36/2b9120535a090d48c082e64fba3ce848 to your computer and use it in GitHub Desktop.
Save jjone36/2b9120535a090d48c082e64fba3ce848 to your computer and use it in GitHub Desktop.
import numpy as np
# what is convolution? How to capture the edge by padding?
array = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
kernel = np.array([-1, 1])
conv = np.array([0, 0, 0, 0, 0, 0, 0, 0])
# convolution
conv[1] = (kernel * array[0:2]).sum()
conv[2] = (kernel * array[1:3]).sum()
conv[3] = (kernel * array[2:4]).sum()
# padding
for i in range(len(conv)):
conv[i] = (kernel * array[i:i+2]).sum()
print(conv)
# another example
array_2 = np.array([0, 0, 1, 1, 0, 0, 1, 1, 0, 0])
conv_2 = np.array([0, 0, 0, 0, 0, 0, 0, 0])
for i in range(len(conv_2)):
conv_2[i] = (kernel * array_2[i:i+2]).sum()
print(conv_2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment