text_plain_output_1.png
import numpy as np

import pandas as pd

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
numClasses = 10
numEig = 28 * 28
picSize = 28 * 28
trainData = pd.read_csv('../input/train.csv')
testData = pd.read_csv('../input/test.csv')
trainData.sort_values(by=['label'], inplace=True)
trainY = trainData.iloc[:, 0].values
trainX = trainData.iloc[:, 1:].values
testX = testData.iloc[:, :].values
trainMean = np.mean(trainX, axis=0)
trainX = trainX - trainMean
cov = np.cov(trainX.T)
w, v = np.linalg.eig(cov)
ws = np.sort(w)
ws = ws[::-1]
for i in range(0, numEig):
    v[:, i] = v[:, np.where(w == ws[i])[0][0]]
v = v[:, :numEig].real
del trainData, testData, cov, w, ws
omega = np.zeros((numClasses, numEig, picSize))
for i in range(0, numClasses):
    trainDigit = trainX[np.where(trainY == i)]
    print('calculating weights for digit %d, samples %d' % (i, len(trainDigit)))
    for k in range(0, len(trainDigit)):
        tmp = v.T * trainDigit[k]
        omega[i] += tmp
    omega[i] /= len(trainDigit)