有人为我的matlab程序发光吗?我有来自两个传感器的数据,我正在kNN分别对它们进行分类.在这两种情况下,训练集看起来像一组总共42行的向量,如下所示:
[44 12 53 29 35 30 49;
54 36 58 30 38 24 37;..]
Run Code Online (Sandbox Code Playgroud)
然后我得到一个样本,例如[40 30 50 25 40 25 30],我想将样本分类到最近的邻居.作为接近度的标准,我使用欧几里德度量,sqrt(sum(Y 2)),其中Y是每个元素之间的差异,它给出了Sample和每个训练集类别之间的距离数组.
那么,有两个问题:
补充:到目前为止我正在使用公式:probability = distance/sum of distances但我无法绘制正确的cdf或直方图.这给了我一些分布,但我看到了一个问题,因为如果距离很大,例如700,那么最接近的类将获得最大的概率,但它是错的,因为距离太大而不能与任何课程相比.
任何帮助或评论都非常感谢.
matlab classification knn euclidean-distance probability-density
我在欧氏距离计算中非常迷失.我发现函数dist2 {SpatialTools}或rdist {fields}来执行此操作,但它们不能按预期工作.
我想一个点在carthesian系统中有两个坐标,所以[x,y].要测量2个点之间的距离(由行定义),我需要2个点的4个坐标,所以点A:[x1,y1]点B:[x2,y2]
积分协调:

A[0,1]
B[0,0]
C[1,1]
D[1,1]
Run Code Online (Sandbox Code Playgroud)
我有两个矩阵:x1(A和C在那里,由行定义)和x2(包含B和D).写在矩阵中:
library("SpatialTools")
x1<-matrix(c(0,1,1,1), nrow = 2, ncol=2, byrow=TRUE)
x2<-matrix(c(0,0,1,1), nrow = 2, ncol=2, byrow=TRUE)
Run Code Online (Sandbox Code Playgroud)
所以我得到了
> x1
[,1] [,2]
[1,] 0 1 #(as xy coordinates of A point)
[2,] 1 1 #(same for C point)
> x2
[,1] [,2]
[1,] 0 0 #(same for B point)
[2,] 1 1 #(same for D point)
Run Code Online (Sandbox Code Playgroud)
计算之间的欧氏距离
A <-> B # same as x1[1,] <-> x2[1,]
C <-> …Run Code Online (Sandbox Code Playgroud) 我试图绘制一些圆圈,我有点希望它们会与某些点交叉,唉...
library(maptools)
library(plotrix)
xy <- matrix(runif(20, min = -100, max = 100), ncol = 2)
distance <- spDistsN1(xy, xy[1, ])
plot(0,0, xlim = c(-100, 100), ylim = c(-100, 100), type = "n")
points(data.frame(xy))
points(xy[1, 1], xy[1, 2], pch = 16)
draw.circle(xy[1, 1], xy[1, 2], radius = distance)
Run Code Online (Sandbox Code Playgroud)
上面的代码执行以下操作:

sup = x(i, :);
phi(1, i) = {@(z) exp(-g * sum((z - sup(ones([size(z, 1) 1]),:)) .^ 2, 2))};
这是用于逻辑回归的径向基函数(RBF).这是公式:

