def de_mean(A): """ returns result of subtracting from every value of A the mean value of its column """ num_rows, num_cols = Ch4.shape(A) col_means, _ = scale(A) return Ch4.make_matrix(num_rows, num_cols, lambda i, j: A[i][j] - col_means[j])
def de_mean(A): """ returns result of subtracting from every value of A the mean value of its column """ num_rows, num_cols = Ch4.shape(A) col_means, _ = scale(A) return Ch4.make_matrix(num_rows,num_cols, lambda i,j: A[i][j]-col_means[j])
def new_point_distance(labeled_point): """ helper function to calculate distance between a (point, label) tuple and new_point """ # get the location from the labeled_point point = labeled_point[0] # return distance return Ch4.distance(point, new_point)
def directional_variance_i(r_i, w): """" computes for row r_i the projection of r_i onto w """ # we square this because we later sum to get a resultant vector return Ch4.dot_product(r_i, direction(w))**2
def direction(w): """ returns a normalized vector in direction w """ mag = Ch4.magnitude(w) return [w_i / float(mag) for w_i in w]
def remove_projection_from_vector(v, w): """ projects v onto w and then subtracts the projection from v """ return Ch4.vector_subtract(v, project(v, w))
def project(v, w): """ projects vector v onto w """ projection_length = Ch4.dot_product(v, w) return Ch4.scalar_multiply(projection_length, w)
# Remove the mean of the data # ############################### def de_mean(A): """ returns result of subtracting from every value of A the mean value of its column """ num_rows, num_cols = Ch4.shape(A) col_means, _ = scale(A) return Ch4.make_matrix(num_rows, num_cols, lambda i, j: A[i][j] - col_means[j]) de_meaned_data = de_mean(data) de_xs = Ch4.get_col(de_meaned_data, 0) de_ys = Ch4.get_col(de_meaned_data, 1) # Compute Directional Variance Gradient # ######################################### # given a directional vector 'd' (magnitude=1) each row in 'r' data extends # in 'd' direction by dot(r,d). Lets first define a directional vector def direction(w): """ returns a normalized vector in direction w """ mag = Ch4.magnitude(w) return [w_i / float(mag) for w_i in w] def directional_variance_i(r_i, w):
def directional_variance_gradient_i(r_i, w): """ computes the gradient of the directioanl variance for row_i in data """ return [2 * Ch4.dot_product(r_i, direction(w)) * r_ij for r_ij in r_i]
def direction(w): """ returns a normalized vector in direction w """ mag = Ch4.magnitude(w) return [w_i/float(mag) for w_i in w]
def remove_projection_from_vector(v,w): """ projects v onto w and then subtracts the projection from v """ return Ch4.vector_subtract(v, project(v,w))
def project(v,w): """ projects vector v onto w """ projection_length = Ch4.dot_product(v,w) return Ch4.scalar_multiply(projection_length,w)
# make a matrix of the data data = [list(tup) for tup in zip(xs,ys)] # Remove the mean of the data # ############################### def de_mean(A): """ returns result of subtracting from every value of A the mean value of its column """ num_rows, num_cols = Ch4.shape(A) col_means, _ = scale(A) return Ch4.make_matrix(num_rows,num_cols, lambda i,j: A[i][j]-col_means[j]) de_meaned_data = de_mean(data) de_xs = Ch4.get_col(de_meaned_data,0) de_ys = Ch4.get_col(de_meaned_data,1) # Compute Directional Variance Gradient # ######################################### # given a directional vector 'd' (magnitude=1) each row in 'r' data extends # in 'd' direction by dot(r,d). Lets first define a directional vector def direction(w): """ returns a normalized vector in direction w """ mag = Ch4.magnitude(w) return [w_i/float(mag) for w_i in w] def directional_variance_i(r_i, w): """" computes for row r_i the projection of r_i onto w """ # we square this because we later sum to get a resultant vector
def directional_variance_gradient(data, w): """ computes the variance gradient for all rows in data """ return Ch4.vector_sum( [directional_variance_gradient_i(r_i, w) for r_i in data])
def directional_variance_gradient(data, w): """ computes the variance gradient for all rows in data """ return Ch4.vector_sum([directional_variance_gradient_i(r_i, w) for r_i in data])