Created
January 17, 2019 07:34
-
-
Save jjone36/2b9120535a090d48c082e64fba3ce848 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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