ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

NumPy数组的创建

2022-07-30 19:01:18  阅读:109  来源: 互联网

标签:10 创建 dtype print 数组 numpy np NumPy


一、NumPy 创建数组

ndarray 数组除了可以使用底层 ndarray 构造器来创建外,也可以通过以下几种方式来创建。

1.numpy.empty

numpy.empty 方法用来创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组:

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

 

shape 数组形状
dtype 数据类型,可选
order 有"C"和"F"两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序。

 

演示代码:

import numpy as np 
x = np.empty([3,2], dtype = int) 
print (x)

输出结果:

[[1632772128 1936028521]
 [ 538976266  757935392]
 [ 170732845  538976288]]

 

演示代码:

x = np.empty([1,1], dtype = int) 
print (x[0][0])

输出结果:

1970086003

注意:数组元素为随机值,因为它们未初始化。

2.numpy.zeros

创建指定大小的数组,数组元素以 0 来填充:

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

演示代码:

# 默认为浮点数
x = np.zeros(5) 
print('x:',x)
 
# 设置类型为整数
y = np.zeros((5,), dtype = int) 
print('y:',y)
 
# 自定义类型
z = np.zeros((2,3), dtype = [('x', 'float'), ('y', 'i4')])  
print('z:',z)

输出结果:

x: [0. 0. 0. 0. 0.]
y: [0 0 0 0 0]
z: [[(0., 0) (0., 0) (0., 0)]
 [(0., 0) (0., 0) (0., 0)]]

3.numpy.ones

创建指定形状的数组,数组元素以 1 来填充:

numpy.ones(shape, dtype = None, order = 'C')

演示代码:

# 默认为浮点数
x = np.ones(5) 
print(x)
 
# 自定义类型
x = np.ones((2,3), dtype = [('x', 'float'), ('y', 'i4')])  
print(x)

输出结果:

[1. 1. 1. 1. 1.]
[[(1., 1) (1., 1) (1., 1)]
 [(1., 1) (1., 1) (1., 1)]]

 

二、从已有的数组创建数组

1.numpy.asarray

numpy.asarray 类似 numpy.array,但 numpy.asarray 参数只有三个,比 numpy.array 少两个。

numpy.asarray(a, dtype = None, order = None)

 

参数描述
a 任意形式的输入参数,可以是,列表, 列表的元组, 元组, 元组的元组, 元组的列表,多维数组
dtype 数据类型,可选
order 可选,有"C"和"F"两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序。

 

演示代码:

# 将列表转换为 ndarray
x =  [1,2,3] 
a = np.asarray(x)  
print(a)

# 将元组转换为 ndarray
x = (1,2,3)
a = np.asarray(x)
print(a)

# 将元组列表转换为 ndarray
x = [(1,2,3),(4,5)]
a = np.asarray(x)
print (a)

输出结果:

[1 2 3]
[1 2 3]
[(1, 2, 3) (4, 5)]

 

代码演示:

# 设置 dtype 参数
x =  [1,2,3] 
a = np.asarray(x, dtype =  float)  
print (a)

输出结果:

[1. 2. 3.]

2.numpy.frombuffer

numpy.frombuffer 用于实现动态数组。

numpy.frombuffer 接受 buffer 输入参数,以流的形式读入转化成 ndarray 对象。

numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)

注意:buffer 是字符串的时候,Python3 默认 str 是 Unicode 类型,所以要转成 bytestring 在原 str 前加上 b。

 

参数描述
buffer 可以是任意对象,会以流的形式读入。
dtype 返回数组的数据类型,可选
count 读取的数据数量,默认为-1,读取所有数据。
offset 读取的起始位置,默认为0。

 

演示代码:

s =  b'Hello World' 
a = np.frombuffer(s, dtype =  'S1')  
print (a)

输出结果:

[b'H' b'e' b'l' b'l' b'o' b' ' b'W' b'o' b'r' b'l' b'd']

3.numpy.fromiter

numpy.fromiter 方法从可迭代对象中建立 ndarray 对象,返回一维数组。

numpy.fromiter(iterable, dtype, count=-1)

演示代码:

# 使用 range 函数创建列表对象  
list=range(5)
it=iter(list)
 
# 使用迭代器创建 ndarray 
x=np.fromiter(it, dtype=float)
print(x)

输出结果:

[0. 1. 2. 3. 4.]

 

三、从数值范围创建数组

1.numpy.arange

numpy 包中的使用 arange 函数创建数值范围并返回 ndarray 对象,函数格式如下:

numpy.arange(start, stop, step, dtype)

 

参数描述
start 起始值,默认为0
stop 终止值(不包含)
step 步长,默认为1
dtype 返回ndarray的数据类型,如果没有提供,则会使用输入数据的类型。

 

演示代码:

# 生成 0-5 的数组
x = np.arange(5)  
print (x)

# 设置 dtype、起始值、终止值及步长
x = np.arange(10,20,2,dtype = float)
print (x)

输出结果:

[0  1  2  3  4]
[10. 12. 14. 16. 18.]

2.numpy.linspace

numpy.linspace 函数用于创建一个一维数组,数组是一个等差数列构成的,格式如下:

np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

 

参数描述
start 序列的起始值
stop 序列的终止值,如果endpointtrue,该值包含于数列中
num 要生成的等步长的样本数量,默认为50
endpoint 该值为 true 时,数列中包含stop值,反之不包含,默认是True。
retstep 如果为 True 时,生成的数组中会显示间距,反之不显示。
dtype ndarray 的数据类型

 

演示代码:

# 设置起始点为 1 ,终止点为 10,数列个数为 4
a = np.linspace(1,10,4)
print(a)

# 将 endpoint 设为 false,不包含终止值
a = np.linspace(10, 20, 5, endpoint = False)
print(a)

# 设置间距
a =np.linspace(1,10,10,retstep= True)
print(a)

# 拓展例子
b =np.linspace(1,10,10).reshape([10,1])
print(b)

输出结果:

[ 1.  4.  7. 10.]
[10. 12. 14. 16. 18.]
(array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]), 1.0)
[[ 1.]
 [ 2.]
 [ 3.]
 [ 4.]
 [ 5.]
 [ 6.]
 [ 7.]
 [ 8.]
 [ 9.]
 [10.]]

3.numpy.logspace

numpy.logspace 函数用于创建一个于等比数列。格式如下:

np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)

 

参数描述
start 序列的起始值为:base ** start
stop 序列的终止值为:base ** stop。如果endpointtrue,该值包含于数列中
num 要生成的等步长的样本数量,默认为50
endpoint 该值为 true 时,数列中中包含stop值,反之不包含,默认是True。
base 对数 log 的底数。
dtype ndarray 的数据类型

演示代码:

# 默认底数是 10
a = np.logspace(1.0, 2.0, num = 4)
print (a)

# 将对数的底数设置为 2
a = np.logspace(0,9,num=10,base=2,dtype='int')
print (a)

输出结果:

[ 10.          21.5443469   46.41588834 100.        ]
[  1   2   4   8  16  32  64 128 256 512]

 

标签:10,创建,dtype,print,数组,numpy,np,NumPy
来源: https://www.cnblogs.com/ikunn/p/16534849.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有