numpy片与循环
我仍然在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试将整个numpy阵列/切片转换为单个整数值,这是不可能的。相反,您应该使用
astype
让Numpy这样做。试试看
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