numpy片与循环

发布于 2025-02-08 16:00:05 字数 571 浏览 2 评论 0原文

我仍然在python的阵列上围绕切片等。 我有一个循环,它可以完美地将2D阵列zmap的每个元素重新缩放到0和“ Palette_size-1”之间(是的,我在分形的情况下弄乱了)...所以一切都很好,但是我认为嵌套循环

效率

for x in range(self.res):
  for y in range(self.res):
     zScaled[x,y] = int((zMap[x,y] - np.amin(zMap)) * (palette_size-1) / 
                    (np.amax(zMap) - np.amin(zMap)))

低下线路休息在这里,以便于阅读...) 但是,这种切片的尝试给出了上述错误:

zScaled[:,:] = int((zMap[:,:] - np.amin(zMap)) * (palette_size-1) / 
                   (np.amax(zMap) - np.amin(zMap)))

再次添加了线路断裂以清楚。 任何帮助都将不胜感激

; 斯图尔特

I'm still getting my head around slices etc for arrays in python....
I have this in a loop, which works perfectly to re-scale each element of the 2D Array zMap to between 0 and "palette_size-1" (yes, I'm messing around with Fractals)... so all is good, but I think the nested loop is inefficient, so I wanted to try with slices but get this error: TypeError: only size-1 arrays can be converted to Python scalars

Here's the nested loop...

for x in range(self.res):
  for y in range(self.res):
     zScaled[x,y] = int((zMap[x,y] - np.amin(zMap)) * (palette_size-1) / 
                    (np.amax(zMap) - np.amin(zMap)))

(again, works just fine although I added the line break here for ease of reading...)
but this attempt at slices gives the aforementioned error:

zScaled[:,:] = int((zMap[:,:] - np.amin(zMap)) * (palette_size-1) / 
                   (np.amax(zMap) - np.amin(zMap)))

again added the line breaks for clarity.
Any help would be appreciated

Regards;
stuart

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酉 2025-02-15 16:00:05

您正在尝试将整个numpy阵列/切片转换为单个整数值,这是不可能的。相反,您应该使用astype让Numpy这样做。

试试看

Scaled[:,:] = ((zMap[:,:] - np.amin(zMap)) * (palette_size-1) / 
                   (np.amax(zMap) - np.amin(zMap))).astype(int)

You're trying to convert the entire numpy array/slice into a single integer value which isn't possible. Instead you should use astype to let numpy do so elementwise.

Try this instead

Scaled[:,:] = ((zMap[:,:] - np.amin(zMap)) * (palette_size-1) / 
                   (np.amax(zMap) - np.amin(zMap))).astype(int)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文