ユーザーが設定した条件(例えば、300, 25, 0.7)に近い条件をとってくるような関数を、マハラノビス距離で定義したいです。
下記は、それをユークリッド距離で定義したものになります。
python
1def take_nearest_points(df, target_vector, nearest_points =70): 2 """ 3 df : Pandas Dataframe where there are 3 columns (MW, Amb.temp, FD) 4 target_vector : List containing 3 values (MW, Amb.temp, FD) 5 nearest_points: Number meaning how many closest nearest points taken 6 7 """ 8 9 # Preliquistes 10 array_X_inputs = df.as_matrix() 11 list_euclidean_distance = [] 12 13 # Calculate Euclidean distances 14 list_euclidean_distance = [distance.euclidean(target_vector, each_input) for each_input in array_X_inputs] 15 16 # Concat the list to the original dframe 17 df["euclidean"] = list_euclidean_distance 18 df_with_euclidean_sort = df.sort_values(["euclidean"], ascending=True) 19 20 # Extract the top n points 21 df_top_n_points = df_with_euclidean_sort.head(nearest_points) 22 23 return df_top_n_points
ご教授のほど、よろしくお願いします!
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。