0s와 1s의 임의의 배열을 생성하려고하는데 오류가 발생합니다. 모양이 일치하지 않습니다. 개체를 단일 모양으로 브로드 캐스팅 할 수 없습니다. 이 오류는 줄 randints = np.random.binomial(1,p,(n,n))
에서 발생하는 것으로 보입니다. 내가 스스로 그것을 실행하면파이썬에서 numpy를 사용하여 "shape mismatch"오류가 발생했습니다.
import numpy as np
def rand(n,p):
'''Generates a random array subject to parameters p and N.'''
# Create an array using a random binomial with one trial and specified
# parameters.
randints = np.random.binomial(1,p,(n,n))
# Use nested while loops to go through each element of the array
# and assign True to 1 and False to 0.
i = 0
j = 0
rand = np.empty(shape = (n,n),dtype = bool)
while i < n:
while j < n:
if randints[i][j] == 0:
rand[i][j] = False
if randints[i][j] == 1:
rand[i][j] = True
j = j+1
i = i +1
j = 0
# Return the new array.
return rand
print rand
, 그것은 <function rand at 0x1d00170>
를 반환 여기에 기능입니다. 이것은 무엇을 의미 하는가? 어떻게 다른 함수에서 사용할 수있는 배열로 변환해야합니까?
randints = np.random.binomial(1,p,(n,n))
이
0
및
1
값의 배열을 생산하고 당신은 그 모든 통해 갈 필요가 없습니다
실행중인 python 및 numpy의 버전은 무엇입니까? 위에 게시 된 코드는'i = i + 1'에 오판이 있습니다 ... – Jaime
이 프로그램을 실행할 때''을 인쇄하는 이유는'print rand' 행이 인쇄되기 때문입니다 함수 객체 함수를 대신 호출해야합니다. 대신'print rand (4,2)'를 시도하십시오. –
Wilduck