03 使用numpy内置函数创建ndarray

目录

一、zeros

二、ones

三、full

四、eye

五、diag

六、arange

七、linespace

八、reshape

九、random

 


使用ndarray提供的内置函数可以快速,简单的创建需要的ndarray数组。

一、zeros

import numpy as np

#np.zeros 创建用0填充的ndarray
arr = np.zeros((3, 4))

print('arr={}'.format(arr))
print('arr.shape={}'.format(arr.shape))
print('arr.dtype={}'.format(arr.dtype))

运行结果:

arr=[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
arr.shape=(3, 4)
arr.dtype=float64

二、ones

import numpy as np

#np.ones创建用1填充的ndarray
arr = np.ones((3, 4))

print('arr={}'.format(arr))
print('arr.shape={}'.format(arr.shape))
print('arr.dtype={}'.format(arr.dtype))

运行结果:

arr=[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]
arr.shape=(3, 4)
arr.dtype=float64

三、full

import numpy as np

#np.full 创建用自定义内容(20)填充的ndarray
arr = np.full((3, 4), 20)

print('arr={}'.format(arr))
print('arr.type={}'.format(type(arr)))
print('arr.shape={}'.format(arr.shape))
print('arr.dtype={}'.format(arr.dtype))

运行结果:

arr=[[20 20 20 20]
 [20 20 20 20]
 [20 20 20 20]]
arr.type=<class 'numpy.ndarray'>
arr.shape=(3, 4)
arr.dtype=int64

四、eye

import numpy as np

#np.eye(N) 创建一个 N*N的单位矩阵。单位矩阵是主对角线上全是 1,其他位置全是 0 的方形矩阵。
arr = np.eye(3)

print('arr={}'.format(arr))
print('arr.type={}'.format(type(arr)))
print('arr.shape={}'.format(arr.shape))
print('arr.dtype={}'.format(arr.dtype))

运行结果:

arr=[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
arr.type=<class 'numpy.ndarray'>
arr.shape=(3, 3)
arr.dtype=float64

五、diag

import numpy as np

#np.diag(list) 创建一个指定list的对角矩阵
arr = np.diag((10, 20, 30))

print('arr={}'.format(arr))
print('arr.type={}'.format(type(arr)))
print('arr.shape={}'.format(arr.shape))
print('arr.dtype={}'.format(arr.dtype))

运行结果:

arr=[[10  0  0]
 [ 0 20  0]
 [ 0  0 30]]
arr.type=<class 'numpy.ndarray'>
arr.shape=(3, 3)
arr.dtype=int64

六、arange

#np.arange(N) 将创建一个秩为 1 的 ndarray,其中包含从 0 到 N - 1 的连续整数。
arr = np.arange(10)
print('arr={}'.format(arr))
print('--------------------------------')

#np.arange(start,stop) 将创建一个秩为 1 的 ndarray,其中包含位于半开区间 [start, stop) 内并均匀分布的值。
arr = np.arange(4, 10)
print('arr={}'.format(arr))
print('--------------------------------')

#np.arange(start,stop,step) 将创建一个秩为 1 的 ndarray
# 其中包含位于半开区间 [start, stop) 内并均匀分布的值,step 表示两个相邻值之间的差。
arr = np.arange(4, 15, 3)
print('arr={}'.format(arr))

运行结果:

arr=[0 1 2 3 4 5 6 7 8 9]
--------------------------------
arr=[4 5 6 7 8 9]
--------------------------------
arr=[ 4  7 10 13]

七、linespace

import numpy as np

#np.linspace(start, stop, N) 函数返回 N 个在闭区间 [start, stop] 内均匀分布的数字
#endpoint 表示是否包含最后stop
arr = np.linspace(0, 10, 4, endpoint=False)
print('arr={}'.format(arr))

运行结果:

arr=[0.  2.5 5.  7.5]

八、reshape

import numpy as np

#np.reshape(ndarray, new_shape) 函数会将给定 ndarray 转换为指定的 new_shape。
#注意:new_shape 应该与给定 ndarray 中的元素数量保持一致
arr = np.arange(0, 10)
arr_new = np.reshape(arr, (2, 5))

print('arr={}'.format(arr))
print('------------------------')
print(arr_new)

运行结果:

arr=[0 1 2 3 4 5 6 7 8 9]
------------------------
arr_new = [[0 1 2 3 4]
 [5 6 7 8 9]]

九、random

import numpy as np

#np.random(shape)函数创建具有给定形状的 ndarray
# 其中包含位于半开区间 [0.0, 1.0) 内的随机浮点数。
arr = np.random.random((2, 3))
print('arr={}'.format(arr))
print('arr.shape={}'.format(arr.shape))
print('----------------------------------')

#np.random.randint(start, stop, size = shape) 会创建一个具有给定形状的 ndarray
# 其中包含在半开区间 [start, stop) 内的随机整数
arr = np.random.randint(4, 9, size=(3, 4))
print('arr={}'.format(arr))
print('arr.shape={}'.format(arr.shape))
print('----------------------------------')

#np.random.normal(mean, standard deviation, size=shape) 会创建一个具有给定形状的 ndarray
#其中包含从正态高斯分布(具有给定均值和标准差)中抽样的随机数字
# 我们来创建一个 1,000 arr 1,000 ndarray,其中包含从正态分布(均值为 0,标准差为 0.1)中随机抽样的浮点数。
arr = np.random.normal(0, 0.1, size=(1000,1000))
print('arr.shape={}'.format(arr.shape))
print('arr type={}'.format(type(arr)))
print('arr.dtype={}'.format(arr.dtype))
print('arr 的平均值={}'.format(arr.mean()))
print('arr 的最大值={}'.format(arr.max()))
print('arr 的最小值={}'.format(arr.min()))
print('arr 中小于0的元素个数为:{}'.format((arr < 0).sum()))
print('arr 中大于0的元素个数为:{}'.format((arr > 0).sum()))

运行结果:

arr=[[0.68767487 0.41654744 0.10873277]
 [0.93088438 0.91054255 0.70659358]]
arr.shape=(2, 3)
----------------------------------
arr=[[4 6 7 5]
 [8 7 4 5]
 [6 8 8 4]]
arr.shape=(3, 4)
----------------------------------
arr.shape=(1000, 1000)
arr type=<class 'numpy.ndarray'>
arr.dtype=float64
arr 的平均值=-8.940630904173296e-05
arr 的最大值=0.48415107922103773
arr 的最小值=-0.49444820736728473
arr 中小于0的元素个数为:500370
arr 中大于0的元素个数为:499630

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值