从多个均值向量中找到欧氏距离

时间:2019-03-13 18:13:46

标签: python pandas euclidean-distance

这就是我想要做的-我能够执行步骤1到4。在步骤5以后需要帮助

基本上,对于每个数据点,我想根据列y找到所有均值向量的欧几里得距离

  1. 获取数据
  2. 分隔非数字列
  3. 通过y列查找均值向量
  4. 保存方式
  5. 根据y值减去每一行的平均向量
  6. 在每列中加平方
  7. 添加所有列
  8. 联接回到数值数据集,然后联接非数值列
import pandas as pd

data = [['Alex',10,5,0],['Bob',12,4,1],['Clarke',13,6,0],['brke',15,1,0]]
df = pd.DataFrame(data,columns=['Name','Age','weight','class'],dtype=float)
print (df)
df_numeric=df.select_dtypes(include='number')#, exclude=None)[source]
df_non_numeric=df.select_dtypes(exclude='number')
means=df_numeric.groupby('class').mean()

对于means的每一行,从df_numeric的每一行中减去该行。然后对输出中每一列的平方,然后为每一行添加所有列。然后将这些数据重新连接到df_numericdf_non_numeric

-------------- update1

添加了如下代码。我的问题已更改,更新的问题在最后。

def calculate_distance(row):
    return (np.sum(np.square(row-means.head(1)),1))

def calculate_distance2(row):
    return (np.sum(np.square(row-means.tail(1)),1))


df_numeric2=df_numeric.drop("class",1)
#np.sum(np.square(df_numeric2.head(1)-means.head(1)),1)
df_numeric2['distance0']= df_numeric.apply(calculate_distance, axis=1)
df_numeric2['distance1']= df_numeric.apply(calculate_distance2, axis=1)

print(df_numeric2)

final = pd.concat([df_non_numeric, df_numeric2], axis=1)
final["class"]=df["class"]

谁能确认这是获得结果的正确方法?我主要关注最后两个声明。倒数第二条语句会正确连接吗?最终声明会分配原始的class吗?我想确认python不会以随机顺序进行concat和class分配,并且python会保持行出现的顺序

final = pd.concat([df_non_numeric, df_numeric2], axis=1)
final["class"]=df["class"]

2 个答案:

答案 0 :(得分:2)

我想这就是你想要的

import pandas as pd
import numpy as np
data = [['Alex',10,5,0],['Bob',12,4,1],['Clarke',13,6,0],['brke',15,1,0]]
df = pd.DataFrame(data,columns=['Name','Age','weight','class'],dtype=float) 
print (df)
df_numeric=df.select_dtypes(include='number')#, exclude=None)[source]
# Make df_non_numeric a copy and not a view
df_non_numeric=df.select_dtypes(exclude='number').copy()

# Subtract mean (calculated using the transform function which preserves the 
# number of rows) for each class  to create distance to mean
df_dist_to_mean =  df_numeric[['Age', 'weight']] - df_numeric[['Age', 'weight', 'class']].groupby('class').transform('mean')
# Finally calculate the euclidean distance (hypotenuse)
df_non_numeric['euc_dist'] = np.hypot(df_dist_to_mean['Age'], df_dist_to_mean['weight'])
df_non_numeric['class'] = df_numeric['class']
# If you want a separate dataframe named 'final' with the end result
df_final = df_non_numeric.copy()
print(df_final)

也许可以写得更密集一些,但是这样一来,您会发现发生了什么。

答案 1 :(得分:1)

我确定有更好的方法可以做到这一点,但是我根据班级进行了迭代,并遵循了确切的步骤。

  1. 将“类”分配为索引。
  2. 进行旋转,以使“类”位于列中。
  3. 执行与df_numeric对应的均值的运算
  4. 平方值。
  5. 汇总行。
  6. 将数据帧重新连接在一起。

    data = [['Alex',10,5,0],['Bob',12,4,1],['Clarke',13,6,0],['brke',15,1,0]]
    df = pd.DataFrame(data,columns=['Name','Age','weight','class'],dtype=float)
    #print (df)
    
    
    df_numeric=df.select_dtypes(include='number')#, exclude=None)[source]
    df_non_numeric=df.select_dtypes(exclude='number')
    
    means=df_numeric.groupby('class').mean().T
    
    
    import numpy as np
    # Changed index 
    df_numeric.index = df_numeric['class']
    df_numeric.drop('class' , axis = 1 , inplace = True)
    
    # Rotated the Numeric data sideways so the class was in the columns
    df_numeric = df_numeric.T
    
    #Iterated through the values in means and seen which df_Numeric values matched
    store = [] # Assigned an empty array
    for j in means:
        sto = df_numeric[j]
        if type(sto) == type(pd.Series()): # If there is a single value it comes out as a pd.Series type
            sto = sto.to_frame() # Need to convert ot dataframe type
        store.append(sto-j) # append the various values to the array
    
    
    
    values = np.array(store)**2 # Squaring the values
    
    # Summing the rows
    summed = []
    for i in values:
        summed.append((i.sum(axis = 1)))
    
    
    
    df_new = pd.concat(summed , axis = 1)
    df_new.T