请考虑:
daList = {{{21, 18}, {20, 18}, {18, 17}, {20, 15}},
{{21, 18}, {20, 18}, {21, 14}, {21, 14}}};
Run Code Online (Sandbox Code Playgroud)
我想计算该列表的2个子列表中每个点之间的距离:
但我需要使用a Function来应用于正确的级别:
Function[seqNo,
EuclideanDistance[#, {0, 0}] & /@ daList[[seqNo]]] /@
Range[Length@daList]
out = {{3 Sqrt[85], 2 Sqrt[181], Sqrt[613], 25}, {3 Sqrt[85], 2 Sqrt[181],
7 Sqrt[13], 7 Sqrt[13]}}
Run Code Online (Sandbox Code Playgroud)
有没有办法避免这种繁重的功能呢?使用seqNo作为参数指定避免我的函数的级别?:
EuclideanDistance[#, {0, 0}] & /@ daList
out={EuclideanDistance[{{21, 18}, {20, 18}, {18, 17}, {20, 15}}, {0, 0}],
EuclideanDistance[{{21, 18}, {20, 18}, {21, 14}, {21, 14}}, {0, 0}]}
Run Code Online (Sandbox Code Playgroud) 八度文档说pdist存在,但我不能在我在ubuntu 12.04上安装的版本中使用它.
八度版:
GNU Octave, version 3.6.2
Copyright (C) 2012 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. For details, type `warranty'.
Octave was configured for "x86_64-pc-linux-gnu"
Run Code Online (Sandbox Code Playgroud)
我需要为它安装任何软件包吗?
回复后:我试图安装统计包:
octave:1> pkg install -forge statistics
error: the following dependencies where unsatisfied:
statistics needs io >= 1.0.18
Run Code Online (Sandbox Code Playgroud) 我正在计算c ++中大量(~1e5)粒子数的势能.为了做到这一点,我正在运行一个双循环,我在其中计算成对距离,并从这些距离计算系统的总势能.下面是代码的相关部分(它不是复制/粘贴准备好的,因为需要定义数据,有些东西不在上下文中;方法仍然有效,这就是我在这里要展示的内容) :
int colstart = 2;
int colend = 4;
double PE = 0;
double p_mass = 8.721e9 * 1.989e30; // mass of sim particle in kg
double mpc_to_m = 3.08567758e22; // meters per mpc
double G = 6.67384e-11; // grav. constant in mks units
// Calculating PE
for(int i = 0; i < data.size()-1; i++) // -1 is for empty line at the end of every file
{
//cout << i << " " << data[i].size() << endl; …Run Code Online (Sandbox Code Playgroud) import math
from math import sqrt
Hailey=[0,4,1,4,0,0,4,1]
Verica=[3,0,0,5,4,2.5,3,0]
temp=[]
distance=0
x=0
for i in range(0,len(Hailey)):
if (Hailey[i]!=0 and Verica[i]!=0):
temp[x]=math.sqrt(abs(Hailey[i]**2) - abs(Verica[i]**2))
x=x+1
for i in range(0,len(temp)):
distance=distance+temp[i]
print("distance is",distance)
Run Code Online (Sandbox Code Playgroud)
我试图制作一个程序,找到两个人之间的欧几里德距离.它似乎没有数学上的正确性,我得到这个:
distance=distance + math.sqrt(abs(Hailey[i]**2) - abs(Verica[i]**2))
ValueError: math domain error
Run Code Online (Sandbox Code Playgroud) BigList = rand(20, 3)
LittleList = rand(5, 3)
Run Code Online (Sandbox Code Playgroud)
我想为大列表中的每一行找到小列表中的"最近"行,由欧几里德范数定义(即k = 3维度中相应值之间的平方距离之和).
我可以看到如何使用两个循环来做到这一点,但似乎应该有一个更好的方法来使用内置矩阵操作来做到这一点.
我有许多平行线段,例如L1(P1,P2)和L2(P3,P4)。这些点分别具有x和y坐标。这些平行线段的角度在0-180度之间。
如何有效地在c ++中找到这些线段之间的垂直空间?

我有2个numpy数组(比如X和Y),每行代表一个点向量.
我想在X中的每个点到Y中的每个点之间找到平方的欧氏距离(将其称为'dist').
我希望输出为矩阵D,其中D(i,j)是dist(X(i) ),Y(j)).
我有以下python代码基于:http://nonconditional.com/2014/04/on-the-trick-for-computing-the-squared-euclidian-distances-between-two-sets-of-vectors/
def get_sq_distances(X, Y):
a = np.sum(np.square(X),axis=1,keepdims=1)
b = np.ones((1,Y.shape[0]))
c = a.dot(b)
a = np.ones((X.shape[0],1))
b = np.sum(np.square(Y),axis=1,keepdims=1).T
c += a.dot(b)
c -= 2*X.dot(Y.T)
return c
Run Code Online (Sandbox Code Playgroud)
我试图避免循环(我应该吗?)并使用矩阵mult来进行快速计算.但我在大型阵列上遇到"内存错误"的问题.也许有更好的方法来做到这一点?
假设我有以下两个向量:
x = [(10-1).*rand(7,1) + 1; randi(10,1,1)];
y = [(10-1).*rand(7,1) + 1; randi(10,1,1)];
Run Code Online (Sandbox Code Playgroud)
前七个元素是 [1,10] 范围内的连续值。最后一个元素是 [1,10] 范围内的整数。
现在我想计算 x 和 y 之间的欧几里得距离。我认为整数元素是一个问题,因为所有其他元素都可以非常接近,但整数元素总是有 1 的间距。所以对整数元素有偏见。
我如何计算它的归一化欧几里德距离之类的东西?
dictionaries我的代码中已经计算了两个,如下所示:
X = {'a': 10, 'b': 3, 'c': 5, ...}
Y = {'a': 8, 'c': 3, 'e': 8, ...}
Run Code Online (Sandbox Code Playgroud)
实际上,它们包含来自Wiki文本的单词,但这应有助于显示我的意思。它们不一定包含相同的密钥。
最初,我想像这样使用sklearn的成对度量:
from sklearn.metrics.pairwise import pairwise_distances
obama = wiki[wiki['name'] == 'Barack Obama']['tf_idf'][0]
biden = wiki[wiki['name'] == 'Joe Biden']['tf_idf'][0]
obama_biden_distance = pairwise_distances(obama, biden, metric='euclidean', n_jobs=2)[0][0]
Run Code Online (Sandbox Code Playgroud)
但是,这会导致错误:
--------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-124-7ff03bd40683> in <module>()
6 biden = wiki[wiki['name'] == 'Joe Biden']['tf_idf'][0]
7
----> 8 obama_biden_distance = pairwise_distances(obama, biden, metric='euclidean', n_jobs=2)[0][0]
/home/xiaolong/development/anaconda3/envs/coursera_ml_clustering_and_retrieval/lib/python3.4/site-packages/sklearn/metrics/pairwise.py in pairwise_distances(X, Y, metric, n_jobs, **kwds) …Run Code Online (Sandbox Code Playgroud) 我从字典开始,这是我的数据已经被格式化的方式:
import pandas as pd
dict2 = {'A': {'a':1.0, 'b':2.0, 'd':4.0}, 'B':{'a':2.0, 'c':2.0, 'd':5.0},
'C':{'b':1.0,'c':2.0, 'd':4.0}}
Run Code Online (Sandbox Code Playgroud)
然后将其转换为熊猫数据框:
df = pd.DataFrame(dict2)
print(df)
A B C
a 1.0 2.0 NaN
b 2.0 NaN 1.0
c NaN 2.0 2.0
d 4.0 5.0 4.0
Run Code Online (Sandbox Code Playgroud)
当然,通过这样做,我可以一次获得一个差异:
df['A'] - df['B']
Out[643]:
a -1.0
b NaN
c NaN
d -1.0
dtype: float64
Run Code Online (Sandbox Code Playgroud)
我想出了如何遍历和计算AA,AB,AC:
for column in df:
print(df['A'] - df[column])
a 0.0
b 0.0
c NaN
d 0.0
Name: A, dtype: float64
a -1.0
b NaN
c …Run Code Online (Sandbox Code Playgroud) python ×4
matlab ×3
numpy ×3
c++ ×2
algorithm ×1
dataframe ×1
dictionary ×1
energy ×1
geometry ×1
lines ×1
math ×1
octave ×1
pandas ×1
performance ×1
scikit-learn ×1