()
| 68 | |
| 69 | |
| 70 | def get_transformed_data(): |
| 71 | print("Reading in and transforming data...") |
| 72 | |
| 73 | if not os.path.exists('../large_files/train.csv'): |
| 74 | print('Looking for ../large_files/train.csv') |
| 75 | print('You have not downloaded the data and/or not placed the files in the correct location.') |
| 76 | print('Please get the data from: https://www.kaggle.com/c/digit-recognizer') |
| 77 | print('Place train.csv in the folder large_files adjacent to the class folder') |
| 78 | exit() |
| 79 | |
| 80 | df = pd.read_csv('../large_files/train.csv') |
| 81 | data = df.values.astype(np.float32) |
| 82 | np.random.shuffle(data) |
| 83 | |
| 84 | X = data[:, 1:] |
| 85 | Y = data[:, 0].astype(np.int32) |
| 86 | |
| 87 | Xtrain = X[:-1000] |
| 88 | Ytrain = Y[:-1000] |
| 89 | Xtest = X[-1000:] |
| 90 | Ytest = Y[-1000:] |
| 91 | |
| 92 | # center the data |
| 93 | mu = Xtrain.mean(axis=0) |
| 94 | Xtrain = Xtrain - mu |
| 95 | Xtest = Xtest - mu |
| 96 | |
| 97 | # transform the data |
| 98 | pca = PCA() |
| 99 | Ztrain = pca.fit_transform(Xtrain) |
| 100 | Ztest = pca.transform(Xtest) |
| 101 | |
| 102 | plot_cumulative_variance(pca) |
| 103 | |
| 104 | # take first 300 cols of Z |
| 105 | Ztrain = Ztrain[:, :300] |
| 106 | Ztest = Ztest[:, :300] |
| 107 | |
| 108 | # normalize Z |
| 109 | mu = Ztrain.mean(axis=0) |
| 110 | std = Ztrain.std(axis=0) |
| 111 | Ztrain = (Ztrain - mu) / std |
| 112 | Ztest = (Ztest - mu) / std |
| 113 | |
| 114 | return Ztrain, Ztest, Ytrain, Ytest |
| 115 | |
| 116 | |
| 117 | def get_normalized_data(): |
no test coverage detected