def manual_nodev(parcel_rejections, parcels): df1 = parcels.to_frame(['x', 'y']).dropna(subset=['x', 'y']) df2 = parcel_rejections.to_frame(['lng', 'lat']) df2 = df2[parcel_rejections.state == "denied"] df2 = df2[["lng", "lat"]] # need to change the order ind = nearest_neighbor(df1, df2) s = pd.Series(False, parcels.index) s.loc[ind.flatten()] = True return s.astype('int')
def manual_nodev(parcel_rejections, parcels): df1 = parcels.to_frame(['x', 'y']).dropna(subset=['x', 'y']) df2 = parcel_rejections.to_frame(['lng', 'lat']) df2 = df2[parcel_rejections.state == "denied"] df2 = df2[["lng", "lat"]] # need to change the order ind = nearest_neighbor(df1, df2) col = pd.Series(False, parcels.index) col.loc[ind.flatten()] = True return col.astype('int')
def costar(store, parcels): df = pd.read_csv(os.path.join(misc.data_dir(), '2015_08_29_costar.csv')) df["PropertyType"] = df.PropertyType.replace("General Retail", "Retail") df = df[df.PropertyType.isin(["Office", "Retail", "Industrial"])] df["costar_rent"] = df["Average Weighted Rent"].astype('float') df["year_built"] = df["Year Built"].fillna(1980) df = df.dropna(subset=["costar_rent", "Latitude", "Longitude"]) # now assign parcel id df["parcel_id"] = nearest_neighbor( parcels.to_frame(['x', 'y']).dropna(subset=['x', 'y']), df[['Longitude', 'Latitude']]) return df
def costar(store, parcels): df = pd.read_csv(os.path.join(misc.data_dir(), '2015_08_29_costar.csv')) df["PropertyType"] = df.PropertyType.replace("General Retail", "Retail") df = df[df.PropertyType.isin(["Office", "Retail", "Industrial"])] df["costar_rent"] = df["Average Weighted Rent"].astype('float') df["year_built"] = df["Year Built"].fillna(1980) df = df.dropna(subset=["costar_rent", "Latitude", "Longitude"]) # now assign parcel id df["parcel_id"] = nearest_neighbor( parcels.to_frame(['x', 'y']).dropna(subset=['x', 'y']), df[['Longitude', 'Latitude']] ) return df
def icp(A, B, max_iterations=10000, tolerance=0.001): assert A.shape == B.shape n = A.shape[1] # 构造齐次坐标 src = np.ones((n+1,A.shape[0])) dst = np.ones((n+1,B.shape[0])) src[:n,:] = np.copy(A.T) dst[:n,:] = np.copy(B.T) error = 0 for i in range(max_iterations): distances, indices = nearest_neighbor(src[:n,:].T, dst[:n,:].T) # print('icp distances', distances.shape[0]) T,_,_ = register_by_SVD(src[:n,:].T, dst[:n,indices].T) # dst = dst[:,indices] # print('icp distances', distances.shape[0]) # T,_,_ = register_by_SVD(src[:n,:].T, dst[:n,:].T) # 变换矩阵作用于齐次坐标 src = np.dot(T, src) # check error new_error = np.mean(distances) if np.abs(error - new_error) < tolerance: break error = new_error # calculate final transformation T,_,_ = register_by_SVD(A, src[:n,:].T) return T, distances, i
def test_rough_fit(self): total_time = 0 for i in range(TESTS_NUM): # ax2=fig.add_subplot(111,projection='3d') # ax2.scatter(z2,x2,y2,c='b',marker='.',s=2,linewidth=0,alpha=1,cmap='spectral') # # #ax.set_facecolor((0,0,0)) # ax2.axis('scaled') # # ax.xaxis.set_visible(False) # # ax.yaxis.set_visible(False) # ax2.set_xlabel('X Label') # ax2.set_ylabel('Y Label') # ax2.set_zlabel('Z Label') start = time.time() T, R1, t1 = register_by_SVD(self.B, self.A) total_time += time.time() - start # Make C a homogeneous representation of B C = np.ones((self.A.shape[0], 4)) C[:, 0:3] = self.B # Transform C C = np.dot(T, C.T).T # view_numpy_data(self.A, C) print(np.mean(nearest_neighbor(self.A.T, C.T))[0]) assert np.allclose(C[:, 0:3], self.A, atol=6 * NOISE_SIGMA) # T should transform B (or C) to A assert np.allclose(-t1, self.t, atol=6 * NOISE_SIGMA) # t and t1 should be inverses assert np.allclose(R1.T, self.R, atol=6 * NOISE_SIGMA) # R and R1 should be inverses print('rough fit time: {:.3}'.format(total_time / TESTS_NUM)) return
def dis_icp(A, B, max_iterations=10000, tolerance=0.001): assert A.shape == B.shape n = A.shape[1] # print(A) # print(n) # 构造齐次坐标 src = np.ones((n + 1, A.shape[0])) dst = np.ones((n + 1, B.shape[0])) src[:n, :] = np.copy(A.T) dst[:n, :] = np.copy(B.T) s = src[:n, :] d = dst[:n, :] error = 0 match_l = [] for i in range(max_iterations): # distances, indices = nearest_neighbor(src[:n,:].T, dst[:n,:].T) distances, indices = nearest_neighbor(s.T, d.T) print('indices', indices) c = Counter(indices) # print(c) # print('d', distances.shape[0]) # d = d[:, indices] max_distance = np.max(distances) min_distance = np.min(distances) diff = max_distance - min_distance threshold = min_distance + diff / 2 # match_l = [] # 剔除过大值 while True: # print(distances, threshold) nomatch_indices = np.where(distances > threshold)[0] print('nomatch_indices', nomatch_indices) # break # available_indices = [] # for m in match_indices: # print(m) # if Counter(m) == 1: # available_indices.append(m) # print('available_indices', available_indices) # available_indices = np.array(available_indices) # match_indices = np.array([i[0] for i in np.argwhere(distances<threshold)]) # print('match_indices', match_indices) # match_l.append(match_indices) # break # print(match_indices) # match_distances = np.minimum(distances, threshold) if distances.shape[0] * 0.8 < nomatch_indices.shape[0]: # print(1) break else: threshold += (max_distance - threshold) / 2 # distances = distances[match_indices] # else: # threshold = (max_distance+threshold)/2 # match_l.append(match_indices) # for d in match_distances: # np.where() # print('min_distance', min_distance) # print('max_distance', max_distance) # print('match_indices', match_indices) # print('src[:n,:]', src[:n,:].shape[1], src[:n,:]) # print('threshold', threshold) # s = src[:n, :] # d = dst[:n, indices] # print('match_l', len(match_l)) # for m in match_l: # print(m.shape, m) # distances = distances[m] # for m in match_l: # print(s.shape, m.shape, ) # # s = s[:, m] # d = d[:, m] # s = s[:, match_indices] # d = d[:, match_indices] # distances = distances[match_indices] # print(s.shape[1]) # print('src[:n,:][match_indices]', s.shape[1], s) # print('dst[:n,indices]', dst[:n,indices]) # print('distances', distances) T, _, _ = register_by_SVD(s[:, match_indices].T, d[:, indices][:, match_indices].T) # T,_,_ = register_by_SVD(src[:n,:].T, dst[:n,indices].T) # print('T', T) # 变换矩阵作用于齐次坐标 src = np.dot(T, src) # print('before', distances) print('distances', distances.shape[0]) # print('after', distances) # check error new_error = np.mean(distances) if np.abs(error - new_error) < tolerance: break error = new_error # calculate final transformation T, _, _ = register_by_SVD(A, src[:n, :].T) return T, distances, i