Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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 对矩阵应用操作时,结果不正确_Python_Numpy - Fatal编程技术网

Python 对矩阵应用操作时,结果不正确

Python 对矩阵应用操作时,结果不正确,python,numpy,Python,Numpy,我试图通过将numpy矩阵转换为图像来制作一个圆的图像,但是当输入值为50或更多时,图像上会出现奇怪的缺行。我怎样才能解决这个问题 输入决定了矩阵的大小,输入50将生成50乘50的矩阵。我是一个初学者程序员,这是我第一次问关于堆栈溢出的问题,所以请不要太苛刻:)这是我的代码 from PIL import Image import itertools np.set_printoptions(threshold=np.inf) inp = int(input("Input size of matr

我试图通过将
numpy矩阵
转换为图像来制作一个圆的图像,但是当输入值为
50
或更多时,图像上会出现奇怪的缺行。我怎样才能解决这个问题

输入决定了矩阵的大小,输入
50
将生成
50乘50的矩阵。我是一个初学者程序员,这是我第一次问关于堆栈溢出的问题,所以请不要太苛刻:)这是我的代码

from PIL import Image
import itertools
np.set_printoptions(threshold=np.inf)
inp = int(input("Input size of matrix"))
dt = np.dtype(np.int8)
M = np.zeros((inp, inp), dtype=dt)
A = (list(itertools.product(range(0, inp), repeat=2)))
count1 = 0
for n in A:
    x = (int(n[0]) / (inp - 1)) * 2
    y = (int(n[1]) / (inp - 1)) * 2
    if (x ** 2) + (y ** 2) - (2 * x) - (2 * y) <= -1:
        M[int(x * (inp - 1)/2), int(y * (inp - 1)/2)] = 1
        count1 += 1
print(M)
im = Image.fromarray(M * 255)
im.show()
print("Approximation of pi: " + str(4 * (count1 / inp ** 2))) ```
从PIL导入图像
进口itertools
np.set\u打印选项(阈值=np.inf)
inp=int(输入(“矩阵的输入大小”))
dt=np.dtype(np.int8)
M=np.zero((inp,inp),dtype=dt)
A=(列表(itertools.product(范围(0,inp),重复=2)))
count1=0
对于A中的n:
x=(int(n[0])/(inp-1))*2
y=(int(n[1])/(inp-1))*2

如果(x**2)+(y**2)-(2*x)-(2*y)问题在这行:
M[int(x*(inp-1)/2),int(y*(inp-1)/2]=1
实际上,这一行在某些索引中分配了两次1,并忽略了某些索引,因为您使用的是
int()
。使用
round()
获取最接近的整数。那会有帮助的。将此行更改为:
M[int(x*(inp-1)/2)、int(y*(inp-1)/2]=1
:将M[round(x*(inp-1)/2)、round(y*(inp-1)/2]=1

您的代码应该如下所示:

from PIL import Image
import itertools
np.set_printoptions(threshold=np.inf)
inp = int(input("Input size of matrix"))
dt = np.dtype(np.int8)
M = np.zeros((inp, inp), dtype=dt)
A = (list(itertools.product(range(0, inp), repeat=2)))
count1 = 0
for n in A:
    x = (int(n[0]) / (inp - 1)) * 2
    y = (int(n[1]) / (inp - 1)) * 2
    if (x ** 2) + (y ** 2) - (2 * x) - (2 * y) <= -1:
        M[round(x * (inp - 1)/2), round(y * (inp - 1)/2)] = 1
        count1 += 1
print(M)
im = Image.fromarray(M * 255)
im.show()
print("Approximation of pi: " + str(4 * (count1 / inp ** 2)))
从PIL导入图像
进口itertools
np.set\u打印选项(阈值=np.inf)
inp=int(输入(“矩阵的输入大小”))
dt=np.dtype(np.int8)
M=np.zero((inp,inp),dtype=dt)
A=(列表(itertools.product(范围(0,inp),重复=2)))
count1=0
对于A中的n:
x=(int(n[0])/(inp-1))*2
y=(int(n[1])/(inp-1))*2

如果(x**2)+(y**2)-(2*x)-(2*y)我说奇怪的行只出现在等于或大于50的值上,但我意识到问题发生在输入=48,而不是输入=49,所以我不确定是什么happening@CarlosViramontes嗨,我更新了答案。现在有两种解决方案。我想最新的答案解决了你的问题。如果你这么认为,那么请接受我的回答。谢谢。非常感谢,谢谢你检查我的代码为什么不起作用。它帮助我解决了另一个我试图用不同代码解决的问题。
import itertools
import numpy as np
np.set_printoptions(threshold=np.inf)
inp = int(input("Input size of matrix"))
dt = np.dtype(np.int8)
M = np.zeros((inp, inp), dtype=dt)
A = (list(itertools.product(range(0, inp), repeat=2)))
# assign the center
cx,cy=int(inp/2), int(inp/2)
# assign the radius
rad=int(inp/2)
count1 = 0
for n in A:
    # calculate distance of a point from the center
    dist = np.sqrt((n[0]-cx)**2+(n[1]-cy)**2)
    # Assign 1 where dist < rad.
    if dist < rad:
        M[n[0], n[1]] = 1
        count1 += 1

print(M)
im = Image.fromarray(M * 255)
im.show()
print("Approximation of pi: " + str(4 * (count1 / inp ** 2)))