如何计算 1d 和 nd 数组之间的距离公式?
How to compute distance formula between a 1d and an nd array?
我正在尝试减去一维数组:
probe = [1, 2, 3, 4, 5, 6]
来自 nd 数组中的每个元素:
k = np.array([["words words ", 1,1,3,4,6,7], ["blah blah", 2,8,7,5,3,2], [" please help me", 3,4, 5, 6, 7,1], [" What are you doing, man", 1,3,5,10,9,11]])
我删除了 k 数组的第 0 个索引并将这些值存储在 new_k 中,这样现在就可以在我要比较的两个数组中的值之间进行比较。
new_k = k
new_k = np.delete(new_k, 0, axis=1)
我试图在 nd 数组中找到最接近 input.I 需要帮助的值。
到目前为止我已经设法到达这里,但我迷路了。:
for i in range(len(new_k)):
for j in range(len(new_k[0][1])):
temp[j] = (new_k[1][j] - probe[j])
new_k[i][1] = temp
print(new_k)
new_k2 = new_k*new_k
以上代码抛出此错误:
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
另外:我知道如何通过将单个值与值数组进行比较来获得 "closest value to",但我想将数组与数组
进行比较
np.asarray(k[:,1:], dtype=int) - probe
问题是 numpy 不像普通列表,它一次只包含唯一的类型元素。如果有超过 1 种类型,则全部转换为 U32
,即 string
。当您从列表中删除第一个元素时,其余元素仍然是字符串类型,而不是 int。您应该将它们转换为 int
。因此出现此错误:
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
当你尝试这个时:
temp[j] = (new_k[1][j] - probe[j])
In [263]: probe = [1, 2, 3, 4, 5, 6]
k
有一个 dtype
来容纳字符串:
In [264]: k = np.array([["words words ", 1,1,3,4,6,7], ["blah blah", 2,8,7,5,3,2
...: ], [" please help me", 3,4, 5, 6, 7,1], [" What are you doing, man", 1
...: ,3,5,10,9,11]])
In [265]: k
Out[265]:
array([['words words ', '1', '1', '3', '4', '6', '7'],
['blah blah', '2', '8', '7', '5', '3', '2'],
[' please help me', '3', '4', '5', '6', '7', '1'],
[' What are you doing, man', '1', '3', '5', '10', '9', '11']],
dtype='<U24')
但通过切掉第一列,其余部分可以转换为整数数组:
In [266]: k[:, 1:].astype(int)
Out[266]:
array([[ 1, 1, 3, 4, 6, 7],
[ 2, 8, 7, 5, 3, 2],
[ 3, 4, 5, 6, 7, 1],
[ 1, 3, 5, 10, 9, 11]])
这是一个 (4,6) 数组。 np.array(probe)
是 (6,);广播让我们减去:
In [270]: k[:, 1:].astype(int) - np.array(probe)
Out[270]:
array([[ 0, -1, 0, 0, 1, 1],
[ 1, 6, 4, 1, -2, -4],
[ 2, 2, 2, 2, 2, -5],
[ 0, 1, 2, 6, 4, 5]])
Numpy 数组并不意味着具有多种数据类型。 k
实际上是一个字符串数组,因此数组 k
.
中没有要减去的数字数据类型
如果您想保留第一列字符串,您应该查看 pandas 数据帧。您可以将字符串保留为数据帧的索引,但仍将其作为数字数据数组进行操作。使用您的数据,您可以执行以下操作:
df = pd.DataFrame(k[:, 1:].astype(int), index=k[:, 0])
df - probe
我正在尝试减去一维数组:
probe = [1, 2, 3, 4, 5, 6]
来自 nd 数组中的每个元素:
k = np.array([["words words ", 1,1,3,4,6,7], ["blah blah", 2,8,7,5,3,2], [" please help me", 3,4, 5, 6, 7,1], [" What are you doing, man", 1,3,5,10,9,11]])
我删除了 k 数组的第 0 个索引并将这些值存储在 new_k 中,这样现在就可以在我要比较的两个数组中的值之间进行比较。
new_k = k
new_k = np.delete(new_k, 0, axis=1)
我试图在 nd 数组中找到最接近 input.I 需要帮助的值。
到目前为止我已经设法到达这里,但我迷路了。:
for i in range(len(new_k)):
for j in range(len(new_k[0][1])):
temp[j] = (new_k[1][j] - probe[j])
new_k[i][1] = temp
print(new_k)
new_k2 = new_k*new_k
以上代码抛出此错误:
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
另外:我知道如何通过将单个值与值数组进行比较来获得 "closest value to",但我想将数组与数组
进行比较np.asarray(k[:,1:], dtype=int) - probe
问题是 numpy 不像普通列表,它一次只包含唯一的类型元素。如果有超过 1 种类型,则全部转换为 U32
,即 string
。当您从列表中删除第一个元素时,其余元素仍然是字符串类型,而不是 int。您应该将它们转换为 int
。因此出现此错误:
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
当你尝试这个时:
temp[j] = (new_k[1][j] - probe[j])
In [263]: probe = [1, 2, 3, 4, 5, 6]
k
有一个 dtype
来容纳字符串:
In [264]: k = np.array([["words words ", 1,1,3,4,6,7], ["blah blah", 2,8,7,5,3,2
...: ], [" please help me", 3,4, 5, 6, 7,1], [" What are you doing, man", 1
...: ,3,5,10,9,11]])
In [265]: k
Out[265]:
array([['words words ', '1', '1', '3', '4', '6', '7'],
['blah blah', '2', '8', '7', '5', '3', '2'],
[' please help me', '3', '4', '5', '6', '7', '1'],
[' What are you doing, man', '1', '3', '5', '10', '9', '11']],
dtype='<U24')
但通过切掉第一列,其余部分可以转换为整数数组:
In [266]: k[:, 1:].astype(int)
Out[266]:
array([[ 1, 1, 3, 4, 6, 7],
[ 2, 8, 7, 5, 3, 2],
[ 3, 4, 5, 6, 7, 1],
[ 1, 3, 5, 10, 9, 11]])
这是一个 (4,6) 数组。 np.array(probe)
是 (6,);广播让我们减去:
In [270]: k[:, 1:].astype(int) - np.array(probe)
Out[270]:
array([[ 0, -1, 0, 0, 1, 1],
[ 1, 6, 4, 1, -2, -4],
[ 2, 2, 2, 2, 2, -5],
[ 0, 1, 2, 6, 4, 5]])
Numpy 数组并不意味着具有多种数据类型。 k
实际上是一个字符串数组,因此数组 k
.
如果您想保留第一列字符串,您应该查看 pandas 数据帧。您可以将字符串保留为数据帧的索引,但仍将其作为数字数据数组进行操作。使用您的数据,您可以执行以下操作:
df = pd.DataFrame(k[:, 1:].astype(int), index=k[:, 0])
df - probe