[英]How to insert value in numpy ndArray?
I have two ndArray.我有两个 ndArray。
ex:
x = np.array([110,200, 500,100])
y = np.array([50,150,30,70])
Now based on their value I have created an image.现在根据它们的价值,我创建了一个图像。
x_shape = np.max(x) #x_shape=500
y_shape = np.max(y) #y-shape=150
image = np.zeros((x_shape+1, y_shape+1))
according to my data now my image size is (501,151)根据我的数据,现在我的图像大小是 (501,151)
Now, How can I insert data from (x, y) as x,y pair?现在,如何将 (x, y) 中的数据作为 x,y 对插入? I mean for the pixel value: (110,50), (200,150), (500,30), (100,70) I want the image will be white and the rest pixel will be dark.
我的意思是像素值:(110,50), (200,150), (500,30), (100,70) 我希望图像是白色的,其余像素是暗的。 How can I achieve this?
我怎样才能做到这一点?
Based on OP's own answer , one can improve it by using a vectorized approach:根据OP 自己的答案,可以通过使用矢量化方法来改进它:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([110,200, 500,100])
y = np.array([50,150,30,70])
x = np.floor(x / 10).astype(int)
y = np.floor(y / 10).astype(int)
x_shape = np.max(x) # x_shape = 500
y_shape = np.max(y) # y_shape = 150
image = np.zeros((x_shape + 10, y_shape + 10))
image[x, y] = 10
plt.imshow(image)
(To be fair, I did not understand from the question that this is what OP was after). (公平地说,我不明白这是 OP 所追求的问题)。
To address the "visualization issue" without resizing from the comments:要在不调整评论大小的情况下解决“可视化问题”:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([110, 200, 500, 100])
y = np.array([50, 150, 30, 70])
x_shape = np.max(x)
y_shape = np.max(y)
image = np.zeros((x_shape + 1, y_shape + 1))
image[x, y] = 10
plt.figure(figsize=(20, 20))
plt.imshow(image.transpose(), interpolation='nearest', aspect='equal')
not sure exactly what do you need you may try不确定你到底需要什么,你可以试试
a = np.array([110, 200, 500, 100])
b = np.array([50, 150, 30, 70])
np.array([zip(x,y) for x,y in zip(a,b)])
pd.DataFrame(list(zipped))```
##or another representation
np.dstack((x,y))
both are taken from https://stackoverflow.com/questions/49461605/why-do-we-need-to-convert-a-zipped-object-into-a-list
[1]: https://stackoverflow.com/questions/49461605/why-do-we-need-to-convert-a-zipped-object-into-a-list
Well, I got the answer.嗯,我得到了答案。 It was easy and as I am new it makes me confused.
这很容易,因为我是新手,这让我感到困惑。
import numpy as np
import matplotlib.pyplot as plt
x = np.array([110,200, 500,100])
y = np.array([50,150,30,70])
x = np.floor(x/10).astype(int) #devided by 10 to reduce the img size
y = np.floor(y/10).astype(int) #devided by 10 to reduce the img size
x_shape = np.max(x) #x_shape=500
y_shape = np.max(y) #y-shape=150
image = np.zeros((x_shape+10, y_shape+10))
for x, y in zip(x,y):
image[x,y]=200
plt.imshow(image)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.