2

【Numpy总结】第三节:Numpy创建数组

 1 year ago
source link: https://blog.51cto.com/u_15807450/5721835
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Table of Contents

一、标准数组的创建

1.1 numpy.empty 创建空数组

用来创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组;由于未初始化,所以数组中的数据是随机的;

numpy.empty(shape, dtype = float, order = 'C')

参数 描述
shape 数组形状
dtype 数据类型,可选
order 有"C"和"F"两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序。<br>一般情况下不必关注
a = np.empty((4,3),dtype=int)
print(a)   # 每次输出都不同,因为没有初始化
# [[-958363344        464 -958381568]
# [       464 -958387104        464]
# [-958380912        464 -958380224]
# [       464 -958380224        464]]

1.2 numpy.zeros 创建0数组

用来创建一个指定形状(shape)的数组,并全部初始化为0
举例:

a = np.ones((4,3))
print(a)  
# [[1 1 1]
# [1 1 1]
# [1 1 1]
# [1 1 1]]

1.3 numpy.ones 创建1数组

用来创建一个指定形状(shape)的数组,并全部初始化为1
举例:

a = np.zeros((4,3))
print(a)
# [[0 0 0]
#  [0 0 0]
#  [0 0 0]
#  [0 0 0]]

二、创建一般数组

2.0 利用list 创建数组 numpy.array

格式为: numpy.array(object, dtype=None) ,其中:

参数 描述
object 创建的数组的对象,可以为单个值,列表,元胞等。
dtype 数据类型,可选
array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(array)   # [0 1 2 3 4 5 6 7 8 9]
print(array.dtype)   # int32 

2.1 利用list 创建数组 numpy.asarray

格式为: numpy.asarray(a, dtype = None, order = None) ,其中:

参数 描述
a 任意形式的输入参数,可以是: 列表, 列表的元组, 元组, 元组的元组, 元组的列表,多维数组
dtype 数据类型,可选
order 可选,有"C"和"F"两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序。
a = [1,3,1,5,4]
b = np.asarray(a)
print(type(a))   # <class 'list'>
print(type(b))   # <class 'numpy.ndarray'>

2.2 利用可迭代对象创建数组 numpy.fromiter

格式为:numpy.fromiter(iterable, dtype, count=-1) ,其中:

参数 描述
iterable 可迭代对象
dtype 返回数组的数据类型
count 读取的数据数量,默认为-1,读取所有数据
list=range(5)
it=iter(list)
x=np.fromiter(it, dtype=float)
print(x)

2.3 利用数值范围创建数组 numpy.arange

格式为:numpy.arange(start, stop, step, dtype) ,其中:

参数 描述
start 起始值,默认为0
stop 终止值(不包含)
step 步长,默认为1
dtype 返回ndarray的数据类型,如果没有提供,则会使用输入数据的类型。
a = np.arange(10)
print(a)  # 输出:[0 1 2 3 4 5 6 7 8 9]

b = np.arange(10,20,2)
print(b)  # 输出:[10 12 14 16 18]

2.4 利用数值范围创建数组 numpy.linspace

格式为:np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) ,其中:

参数 描述
start 序列的起始值
stop 序列的终止值,如果endpoint为true,该值包含于数列中
num 要生成的等步长的样本数量,默认为50
endpoint 该值为 true 时,数列中包含stop值,反之不包含,默认是True。
retstep 如果为 True 时,生成的数组中会显示间距,反之不显示。
dtype ndarray 的数据类型
a = np.linspace(10,20,5,endpoint=False)  # 从10开始,20结束,一共产生5个数字,不包含20
print(a)   # 输出:[10. 12. 14. 16. 18.]
b = np.linspace(10,20,5,endpoint=True)
print(b)    # 输出:[10.  12.5 15.  17.5 20. ]

三、创建随机数组

3.1 创建整数随机数组:np.random.randint

格式为 : np.random.randint(0, 100, (3, 4))

在使用random之前,可以通过 np.random.seed(666) 来设置随机种子,这一点与Python一致;

a = np.random.randint(0, 100, (3, 4))
print(a)
# 输出为:
# [[92 58 18 32]
#  [ 4 87 81  1]
#  [12 11 13 68]]

3.2 创建浮点型随机数组

只要在整数的基础上除以整数即可,例如需要创建一个取值范围在0,1之间,精度为0.01的浮点型数组,可以使用如下方法:

a = np.random.randint(0, 100, (3, 4))
b= a/100
print(b)
# 输出为:
# [[0.05 0.48 0.72 0.95]
#  [0.68 0.78 0.22 0.98]
#  [0.17 0.45 0.7  0.85]]

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK