def fwdEngine(res_mesh, geo_factor, dc, electrodes): src1, rec1, rec2, src2 = electrodes.transpose((2, 0, 1)) # Calculate the euclidean distance between electrode pairs r1 = euclidean.dist(src1, rec1) r2 = euclidean.dist(src2, rec1) r3 = euclidean.dist(src1, rec2) r4 = euclidean.dist(src2, rec2) if res_mesh == 1: deltaV = dc * res_mesh / (2 * np.pi) * ((1 / r1 - 1 / r2) - (1 / r3 - 1 / r4)) resistance = deltaV / dc rho_a = resistance * geo_factor return (rho_a)
def fwdEngine(res_mesh,geo_factor,dc,electrodes): src1,rec1,rec2,src2 = electrodes.transpose((2,0,1)) # Calculate the euclidean distance between electrode pairs r1 = euclidean.dist(src1,rec1) r2 = euclidean.dist(src2,rec1) r3 = euclidean.dist(src1,rec2) r4 = euclidean.dist(src2,rec2) if res_mesh == 1: deltaV = dc*res_mesh/(2*np.pi)*((1/r1-1/r2)-(1/r3-1/r4)) resistance = deltaV/dc rho_a = resistance*geo_factor return (rho_a)
def new_predict(movie_id): movie_name1 = movie_dict[str(movie_id)]["title"] eucl = [] for k, v in USER_RATING.iteritems(): movie_name2 = movie_dict[k]["title"] eucl.append((euclidean.dist(user_ratings, movie_name1, movie_name2), v, k)) sorted_list = sorted(eucl) score = sorted_list[0][0] sorted_list.reverse() highest_ratio = sorted_list[0][1] movie_id2 = sorted_list[0][2] prediction = "%.1f" % round(score * highest_ratio) print sorted_list print "Best Euclidean guess for movie %s: %s is %s stars" % (movie_id, movie_name1, prediction)
def knn(movie_ratings, predict_movie, prev_rated,num_neighbors): #prev_rated is a dictionary of previously rated movies (movie_id: rating) eucl_dist = {} for k,v in prev_rated: print prev_rated eucl_dist[str(dist(movie_ratings,predict_movie,v))] = (k, v) # sort keys of eucl_dict eucl_keys = [int(x) for x in eucl_dist.keys()] sorted_keys = sorted(eucl_keys) neighbors = sorted_keys[0:num_neighbors] user_rating_total = 0 for neighbor in neighbors: user_rating_total += eucl_dist[neighbor][1] knn_est = user_rating_total/num_neighbors return knn_est
""" Given two d-dimensional vectors u and v, the ways to compute their distance are various. """ import numpy as np N = int(input('input N-dimension : ')) u = [np.random.randint(10) for x in range(N)] v = [np.random.randint(10) for x in range(N)] print('u vector :', u) print('v vector :', v) import euclidean import manhattan import hamming import cosine import chebyshev print('Euclidean distance :', euclidean.dist(u, v)) print('Manhattan distance :', manhattan.dist(u, v)) print('Hamming distance :', hamming.dist(u, v)) print('Cosine distance :', cosine.dist(u, v)) print('Chebyshev distance :', chebyshev.dist(u, v))