text_plain_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.shape

data_for_svd = train[:, 1:]
data_for_svd.shape

data_mean = np.mean(data_for_svd, axis=0)
data_for_svd -= data_mean

cov_matrix = np.dot(data_for_svd.T, data_for_svd) / data_for_svd.shape[0]

U, S, _ = np.linalg.svd(cov_matrix)

S_thr = 0.83
S_cumsum = 0
for i in range(S.shape[0]):
    S_cumsum += S[i] / np.sum(S)
    if S_cumsum >= S_thr:
        n_comp = i + 1
        break

data_reduced = np.dot(data_for_svd, U[:, :n_comp])
data_reduced.shape