def test_matrix_centred_on_mean_has_dat_0(): d = ClassroomHeights(10, 10) rows = [] n_rows = np.shape(d.m)[0] for i in range(n_rows): row = d.m[i, :] mean = to_test.row_mean_of(row) rows.append(row - mean) m = np.asmatrix(np.stack(rows)) assert math.isclose(np.linalg.det(m), 0., abs_tol=1e-3)
import distributions.covariance as cov import graphics.files as f import graphics.heatmap as g from data.ClassroomHeights import ClassroomHeights if __name__ == "__main__": d = ClassroomHeights(10, 100) (height, width) = np.shape(d.m) # add some dependency rows = [] for i in range(height): row = d.m[i, :] if r.uniform(0, 1) < 0.05: ignore_1 = row[:, 0:-1] mean = cov.row_mean_of(ignore_1) centred = ignore_1 - mean new_row = np.append([0.], centred) rows.append(np.asmatrix(new_row)) else: rows.append(row - cov.row_mean_of(row)) c = cov.squared_with_bessel(np.asmatrix(np.stack(rows)).T) p = np.linalg.inv(c) (ws, vs) = np.linalg.eig(c) print('Number of eigenvalues = '.format(len(ws))) for w in ws: print('eigenvalue = {}'.format(w)) g.add_heatmap(d.m, "Data")
def test_mean_centred_matrix_needs_conditioning(): x = to_test.row_mean_of(m) with pytest.raises(Exception) as e: np.linalg.inv(x) pytest.fail(e)
def test_mean_of_matrix(): mean = to_test.row_mean_of(m) assert np.array_equal(mean.T, np.asmatrix([3.5, 1.5]))
def test_mean_of_row_vector(): assert to_test.row_mean_of(v) == 2.5
def test_mean_of_col_vector(): mean = to_test.row_mean_of(v.T) assert np.array_equal(mean, v.T), "actual = {}".format(mean)