def factor(N, primeset): roots, rowlist = find_candidates(N, primeset) M = transformation_rows(rowlist) m = rowdict2mat(M) a = rowdict2mat(rowlist) ma = mat2rowdict(m * a) zero_pos = [i for i in ma if ma[i] == 0 * ma[0]] for i in zero_pos: a, b = find_a_and_b(M[i], roots, N) factor1, factor2 = gcd(a + b, N), gcd(a - b, N) if factor1 * factor2 == N: return factor1, factor2
def exchange(S, A, z): ''' Input: - S: a list of vectors, as instances of your Vec class - A: a list of vectors, each of which are in S, with len(A) < len(S) - z: an instance of Vec such that A+[z] is linearly independent Output: a vector w in S but not in A such that Span S = Span ({z} U S - {w}) Example: >>> S = [list2vec(v) for v in [[0,0,5,3],[2,0,1,3],[0,0,1,0],[1,2,3,4]]] >>> A = [list2vec(v) for v in [[0,0,5,3],[2,0,1,3]]] >>> z = list2vec([0,2,1,1]) >>> exchange(S, A, z) == Vec({0, 1, 2, 3},{0: 0, 1: 0, 2: 1, 3: 0}) True ''' T=list() for e in S: if e not in A: T.append(e) U = superset_basis(A, S) for w in T: U.remove(w) mat = rowdict2mat(U) s = solve(mat,z) if z*s ==0: return w
def read_training_data(fname, D=None): """Given a file in appropriate format, and given a set D of features, returns the pair (A, b) consisting of a P-by-D matrix A and a P-vector b, where P is a set of patient identification integers (IDs). For each patient ID p, - row p of A is the D-vector describing patient p's tissue sample, - entry p of b is +1 if patient p's tissue is malignant, and -1 if it is benign. The set D of features must be a subset of the features in the data (see text). """ file = open(fname) params = ["radius", "texture", "perimeter","area","smoothness","compactness","concavity","concave points","symmetry","fractal dimension"]; stats = ["(mean)", "(stderr)", "(worst)"] feature_labels = set([y+x for x in stats for y in params]) feature_map = {params[i]+stats[j]:j*len(params)+i for i in range(len(params)) for j in range(len(stats))} if D is None: D = feature_labels feature_vectors = {} patient_diagnoses = {} for line in file: row = line.split(",") patient_ID = int(row[0]) patient_diagnoses[patient_ID] = -1 if row[1]=='B' else +1 feature_vectors[patient_ID] = Vec(D, {f:float(row[feature_map[f]+2]) for f in D}) return rowdict2mat(feature_vectors), Vec(set(patient_diagnoses.keys()), patient_diagnoses)
def read_training_data(fname, features=None): """Given a file in appropriate format, returns the triple (feature_vectors, patient_diagnoses, D) feature_vectors is a dictionary that maps integer patient identification numbers to D-vectors where D is the set of feature labels, and patient_diagnoses is a dictionary mapping patient identification numbers to {+1, -1}, where +1 indicates malignant and -1 indicates benign. """ file = open(fname) params = [ "radius", "texture", "perimeter", "area", "smoothness", "compactness", "concavity", "concave points", "symmetry", "fractal dimension" ] stats = ["(mean)", "(stderr)", "(worst)"] feature_labels = set([y + x for x in stats for y in params]) feature_map = { params[i] + stats[j]: j * len(params) + i for i in range(len(params)) for j in range(len(stats)) } if features is None: features = feature_labels feature_vectors = {} patient_diagnoses = {} for line in file: row = line.split(",") patient_ID = int(row[0]) patient_diagnoses[patient_ID] = -1 if row[1] == 'B' else +1 feature_vectors[patient_ID] = Vec( features, {f: float(row[feature_map[f] + 2]) for f in features}) return rowdict2mat(feature_vectors), Vec(set(patient_diagnoses.keys()), patient_diagnoses)
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] from matutil import mat2rowdict, rowdict2mat a = mat2rowdict(A) m = rowdict2mat({k: v * B for (k, v) in a.items()}) return m
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] row_dict = mat2rowdict(A) res = dict() for r in row_dict.keys(): res[r] = row_dict[r] * B return rowdict2mat(res)
def grayscale(): ''' Input: None Output: 3x3 greyscale matrix. ''' labels = {'r', 'g', 'b'} return rowdict2mat({ i : Vec(labels, {'r' : 77/256, 'g' : 151/256, 'b' : 28/256} ) for i in labels })
def read_training_data(fname, D=None): """Given a file in appropriate format, and given a set D of features, returns the pair (A, b) consisting of a P-by-D matrix A and a P-vector b, where P is a set of patient identification integers (IDs). For each patient ID p, - row p of A is the D-vector describing patient p's tissue sample, - entry p of b is +1 if patient p's tissue is malignant, and -1 if it is benign. The set D of features must be a subset of the features in the data (see text). """ file = open(fname) params = ["radius", "texture", "perimeter","area","smoothness","compactness","concavity","concave points",\ "symmetry","fractal dimension"] stats = ["(mean)", "(stderr)", "(worst)"] feature_labels = set([y + x for x in stats for y in params]) feature_map = { params[i] + stats[j]: j * len(params) + i for i in range(len(params)) for j in range(len(stats)) } if D is None: D = feature_labels feature_vectors = {} patient_diagnoses = {} for line in file: row = line.split(",") patient_ID = int(row[0]) patient_diagnoses[patient_ID] = -1 if row[1] == 'B' else +1 feature_vectors[patient_ID] = Vec( D, {f: float(row[feature_map[f] + 2]) for f in D}) return rowdict2mat(feature_vectors), Vec(set(patient_diagnoses.keys()), patient_diagnoses)
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] import matutil from matutil import mat2rowdict from matutil import rowdict2mat mat2row_A = mat2rowdict(A) return rowdict2mat({key: v * B for (key, v) in zip(mat2row_A.keys(), mat2row_A.values())})
def make_matrix(feature_vectors, diagnoses, features): ids = feature_vectors.keys() # main constraints rows = {i: main_constraint(i, feature_vectors[i], diagnoses[i], features) for i in ids} # nonnegativity constraints rows.update({-i: Vec(A_COLS, {i: 1}) for i in ids}) return rowdict2mat(rows)
def grayscale(): ''' Input: None Output: 3x3 greyscale matrix. ''' labels = {'r', 'g', 'b'} row = Vec(labels, {'r': 77 / 256, 'g': 151 / 256, 'b': 28 / 256}) return (rowdict2mat({'r': row, 'g': row, 'b': row}))
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] from matutil import mat2rowdict from matutil import rowdict2mat rows = mat2rowdict(A) for row,rowVec in rows.items(): rows[row] = rowVec*B return rowdict2mat(rows)
def grayscale(): ''' Input: None Output: 3x3 greyscale matrix. ''' labels = {'r','g','b'} row = Vec(labels,{'r':77/256, 'g':151/256, 'b':28/256}) return(rowdict2mat({'r':row,'g':row,'b':row}))
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] output = {} from matutil import mat2rowdict from matutil import rowdict2mat A_row = mat2rowdict(A) for i in A.D[0]: output[i] = A_row[i] * B return rowdict2mat(output)
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] row_dict = matutil.mat2rowdict(A) foo_dict = {} for key, val in row_dict.items(): foo_dict[key] = val * B return matutil.rowdict2mat(foo_dict)
def getL(): xarray = [(358, 36), (329, 597), (592, 157), (580, 483)] warray = [(0,0), (0,1), (1,0), (1,1)] pairlist = [make_equations(x1,x2,w1,w2) for (x1,x2),(w1,w2) in zip(xarray, warray)] mymap = {} for i in range(len(pairlist)): mymap[2*i]=pairlist[i][0] mymap[2*i+1]=pairlist[i][1] mymap[8]=w return rowdict2mat(mymap)
def grayscale(): ''' Input: None Output: 3x3 greyscale matrix. ''' return rowdict2mat( Vec({'r', 'g', 'b'}, { 'r': 77 / 256, 'g': 151 / 256, 'b': 28 / 256 }))
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] v = mat2rowdict(A) l=[] d={} for (i,j) in v.items(): s = j*B l.append(s) for e in l: d[i]=e return rowdict2mat(d)
def find_a_b(fname): data = read_vectors(fname) a_row_d = set(range(2)) a_rows, b = [], Vec(set(range(len(data))), {}) for i, v in enumerate(data): a_rows.append(Vec(a_row_d, {0: v['age'], 1:1})) b[i] = v['height'] coeffs = QR_solve(rowdict2mat(a_rows), b) return (coeffs[0], coeffs[1])
def direct_sum_decompose(U_basis, V_basis, w): ''' input: A list of Vecs, U_basis, containing a basis for a vector space, U. A list of Vecs, V_basis, containing a basis for a vector space, V. A Vec, w, that belongs to the direct sum of these spaces. output: A pair, (u, v), such that u+v=w and u is an element of U and v is an element of V. >>> U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> direct_sum_decompose(U_basis, V_basis, w) == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})) True ''' M = rowdict2mat(U_basis + V_basis) solution = solve(M.transpose(), w) zero = [zero_vec(U_basis[0].D)] uvec = solution * rowdict2mat(U_basis + zero * len(V_basis)) vvec = solution * rowdict2mat(zero * len(U_basis) + V_basis) return (uvec, vvec)
def grayscale(): ''' Input: None Output: 3x3 greyscale matrix. >>> grayscale() * Vec({'r','g','b'},{'r':1}) == Vec({'r','g','b'},{'r':77/256,'g':77/256,'b':77/256}) True >>> grayscale() * Vec({'r','g','b'},{'b':2}) == Vec({'r','g','b'},{'r':56/256,'g':56/256,'b':56/256}) True ''' return rowdict2mat({colour:Vec({'r','g','b'},{ 'r': 77/256, 'g': 151/256, 'b': 28/256}) for colour in {'r','g','b'}})
def direct_sum_decompose(U_basis, V_basis, w): ''' input: A list of Vecs, U_basis, containing a basis for a vector space, U. A list of Vecs, V_basis, containing a basis for a vector space, V. A Vec, w, that belongs to the direct sum of these spaces. output: A pair, (u, v), such that u+v=w and u is an element of U and v is an element of V. >>> U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> direct_sum_decompose(U_basis, V_basis, w) == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})) True ''' M = rowdict2mat(U_basis+V_basis) solution=solve(M.transpose(),w) zero=[zero_vec(U_basis[0].D)] uvec=solution*rowdict2mat(U_basis+zero*len(V_basis)) vvec=solution*rowdict2mat(zero*len(U_basis)+V_basis) return (uvec,vvec)
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] v = mat2rowdict(A) l = [] d = {} for (i, j) in v.items(): s = j * B l.append(s) for e in l: d[i] = e return rowdict2mat(d)
def matrix_matrix_mul(A, B): "Returns the product of A and B" assert A.D[1] == B.D[0] from matutil import mat2rowdict, rowdict2mat row_vecs = mat2rowdict(A) row_dict = {} for row in A.D[0]: row_dict[row] = (vector_matrix_mul(row_vecs[row], B)) matrix_product = rowdict2mat(row_dict) return matrix_product
def getL(): xarray = [(358, 36), (329, 597), (592, 157), (580, 483)] warray = [(0, 0), (0, 1), (1, 0), (1, 1)] pairlist = [ make_equations(x1, x2, w1, w2) for (x1, x2), (w1, w2) in zip(xarray, warray) ] mymap = {} for i in range(len(pairlist)): mymap[2 * i] = pairlist[i][0] mymap[2 * i + 1] = pairlist[i][1] mymap[8] = w return rowdict2mat(mymap)
def vM_mat_mat_mult(A, B): """ >>> A = Mat(({0,1,2}, {0,1,2}), {(1,1):4, (0,0):0, (1,2):1, (1,0):5, (0,1):3, (0,2):2}) >>> B = Mat(({0,1,2}, {0,1,2}), {(1,0):5, (2,1):3, (1,1):2, (2,0):0, (0,0):1, (0,1):4}) >>> vM_mat_mat_mult(A,B) == Mat(({0,1,2}, {0,1,2}), {(0,0):15, (0,1):12, (1,0):25, (1,1):31}) True >>> C = Mat(({0,1,2}, {'a','b'}), {(0,'a'):4, (0,'b'):-3, (1,'a'):1, (2,'a'):1, (2,'b'):-2}) >>> D = Mat(({'a','b'}, {'x','y'}), {('a','x'):3, ('a','y'):-2, ('b','x'):4, ('b','y'):-1}) >>> vM_mat_mat_mult(C,D) == Mat(({0,1,2}, {'x','y'}), {(0,'y'):-5, (1,'x'):3, (1,'y'):-2, (2,'x'):-5}) True """ assert A.D[1] == B.D[0] return rowdict2mat({x: row * B for x, row in mat2rowdict(A).items()})
def make_matrix(feature_vectors, diagnoises, features): main = dict( (k, main_constraint(k, feature_vectors[k], diagnoises[k], features)) for k in feature_vectors.keys()) minor = dict((-k, Vec(features | {k, 'gamma'}, {k: 1})) for k in feature_vectors.keys()) main.update(minor) for v in main.values(): v.D = features | set(feature_vectors.keys()) | {'gamma'} #print(main[919555]) #print(next(iter(main.values()))) # print(main[-919555]) return matutil.rowdict2mat(main)
def linear_regression(data): datalist = read_vectors(data) x = list(datalist[0].D)[1] y = list(datalist[0].D)[0] x_domain = {1, x} x_rowlist = [] y_list = [] for v in datalist: x_rowlist.append(Vec(x_domain, {1: 1, x: v[x]})) y_list.append(v[y]) y_vec = list2vec(y_list) minimize = QR_solve(rowdict2mat(x_rowlist), y_vec) return minimize[1], minimize[x] # return b,a
def find_non_trivial_div(N): pr_lst = primes(10000) roots, rowlist = find_candidates(N, pr_lst) M = echelon.transformation_rows(rowlist, sorted(pr_lst, reverse=True)) A = rowdict2mat(rowlist) for v in reversed(M): if not is_zero_vec(v*A): raise Exception("Unable to find non-trivial div") a, b = find_a_and_b(v, roots, N) div_candidate = gcd(a - b, N) if div_candidate != 1: return div_candidate raise Exception("Unable to find non-trivial div")
def lin_comb_mat_vec_mult(M, v): assert(M.D[1] == v.D) temp = {} from matutil import mat2coldict from matutil import rowdict2mat M_col = mat2coldict(M) for i in M.D[1]: temp[i] = v[i]*M_col[i] temp = rowdict2mat(temp) output = Vec(temp.D[1],{}) for i in output.D: for j in temp.D[0]: output[i] = output[i] + temp[j,i] return output
def lin_comb_vec_mat_mult(v, M): assert(v.D == M.D[0]) temp = {} from matutil import mat2rowdict from matutil import rowdict2mat M_row = mat2rowdict(M) for i in M.D[0]: temp[i] = v[i]*M_row[i] temp = rowdict2mat(temp) output = Vec(temp.D[1],{}) for i in output.D: for j in temp.D[0]: output[i] = output[i] + temp[j,i] return output
def rep2vec(u, veclist): ''' Input: - u: a vector as an instance of your Vec class with domain set(range(len(veclist))) - veclist: a list of n vectors (as Vec instances) Output: vector v (as Vec instance) whose coordinate representation is u Example: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> rep2vec(Vec({0,1,2}, {0:2, 1:4, 2:6}), [a0,a1,a2]) == Vec({'a', 'c', 'b', 'd'},{'a': 2, 'c': 6, 'b': 4, 'd': 0}) True ''' return u * rowdict2mat(veclist)
def remove_irr_col(rowlist): entry_col = [] label_row = range(len(rowlist)) label_col = list(rowlist[0].D) assert is_echelon(rowlist) for i in label_row: for j in label_col: if rowlist[i][j] is not 0: entry_col.append(j) break remove_col = [i for i in label_col if i not in entry_col] collist = mat2coldict(rowdict2mat(rowlist)) for i in remove_col: collist.pop(i) return collist
def rep2vec(u, veclist): ''' Input: - u: a vector as an instance of your Vec class with domain set(range(len(veclist))) - veclist: a list of n vectors (as Vec instances) Output: vector v (as Vec instance) whose coordinate representation is u Example: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> rep2vec(Vec({0,1,2}, {0:2, 1:4, 2:6}), [a0,a1,a2]) == Vec({'a', 'c', 'b', 'd'},{'a': 2, 'c': 6, 'b': 4, 'd': 0}) True ''' from matutil import rowdict2mat matrix = rowdict2mat(veclist) return u * matrix
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' identity_matrix = identity(A.D[0], one) dict1_rowmatrix = mat2rowdict(identity_matrix) newdict1 = {} for key in dict1_rowmatrix: newdict1[key] = solve(A,dict1_rowmatrix[key]) result = rowdict2mat(newdict1) final_result = transpose(result) return final_result
def grayscale(): ''' Input: None Output: 3x3 greyscale matrix. >>> grayscale() * Vec({'r','g','b'},{'r':1}) == Vec({'r','g','b'},{'r':77/256,'g':77/256,'b':77/256}) True >>> grayscale() * Vec({'r','g','b'},{'b':2}) == Vec({'r','g','b'},{'r':56/256,'g':56/256,'b':56/256}) True ''' return rowdict2mat({ colour: Vec({'r', 'g', 'b'}, { 'r': 77 / 256, 'g': 151 / 256, 'b': 28 / 256 }) for colour in {'r', 'g', 'b'} })
def mat_move2board(Y): ''' Input: - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec giving the whiteboard coordinates of a point q. Output: - a Mat each column of which is the corresponding point in the whiteboard plane (the point of intersection with the whiteboard plane of the line through the origin and q). Example: >>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}), ... {('y1',0):2, ('y2',0):4, ('y3',0):8, ... ('y1',1):10, ('y2',1):5, ('y3',1):5, ... ('y1',2):4, ('y2',2):25, ('y3',2):2, ... ('y1',3):5, ('y2',3):10, ('y3',3):4}) >>> print(Y_in) <BLANKLINE> 0 1 2 3 ------------ y1 | 2 10 4 5 y2 | 4 5 25 10 y3 | 8 5 2 4 <BLANKLINE> >>> print(mat_move2board(Y_in)) <BLANKLINE> 0 1 2 3 ------------------ y1 | 0.25 2 2 1.25 y2 | 0.5 1 12.5 2.5 y3 | 1 1 1 1 <BLANKLINE> ''' rows = mat2rowdict(Y) y1 = rows['y1'] y2 = rows['y2'] y3 = rows['y3'] return rowdict2mat({ 'y1': Vec(y1.D, dict((k, y1[k] / y3[k]) for k in y1.D)), 'y2': Vec(y2.D, dict((k, y2[k] / y3[k]) for k in y2.D)), 'y3': Vec(y3.D, dict((k, y3[k] / y3[k]) for k in y3.D)) })
def rep2vec(u, veclist): """ Input: - u: a Vec whose domain is set(range(len(veclist))) - veclist: a list of Vecs Output: the Vec whose coordinate representation is u (i.e u[0] is the coefficient of veclist[0], u[1] is the coefficient of veclist[1], etc.) Example: >>> v0 = Vec({'a','b','c','d'}, {'a':1}) >>> v1 = Vec({'a','b','c','d'}, {'a':1, 'b':2}) >>> v2 = Vec({'a','b','c','d'}, {'c':4, 'd':8}) >>> rep2vec(Vec({0,1,2}, {0:2, 1:4, 2:6}), [v0,v1,v2]) == Vec({'d', 'a', 'c', 'b'},{'a': 6, 'c': 24, 'b': 8, 'd': 48}) True >>> rep2vec(Vec({0,1,2}, {0:2, 1:4}), [v0, v1, v2]) == Vec({'d', 'a', 'c', 'b'},{'a': 6, 'c': 0, 'b': 8, 'd': 0}) True """ return u * rowdict2mat(veclist)
def rep2vec(u, veclist): ''' Input: - u: a Vec whose domain is set(range(len(veclist))) - veclist: a list of Vecs Output: the Vec whose coordinate representation is u (i.e u[0] is the coefficient of veclist[0], u[1] is the coefficient of veclist[1], etc.) Example: >>> v0 = Vec({'a','b','c','d'}, {'a':1}) >>> v1 = Vec({'a','b','c','d'}, {'a':1, 'b':2}) >>> v2 = Vec({'a','b','c','d'}, {'c':4, 'd':8}) >>> rep2vec(Vec({0,1,2}, {0:2, 1:4, 2:6}), [v0,v1,v2]) == Vec({'d', 'a', 'c', 'b'},{'a': 6, 'c': 24, 'b': 8, 'd': 48}) True >>> rep2vec(Vec({0,1,2}, {0:2, 1:4}), [v0, v1, v2]) == Vec({'d', 'a', 'c', 'b'},{'a': 6, 'c': 0, 'b': 8, 'd': 0}) True ''' return u * rowdict2mat(veclist)
def get_H(): domain = {(a, b) for a in {'y1', 'y2', 'y3'} for b in {'x1', 'x2', 'x3'}} w = Vec(domain, {('y1','x1'):1}) l1 = make_equations(358, 36, 0, 0) l2 = make_equations(329, 597,0, 1) l3 = make_equations(592, 157,1, 0) l4 = make_equations(580, 483,1, 1) rowdict = { 0:l1[0], 1:l1[1], 2:l2[0], 3:l2[1], 4:l3[0], 5:l3[1], 6:l4[0], 7:l4[1], 8:w } L = rowdict2mat(rowdict) b = Vec(set(range(9)), {8:1}) h = solve(L, b) return Mat(({a for a in {'y1','y2','y3'}}, {b for b in {'x1','x2','x3'}}), {(d[0],d[1]):h[d] for d in h.D})
def read_training_data(fname, features=None): """Given a file in appropriate format, returns the triple (feature_vectors, patient_diagnoses, D) feature_vectors is a dictionary that maps integer patient identification numbers to D-vectors where D is the set of feature labels, and patient_diagnoses is a dictionary mapping patient identification numbers to {+1, -1}, where +1 indicates malignant and -1 indicates benign. """ file = open(fname) params = ["radius", "texture", "perimeter","area","smoothness","compactness","concavity","concave points","symmetry","fractal dimension"]; stats = ["(mean)", "(stderr)", "(worst)"] feature_labels = set([y+x for x in stats for y in params]) feature_map = {params[i]+stats[j]:j*len(params)+i for i in range(len(params)) for j in range(len(stats))} if features is None: features = feature_labels feature_vectors = {} patient_diagnoses = {} for line in file: row = line.split(",") patient_ID = int(row[0]) patient_diagnoses[patient_ID] = -1 if row[1]=='B' else +1 feature_vectors[patient_ID] = Vec(features, {f:float(row[feature_map[f]+2]) for f in features}) return rowdict2mat(feature_vectors), Vec(set(patient_diagnoses.keys()), patient_diagnoses)
def morph(S, B): ''' Input: - S: a list of distinct Vec instances - B: a list of linearly independent Vec instances - Span S == Span B Output: a list of pairs of vectors to inject and eject Example: >>> #This is how our morph works. Yours may yield different results. >>> S = [list2vec(v) for v in [[1,0,0],[0,1,0],[0,0,1]]] >>> B = [list2vec(v) for v in [[1,1,0],[0,1,1],[1,0,1]]] >>> morph(S, B) [(Vec({0, 1, 2},{0: 1, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 1, 1: 0, 2: 0})), (Vec({0, 1, 2},{0: 0, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 0})), (Vec({0, 1, 2},{0: 1, 1: 0, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 0, 2: 1}))] ''' L=[] mat = rowdict2mat(S) for i,e in enumerate(B): L.append((e,S[i])) return L
def morph(S, B): ''' Input: - S: a list of distinct Vec instances - B: a list of linearly independent Vec instances - Span S == Span B Output: a list of pairs of vectors to inject and eject Example: >>> #This is how our morph works. Yours may yield different results. >>> S = [list2vec(v) for v in [[1,0,0],[0,1,0],[0,0,1]]] >>> B = [list2vec(v) for v in [[1,1,0],[0,1,1],[1,0,1]]] >>> morph(S, B) [(Vec({0, 1, 2},{0: 1, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 1, 1: 0, 2: 0})), (Vec({0, 1, 2},{0: 0, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 0})), (Vec({0, 1, 2},{0: 1, 1: 0, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 0, 2: 1}))] ''' L = [] mat = rowdict2mat(S) for i, e in enumerate(B): L.append((e, S[i])) return L
from eigenfaces import load_images ## Task 1 D = {(x,y) for x in range(166) for y in range(189)} # see documentation of eigenfaces.load_images face_images = {i: Vec(D, {(x,y): face[y][x] for x in range(166) for y in range(189)}) for i,face in load_images('faces/').items()} # dict of Vecs ## Task 2 centroid = sum(face_images.values()) / len(face_images) centered_face_images = {i: face - centroid for i,face in face_images.items()} ## Task 3 A = matutil.rowdict2mat(centered_face_images) # centered image vectors U, S, V = svd.factor(A) orthonormal_basis = matutil.rowdict2mat({k:v for k,v in matutil.mat2coldict(V).items() if k <10}) # 10 rows ## Task 4 #This is the "transpose" of what was specified in the text. #Follow the spec given here. def projected_representation(M, x): ''' Input: - M: a matrix with orthonormal rows with M.D[1] == x.D - x: a vector Output: - the projection of x onto the row-space of M
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] a_rows = mat2rowdict(A) d = {k:vec*B for k,vec in a_rows.items() } return rowdict2mat(d)
''' vecs = [ make_equations(x1, x2, w1, w2) for (x1, x2), (w1, w2) in zip(corners, [(0, 0), (0, 1), (1, 0), (1, 1)]) ] return [v for vec in vecs for v in vec] + [w] ## 7: (Task 5.12.4) Build linear system # Apply make_nine_equations to the list of tuples specifying the pixel coordinates of the # whiteboard corners in the image. Assign the resulting list of nine vectors to veclist: veclist = make_nine_equations([(358, 36), (329, 597), (592, 157), (580, 483)]) # Build a Mat whose rows are the Vecs in veclist L = rowdict2mat(veclist) ## 8: () Solve linear system # Now solve the matrix-vector equation to get a Vec hvec, and turn it into a matrix H. hvec = solve(L, b) H = Mat(({'y1', 'y2', 'y3'}, {'x1', 'x2', 'x3'}), hvec.f) ## 9: (Task 5.12.7) Y Board Comprehension def mat_move2board(Y): ''' Input: - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec giving the whiteboard coordinates of a point q. Output:
def project(M, x): coordinates = projected_representation(M, x) return coordinates * M raw_images = load_images('./faces') rowsCnt = len(raw_images[0]) colsCnt = len(raw_images[0][0]) images = { key: list2vec([el for row in image for el in row]) for key, image in raw_images.items() } centroid = find_centroid(images) centered_images = center_images(images, centroid) M = rowdict2mat(centered_images) print('Factoring...') U, E, V = factor(M) print('Done') print(len(U.D[0]), len(U.D[1])) print(len(E.D[0]), len(E.D[1])) print(len(V.D[0]), len(V.D[1])) print(E[0, 0], E[1, 1], E[2, 2], E[3, 3]) orth_basis = rowdict2mat( {key: vec for key, vec in mat2coldict(V).items() if key < 10}) print(len(orth_basis.D[0]), len(orth_basis.D[1])) print(len(centered_images[0].D))
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] row_dict= matutil.mat2rowdict(A); return matutil.rowdict2mat({r:row_dict[r]*B for r in A.D[0]})
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] return rowdict2mat({x: mat2rowdict(A)[x] * B for x in A.D[0]})
def dot_prod_mat_mat_mult(A, B): assert A.D[1] == B.D[0] Adict = mat2rowdict(A) Bdict = mat2coldict(B) #return rowdict2mat({x: Vec(Bdict.keys(),{y:row*col for y,col in Bdict.items()}) for x,row in Adict.items()}) return rowdict2mat({x: mat2rowdict(A)[x] * B for x in A.D[0]})
def loss(A, b, w): loss_temp_vec = A * w - b loss_temp_mat = rowdict2mat({0: loss_temp_vec}) loss_val = loss_temp_mat * loss_temp_mat.transpose() return loss_val.f.get((0, 0))
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] rowdict = {row:value*B for row,value in mat2rowdict(A).items()} return rowdict2mat(rowdict)
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] from matutil import mat2rowdict, rowdict2mat rowsA = mat2rowdict(A) return rowdict2mat({i:rowsA[i]*B for i in A.D[0]})
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] return rowdict2mat({row: (mat2rowdict(A)[row])*B for row in A.D[0]})
def vM_mat_mat_mult(A, B): assert A.D[1] == B.D[0] abRows = {k: v * B for k, v in matutil.mat2rowdict(A).items()} return matutil.rowdict2mat(abRows)
('y3', 'x2'): w2 * x2, ('y3', 'x3'): w2, ('y2', 'x1'): -x1, ('y2', 'x2'): -x2, ('y2', 'x3'): -1 }) return [u, v] def scale_vector(): return Vec(funny_domain(), {('y1', 'x1'): 1}) ## Task 3 L = rowdict2mat( make_equations(358, 36, 0, 0) + make_equations(329, 597, 0, 1) + make_equations(592, 157, 1, 0) + make_equations(580, 483, 1, 1) + [scale_vector()]) b = Vec(set(range(9)), {8: 1}) h_vec = solve(L, b) ##print(h_vec.f) H = Mat(({'y1', 'y2', 'y3'}, {'x1', 'x2', 'x3'}), h_vec.f.copy()) ##print(H) (X_pts, colors) = file2mat('board.png', ('x1', 'x2', 'x3')) Y_pts = H * X_pts ## Task 4 def mat_move2board(Y): ''' Input:
def transpose(M): "Returns the transpose of M" from matutil import mat2coldict from matutil import rowdict2mat cols = mat2coldict(M) return rowdict2mat(cols)