numpy建立数组的几种常见方法

np.array

list_1 = [1,5,7,9,5,1]
list_1_1 = np.array(list_1)

np.zeros 构建全为0的矩阵

list_1 = np.zeros(10,dtype = float)
print(list_1)
#anwser
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] 

type of list_1 is <class ‘numpy.ndarray’>

np.ones 构建全为1的矩阵

list_1 = np.ones((3,5),dtype = int)
print(list_1)
# anwser
[[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]

np.full

list_1 = np.full((3,5),3.14,dtype = int)
print(list_1)
#anwser
[[3 3 3 3 3]
 [3 3 3 3 3]
[3 3 3 3 3]]

np.linspace

list_1 = np.linspace(0,1,5) # 0到1的5个数
print(list_1)
# anwser 
[0.   0.25 0.5  0.75 1.  ]  #均分

np.random.randint

list_1 = np.random.randint(0,5,(3,3))  #0到5的3*3矩阵的随机数
print(list_1)
# anwser
[[3 2 2]
[4 0 2]
[0 4 1]]

np.random.normal

list_1 = np.random.normal(0,1,(3,3)) #方差为1,均值为0的3*3随机数矩阵
print(list_1)
# anwser
[[ 0.90235626  1.08201144 -0.02242838]
[ 1.65496962  1.87613763  1.09563896]
[ 0.8574219  -1.40170292 -1.8910511 ]]

np.eye

list_1 = np.eye(5) #建立一个5*5的单位矩阵
print(list_1)
#anwser
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值