在熊猫数据框架中查找行之间的距离,但参考1行
在此PANDAS DATAFRAME中:
y_train feat1 feat2
0 9.596113 -7.900107
1 -1.384157 2.685313
2 -8.211954 5.214797
如何在DataFrame末尾添加“距离0级”列的“距离0”列,该框架返回每个类(即每行)与y_train = 0的距离?我想使用0类用作参考。在此dataframe中, feat 1 = x
和 feat 2 = y
我尝试过:
from sklearn.metrics import pairwise_distances
pairwise_distances(df_centroid['feat1'].values, df_centroid['feat2'].values)
但是这给了我错误,
ValueError: Expected 2D array, got 1D array instead:
任何帮助都将不胜感激!
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pairwise_distances想要第一个输入x-所有点 - 然后是y-我们要计算距离的位置。
因此,对于X,我们有:所有课程。每个功能都是其位置的一个坐标或数学术语,类是向量 f = [ f 0 , f 1 ],其中每个 f i 都是特征权重。
对于y,我们有:class 0。我们要计算到第0类的距离,对于每个X。
只需尝试一下,我们就可以看到
继续进行正确执行此操作
AHA,所以让我们 在两种情况下
这就是我使用
.loc
ie0:0
的2D切片的原因。(并且.to_numpy()等效物会自动发生,但请记住
想一想如何处理丢失的数据。)
我刚刚采用了您在问题中提到的距离指标。现在,您可以看到如何使用它,可以自由地用另一个指标替换它。以这种方式,Sklearn API是非常灵活的WRT互换算法。
pairwise_distances wants a first input X - all the points - and then Y - where we want to compute the distance to.
So for X we have: All the classes. Each feature is one coordinate of its location or in mathematical terms, the class is a vector f = [f0, f1] where each fi is a feature weight.
For Y we have: Class 0. We want to compute the distance to Class 0, for each X.
Just experimenting a bit we can see that
Aha, so let's go ahead and do this properly
We want an ndim=2 array as input to pairwise_distances in both cases,
which is the reason I use a 2D slice for
.loc
i.e0:0
.(And the .to_numpy() equivalents happen automatically, but remember
to think about how pairwise_distances would handle missing data.)
I've just taken the distance metric that you mentioned in your question. Now that you see how it can be used, you're free to replace it with another metric. The sklearn API is quite flexible w.r.t interchangeable algorithms in this way.