image_output_1.png
import cv2

import numpy as np

train = np.loadtxt('../input/train.csv', delimiter=',', skiprows=1)

train_label = train[:, 0]
train_img = np.resize(train[:, 1:], (train.shape[0], 28, 28))

train_img.shape

train_sobel_x = np.zeros_like(train_img)
train_sobel_y = np.zeros_like(train_img)
for i in range(len(train_img)):
    train_sobel_x[i] = cv2.Sobel(train_img[i], cv2.CV_64F, dx=1, dy=0, ksize=3)
    train_sobel_y[i] = cv2.Sobel(train_img[i], cv2.CV_64F, dx=0, dy=1, ksize=3)

# Гистограммы вычисляются с учетом длины вектора градиента
train_hist = np.zeros((len(train_img), 16))
for i in range(len(train_img)):
    hist, borders = np.histogram(train_theta[i],
                                 bins=16,
                                 range=(0., 2. * np.pi),
                                 weights=train_g[i])
    train_hist[i] = hist

train_hist = train_hist / np.linalg.norm(train_hist, axis=1)[:, None]

train_hist.shape