Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用数组列表上的计算更新numpy数组_Python_Numpy - Fatal编程技术网

Python 使用数组列表上的计算更新numpy数组

Python 使用数组列表上的计算更新numpy数组,python,numpy,Python,Numpy,我有一个长度为50的列表,其中填充了长度为5的数组。我试图计算列表中每个数组之间的距离,并用这些值更新numpy数组 距离计算只是取阵列中每个元素之间距离平方和的平方根 当我尝试时: primaryCustomer = np.zeros(shape = (50,50)) for customer in range(0,50): for pair in range(0,50): thisCustomer = [0 for i in range(51)] if customer

我有一个长度为50的列表,其中填充了长度为5的数组。我试图计算列表中每个数组之间的距离,并用这些值更新numpy数组

距离计算只是取阵列中每个元素之间距离平方和的平方根

当我尝试时:

primaryCustomer = np.zeros(shape = (50,50))

for customer in range(0,50):
  for pair in range(0,50):
    thisCustomer = [0 for i in range(51)]
    if customer == pair:
      thisCustomer[pair] = 999
    else:

      calculateScores = (((Customer[customer][0]-Customer[pair][0])**2 
                            + (Customer[customer][1]-Customer[pair][1])**2 
                            + (Customer[customer][2]-Customer[pair][2])**2 
                            + (Customer[customer][3]-Customer[pair][3])**2 
                            + (Customer[customer][4]-Customer[pair][4])**2 )**(0.5))
      thisCustomer[pair] = calculateScores
  np.append(primaryCustomer, thisCustomer)
发生了两件事:

  • thisCustomer的最后一次迭代返回一个全零的列表,除了999的最后一个元素(对应于上面语句的“if”部分)。因此,我知道它可以更新列表,但它在“else”部分无法更新
  • 我希望“primaryCustomer”数组以客户作为索引进行更新,以每对作为行值进行所有计算的分数,但它似乎根本没有更新
我所做的任何更改,比如试图将循环中的这个客户作为一个数组而不是一个列表并附加到其中,最终都会修复一个区域,但会更糟糕地破坏其他区域

以下是我获取客户数据的方式:

Customer = [[0,0,0,0,0] for i in range(51)]

for n in range(51):
  Customer[n] = np.ones(5)
  Customer[n][randint(2,4):5] = 0
  np.random.shuffle(Customer[n])
我知道可能有打包的方法可以做到这一点,但我正试图理解KNN之类的东西在后台是如何工作的,所以我想继续找出上面循环中的逻辑。除此之外,任何帮助都将不胜感激。

我想这就是你的目的,但如果我错了,请纠正我:

import numpy as np
from random import randint

Customer = [[0, 0, 0, 0, 0] for i in range(51)]

for n in range(51):
    Customer[n] = np.ones(5)
    Customer[n][randint(2, 4):5] = 0
    np.random.shuffle(Customer[n])

primaryCustomer = np.zeros(shape=(50, 50))

for customer in range(0, 50):
    thisCustomer = [0 for i in range(51)]
    for pair in range(0, 50):
        if customer == pair:
            primaryCustomer[customer][pair] = 999
        else:
            calculateScores = (((Customer[customer][0] - Customer[pair][0]) ** 2
                                  + (Customer[customer][1] - Customer[pair][1]) ** 2
                                  + (Customer[customer][2] - Customer[pair][2]) ** 2
                                  + (Customer[customer][3] - Customer[pair][3]) ** 2
                                  + (Customer[customer][4] - Customer[pair][4]) ** 2) ** 0.5)
            primaryCustomer[customer][pair] = calculateScores

print(primaryCustomer)

我想我在你的循环中发现的主要问题是
thisCustomer=[0 for I in range(51)]
的位置,我想你是想让它像我一样再上一层楼。我不认为有任何必要使用这一行,而是将
thisCustomer[pair]
改为直接写入
primaryCustomer[customer][pair]
,从而消除了对
thisCustomer=[0 for I in range(51)]
每个循环的需要,这样可以通过完全删除该行来加快程序并提高内存使用率

样本输出:

[999.2.23606798 1…2.0。 1.73205081] [ 2.23606798 999. 2. ... 1. 2.23606798 1.41421356] [ 1. 2. 999. ... 1.73205081 1. 2. ] ... [ 2. 1. 1.73205081 ... 999. 2. 1.73205081] [ 0. 2.23606798 1. ... 2. 999. 1.73205081] [ 1.73205081 1.41421356 2. ... 1.73205081 1.73205081 999.]]


首先要注意几件事

  • primaryCustomer[a][b]=primaryCustomer[b][a]
    因为您使用的是距离度量。这意味着可以重置两个for循环上的范围:
注意*您可以将第二个for循环的范围更改为也从0开始,以保持原始循环结构,但随后需要删除换位线。你也可以
primaryCustomer[a][b]=primaryCustomer[b][a]=dist(a,b)
如果您不想使用转置,但仍然避免不必要的计算

  • primaryCustomer=np.zero(shape=(50,50))
    我假设它是用来存储两个客户之间的距离的。然而,看起来你有51个客户,而不是50个
  • 您应该考虑以更一般的方式计算距离。i、 e.如何使距离计算工作独立于列表大小
  • 为什么要创建一个0的初始二维数组来存储距离,然后附加到距离上?创建
    thisCustomer
    列表似乎没有必要,事实上,Reedinationer发布的解决方案对其进行了初始化,但从未使用过它。另外,正如有人说的那样,
    np.append
    不是这样工作的。最好直接修改最初创建的距离矩阵
  • 为什么
    主要客户[a][a]=999
    ?列表与其自身之间的距离不应该是0吗?如果你真的想让它成为999,我鼓励你找出如何修改上面的代码块来解释这一点

这不是您使用的
np.append
。阅读文档,然后远离这个名称不好的函数。您已经为数组中的这些值分配了空间。为它们指定普通数组索引。您表明您已经知道如何为列表元素赋值。@datahappy:如果我理解您的帖子是正确的,您就不需要有效的(numpy ish)解决方案。你只想坚持你的循环,并修复它们,让它们工作。这种理解正确吗?@fountainhead我想是的。这就是我阅读它的方式,至少
primaryCustomer[customer,pair]=calculateScores
更为惯用。@hpaulj谢谢!我将把它留给OP,让他/她选择什么样的实现更具可读性。我通常喜欢在我的帖子中使用双索引,因为我可以将一个可视化为行,一个可视化为列,但这是一个很好的技巧。使用标量索引时,双索引效果很好。第一个索引选择一行,第二个索引从该行中选择一个元素。但是如果你使用切片或列表,它将返回多行。@hpaulj那么你是说你可以用你的方法做切片表示法?类似于
primaryCustomer[[0:customer],[0:pair]]=someArray
的内容,您不能将其重写为
primaryCustomer[0:customer][0:pair]=someArray
?谢谢。这很有帮助
    numCustomers = 51
    primaryCustomer = np.zeros(shape = (numCustomers, numCustomers))
    for customerA in range(numCustomers-1):
        for customerB in range(customerA+1, numCustomers):
            primaryCustomer[customerA][customerB] = dist(customerA,customerB)
    primaryCustomer += np.transpose(primaryCustomer)