python variable shape 不匹配_在python中使用numpy的“形状不匹配”错误(“shape mismatch” error using numpy in python)...

在尝试生成0和1随机数组时遇到了numpy的“形状不匹配”错误。问题出现在`np.random.binomial(1,p,(n,n))`行。解决方案是直接使用此表达式生成所需数组,而无需额外的while循环进行转换。" 124290932,10893495,Pulsar 2.9.1 在Linux上的集群部署实战,"['Pulsar', '分布式', '消息队列', '集群部署', 'Linux']
摘要由CSDN通过智能技术生成

在python中使用numpy的“形状不匹配”错误(“shape mismatch” error using numpy in python)

我试图生成一个0和1的随机数组,我得到错误:形状不匹配:对象不能广播到单个形状。 该错误似乎发生在行randints = np.random.binomial(1,p,(n,n)) 。 这是功能:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值