我需要你的建议,我可以优化这个公式吗?因为它呼叫数百万次,而且需要很多时间......
我需要以最快的方式计算两点之间的欧氏距离.在C.
我的代码是这样的,看起来有点慢:
float distance(int py, int px, int jy, int jx){
return sqrtf((float)((px)*(px)+(py)*(py)));
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
编辑:
对不起,我不清楚.我最好指定上下文:我正在使用图像,我需要从每个像素到所有其他像素的欧几里德距离.所以我必须计算很多次.我不能使用距离的平方.我将添加更多代码以便更清楚:
for (jy=0; jy<sizeY; jy++) {
for (jx=0; jx<sizeX; jx++) {
if (jx==px && jy==py) {
;
}
else{
num+=rfun(imgI[py][px].red-imgI[jy][jx].red)/distance(py, px, jy, jx);
den+=RMAX/distance(py, px, jy, jx);
}
}
}
float distance(int py, int px, int jy, int jx){
return sqrtf((float)((px-jx)*(px-jx)+(py-jy)*(py-jy)));
}
Run Code Online (Sandbox Code Playgroud)
这就是我要做的.我必须用所有像素(px,py)来做
EDIT2:对不起,我不清楚,但我尽力保持这个问题的一般性.我正在编写一个用算法处理图像的程序.最大的问题是时间因为我必须真的非常快.现在我需要优化的是这个函数:`float normC(int py,int px,int color,pixel**imgI,int sizeY,int sizeX){
int jx, jy;
float num=0, den=0;
if (color==R) {
for (jy=0; jy<sizeY; jy++) { …Run Code Online (Sandbox Code Playgroud) 继一些在线调查(1,2,numpy的,SciPy的,scikit,数学),我已经找到了计算的几种方法在Python欧氏距离:
# 1
numpy.linalg.norm(a-b)
# 2
distance.euclidean(vector1, vector2)
# 3
sklearn.metrics.pairwise.euclidean_distances
# 4
sqrt((xa-xb)^2 + (ya-yb)^2 + (za-zb)^2)
# 5
dist = [(a - b)**2 for a, b in zip(vector1, vector2)]
dist = math.sqrt(sum(dist))
# 6
math.hypot(x, y)
Run Code Online (Sandbox Code Playgroud)
我想知道是否有人可以提供有关上述哪一项(或任何其他我未发现的)在效率和精度方面被认为是最佳的见解.如果有人知道讨论该主题的任何资源也会很棒.
的背景下,我在有趣的是,在计算对数元组之间的欧氏距离,例如之间的距离(52, 106, 35, 12)和(33, 153, 75, 10).
我有一个整数向量vec1,我正在使用dist函数生成一个远程矩阵。我想获取距离矩阵中某个值的元素的坐标(行和列)。本质上,我想获得相距 d 距离的一对元素。例如:
vec1 <- c(2,3,6,12,17)
distMatrix <- dist(vec1)
# 1 2 3 4
#2 1
#3 4 3
#4 10 9 6
#5 15 14 11 5
Run Code Online (Sandbox Code Playgroud)
说,我对向量中相距 5 个单位的一对元素感兴趣。我想得到坐标 1 是行和坐标 2 是距离矩阵的列。在这个玩具示例中,我希望
coord1
# [1] 5
coord2
# [1] 4
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一种有效的方法来获取这些值而不涉及将dist对象转换为矩阵或遍历矩阵?
给定两个维度为 (2,2,2) 的 3-D 数组:
A = [[[ 0, 0],
[92, 92]],
[[ 0, 92],
[ 0, 92]]]
B = [[[ 0, 0],
[92, 0]],
[[ 0, 92],
[92, 92]]]
Run Code Online (Sandbox Code Playgroud)
你如何有效地找到 A 和 B 中每个向量的欧几里德距离?
我尝试过 for 循环,但这些循环很慢,我正在按 (>>2, >>2, 2) 的顺序处理 3-D 数组。
最终我想要一个形式的矩阵:
C = [[d1, d2],
[d3, d4]]
Run Code Online (Sandbox Code Playgroud)
编辑:
我尝试了以下循环,但最大的问题是丢失了我想要保留的尺寸。但距离是正确的。
[numpy.sqrt((A[row, col][0] - B[row, col][0])**2 + (B[row, col][1] -A[row, col][1])**2) for row in range(2) for col in range(2)]
Run Code Online (Sandbox Code Playgroud) 我有两个清单
available_points = [[2,3], [4,5], [1,2], [6,8], [5,9], [51,35]]
和
solution = [[3,5], [2,1]]
我想弹出一个点available_points,并追加它solution用于从该点欧氏距离在总和,所有点solution是最大的.
所以,我会得到这个
solution = [[3,5], [2,1], [51,35]]
我能够选择这样的最初的2个最远点,但不知道如何继续.
import numpy as np
from scipy.spatial.distance import pdist, squareform
available_points = np.array([[2,3], [4,5], [1,2], [6,8], [5,9], [51,35]])
D = squareform(pdist(available_points)
I_row, I_col = np.unravel_index(np.argmax(D), D.shape)
solution = available_points[[I_row, I_col]]
Run Code Online (Sandbox Code Playgroud)
这给了我
solution = array([[1, 2], [51, 35]])
我想知道是否有一种方法可以获取postgres中两个向量的余弦距离。为了存储向量,我使用了CUBE数据类型。
下面是我的表定义:
test=# \d vectors
Table "public.vectors"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+-------------------------------------
id | integer | | not null | nextval('vectors_id_seq'::regclass)
vector | cube | | |
Run Code Online (Sandbox Code Playgroud)
另外,下面给出了示例数据:
test=# select * from vectors order by id desc limit 2;
id | vector
---------+------------------------------------------
2000000 | (109, 568, 787, 938, 948, 126, 271, 499)
1999999 | (139, 365, 222, 653, 313, 103, 215, 796)
Run Code Online (Sandbox Code Playgroud)
我实际上可以为此编写自己的PLPGSql函数,但想避免这种情况,因为它可能效率不高。
postgresql vector euclidean-distance cosine-similarity postgresql-11
matrix ×3
python ×3
r ×3
matlab ×2
numpy ×2
c ×1
distance ×1
geometry ×1
knn ×1
math ×1
optimization ×1
pdist ×1
performance ×1
postgresql ×1
python-3.x ×1
scipy ×1
vector ×1