在terminal中求和Python数组的结果与在scrip中求和Python数组的结果不同

2024-09-28 18:56:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数组定义如下:

particles=random.uniform(0,10, size=(100, 3))
displacement=random.uniform(-2, 2+1, size=(100, 3))

我想把粒子重新定义为它们的坐标和。简单的打字

particles = particles + displacement

进入终端后,我得到的正是我想要的,但当我运行脚本时,会收到错误消息:

ValueError: operands could not be broadcast together with shapes (100,3) (1,300)

是什么导致其中一个阵列改变形状?为什么终端中不会发生这种情况

编辑:以下是回溯:

File "<ipython-input-4-04059a7d3a12>", line 1, in <module>
runfile('C:/Users/Garaidh/Documents/Python Scripts/3D Brownian 
Tree/3DBrownianTree_fork1.py', wdir='C:/Users/Garaidh/Documents/Python 
Scripts/3D Brownian Tree')

File "C:\Users\Garaidh\Anaconda3\lib\site- 
packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
execfile(filename, namespace)

File "C:\Users\Garaidh\Anaconda3\lib\site- 
packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Garaidh/Documents/Python Scripts/3D Brownian 
Tree/3DBrownianTree_fork1.py", line 58, in <module>
particles=particles+displacement 
"""
something keeps fucking up here, I get an 
error that I don't get in the terminal
"""
ValueError: operands could not be broadcast together with shapes (100,3) 
(1,300)

代码如下:

import numpy as np
import numpy.random as random

from scipy.spatial import distance

t=0
patiencelevel=10000



region_length=100


seeds=[]
base_seed =[region_length/2,region_length/2,0];
seeds.append(base_seed)


particles=[]
numParticles=100
part_step=5



particle_Radius = 1


region_length=10
zero_array=np.zeros((numParticles, 3))
ceiling_array=np.full((numParticles, 3), region_length)
rad_array=np.full((numParticles, len(seeds)),particle_Radius)

particles=random.uniform(0,region_length, size=(numParticles, 3)) 

while len(particles)>0:
    displacement=random.uniform(-part_step, part_step+1, size= 
    (numParticles,3))
    particles=displacement+particles 
    """
    something keeps fucking up here, I get 
    an error that I do not get in the terminal    
    """
    particles=np.maximum(particles, zero_array)
    particles=np.minimum(particles, ceiling_array)
    particles=list(particles)

    templist=[]
    for j in range(0,len(seeds)): # for each seed
        for i in range(0,len(particles)):
            if distance.euclidean(particles[i],seeds[j]) 
            <=2*particle_Radius: 
                templist.append(particles[i]) 
    particles=[~np.in1d(particles,templist)]
    for x in templist:
        seeds.append(x) 
    if t > patiencelevel:
        break

Tags: insizenprandomuniformarraylengthusers