2-02-2 NumPy 创建 ndarray 对象

NumPy 创建 ndarray

  1. numpy.array() 方法用来创建一个指定对象的 ndarray 数组。

语法与参数
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)

名称描述
object数组或嵌套的数列
dtype数组元素的数据类型,可选
copy对象是否需要复制,可选
order有 “C” 和 “F” 两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序。
subok默认返回一个与基类类型一致的数组
ndmin指定生成数组的最小维度

实例

import numpy as np 
a = np.array([[1, 2],  [3, 4], [5, 6]])  
print (a)
b=a.reshape((2,3)) # 重新定义数组形状
print (b)
  
# 输出结果如下:
[[1 2]
 [3 4]
 [5 6]]
 [[1 2 3]
 [4 5 6]]
  1. numpy.empty() 方法用来创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组。

语法与参数
numpy.empty(shape, dtype = float, order = ‘C’)

参数描述
shape数组形状,可以用 list 或是 tuple 表示
dtype数据类型,可选
order有 “C” 和 “F” 两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序。

实例

import numpy as np 
a = np.empty((3,2), dtype = int) 
print (a)
  
输出结果为:
[[   4063294    2097214]
 [         0        739]
 [1420215088        739]]
  1. numpy.zeros() 创建指定大小的数组,数组元素以 0 来填充。

语法与参数
numpy.zeros(shape, dtype = float, order = ‘C’)

参数描述
shape数组形状,可以用 list 或是 tuple 表示
dtype数据类型,可选
order有 “C” 和 “F” 两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序。

实例

import numpy as np
# 默认为浮点数
x = np.zeros(5) 
print(x)
  
# 设置类型为整数
y = np.zeros((5,), dtype = np.int8) 
print(y)
  
输出结果为:
  
[0. 0. 0. 0. 0.]
[0 0 0 0 0]
  1. numpy.ones() 创建指定形状的数组,数组元素以 1 来填充。

语法与参数
numpy.ones(shape, dtype = None, order = ‘C’)

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

实例

import numpy as np
# 默认为浮点数
x = np.ones(5) 
print(x)
  
# 自定义类型
x = np.ones([2,2], dtype = int)
print(x)
  
输出结果为:
[1. 1. 1. 1. 1.]
[[1 1]
 [1 1]]

NumPy 从已有的数组创建数组

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

语法与参数
numpy.asarray(a, dtype = None, order = None)

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

实例

import numpy as np 
# 将元组列表转换为 ndarray:
x =  [(1,2,3),(4,5)] 
a = np.asarray(x,dtype=object) 
print (a)
  
输出结果为:
[(1, 2, 3) (4, 5)]
  
  1. numpy.frombuffer() 用于实现动态数组,接受 buffer 输入参数,以流的形式读入转化成 ndarray 对象。

语法与参数
numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)

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

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

实例

import numpy as np 
  
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']
  
  1. numpy.fromiter() 方法从可迭代对象中建立 ndarray 对象,返回一维数组。

语法与参数
numpy.fromiter(iterable, dtype, count=-1)

参数描述
iterable可迭代对象
dtype返回数组的数据类型
count读取的数据数量,默认为-1,读取所有数据

实例

import numpy as np 
  
# 使用 range 函数创建列表对象  
it=iter(range(5))
  
# 使用迭代器创建 ndarray 
a=np.fromiter(it, dtype=float)
print(a)
输出结果为:
  
[0. 1. 2. 3. 4.]

NumPy 从数值范围创建数组
这一章节我们将学习如何从数值范围创建数组。

  1. numpy.arange() 创建数值范围并返回 ndarray 对象。

语法与参数
numpy.arange(start, stop, step, dtype)

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

实例

import numpy as np
x = np.arange(10,20,2)  
print (x)
  
输出结果如下:
[10  12  14  16  18]
  1. 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 时,生成的数组中会显示间距,反之不显示。
dtypendarray 的数据类型

实例

import numpy as np
a = np.linspace(10, 20,  5, endpoint =  False)  
print(a)
  
输出结果为:
[10. 12. 14. 16. 18.]
  
  1. numpy.logspace() 创建一个等比数列。

语法与参数
np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)

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

实例

import numpy as np
# 将对数的底数设置为 2 :
a = np.logspace(0,9,10,base=2)
print (a)
  
输出如下:
[  1.   2.   4.   8.  16.  32.  64. 128. 256. 512.]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值