Numpy数组的创建

NumPy(Numerical Python) 是科学计算基础库,提供大量科学计算相关功能,比如数据 统计,随机数生成等。其提供最核心类型为多维数组类型(ndarray),支持大量的维度数组 与矩阵运算,Numpy 支持向量处理 ndarray 对象,提高程序运算速度。

数组的创建

array 创建 numpy 模块的 array 函数可以生成多维数组。例如,如果要生成一个二维数组,需要向 array 函数传递一个列表类型的参数。每一个列表元素是一维的 ndarray 类型数组,作为二维 数组的行。另外,通过 ndarray 类的 shape 属性可以获得数组每一维的元素个数(元组形式), 也可以通过 shape[n]形式获得每一维的元素个数,其中 n 是维度,从 0 开始。
语法格式如下:

numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)

array 参数说明

描述说明
object数组或嵌套的数列
dtype数组元素的数据类型,可选
copy对象是否需要复制,可选
order创建数组的样式,C 为行方向,F 为列方向,A 为任意方向(默 认)
subok默认返回一个与基类类型一致的数组
ndmin指定生成数组的最小维度

创建一维

import numpy as np
b = np.array([1,2,3,4,5,6])
print(b)
print('b数组的维度:',b.shape)

运行结果为:

[1 2 3 4 5 6]
b数组的维度: (6,)

创建二维

import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(a)
print("a数组的维度:",a.shape)

运行结果为:

[[1 2 3]
 [4 5 6]
 [7 8 9]]
a数组的维度: (3, 3)

ndmin 参数的使用

import numpy as np
a = np.array([1,2,3,4,5,6],ndmin=4)
print(a)

运行结果为:

[[[[1 2 3 4 5 6]]]]

dtype 参数的使用

import numpy as np
a = np.array([1,2,3],dtype=complex)
print(a)

运行结果为:

[1.+0.j 2.+0.j 3.+0.j]

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

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

arange 参数说明

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

arange 生成 0 到 5 的数组

import numpy as np
x = np.arange(0,6,dtype=int)
print(x)

运行结果为:

[0 1 2 3 4 5]

arange 设置了起始值、终止值及步长

import numpy as np
x = np.arange(10,20,2,dtype=float)
print(x)

运行结果为:

[10. 12. 14. 16. 18.]

arange 创建二维数组

import numpy as np
b = np.array([np.arange(1,4),np.arange(4,7),np.arange(7,10)])
print(b)
print("b数组的维度是:",b.shape)

运行结果为:

[[1 2 3]
 [4 5 6]
 [7 8 9]]
b数组的维度是: (3, 3)

随机数创建

numpy.random.random(size=None) 该方法返回[0.0, 1.0)范围的随机数。

numpy.random.random(size=None)的使用

import numpy as np
x = np.random.random(size=4)
y = np.random.random(size=(3,4))
print(x)
print("------------------------------------")
print(y)

运行结果为:

[0.39991409 0.27915768 0.53261798 0.37074104]
------------------------------------
[[0.43350394 0.15846519 0.31325405 0.97243969]
 [0.58468269 0.65362944 0.30493845 0.8195611 ]
 [0.93817092 0.84034127 0.66913598 0.17711244]]

numpy.random.randint()
该方法有三个参数 low、high、size 三个参数。默认 high 是 None,如果只有 low,那 范围就是[0,low)。如果有 high,范围就是[low,high)。
numpy.random.randint()的使用

import numpy as np
# numpy.random.randint()的使用
# #生成 [0,low)范围的随机整数
x = np.random.randint(5, size=10)
print(x)


# 生成[low,high)范围的随机整数
y = np.random.randint(5,10,size=10)
print(y)

# 生成[low,high)范围的 2*4 的随机整数
z = np.random.randint(5,10,size=(2,4))
print(z)

运行结果为:

[1 1 1 0 0 2 3 4 3 3]
[6 8 8 6 7 5 5 7 9 5]
[[9 5 6 9]
 [7 9 9 7]]

numpy.random.randn(d0,d1,…,dn)
randn 函数返回一个或一组样本,具有标准正态分布(期望为 0,方差为 1)。 dn 表格每个维度 返回值为指定维度的 array

# randn 函数返回一个或一组样本,具有标准正态分布
import numpy as np
x = np.random.randn()
print(x)
y = np.random.randn(2,4)
print(y)
z = np.random.randn(2,3,4)
print(z)

运行结果为:

0.959465153260656
[[-0.76539282 -0.87632674  0.06145131 -0.06147865]
 [-0.55543007 -0.13134546 -1.4261319   0.17214542]]
 [[[ 0.4146544   0.08115705 -1.16205841 -0.29338812]
  [ 0.08396677 -1.01104743 -1.08320293 -0.42825476]
  [-0.24699154  0.19283108  1.28023375 -0.90880636]]
  [[-0.61497592 -0.16630262  0.98212104  0.16648512]
  [-0.29560375  1.80980884  0.4643258  -0.08994346]
   [ 0.51603959  0.60209728 -0.36441993 -0.64000876]]]

np.random.normal 指定期望和方差的正太分布
正太分布(高斯分布)loc:期望 scale:方差 size 形状 print(np.random.normal(loc=3,scale=4,size=(2,2,3)))

import numpy as np
print(np.random.normal(loc=3,scale=4,size=(2,2,3)))

运行结果为:

[[[ 4.63209718 10.47901373  3.4205948 ]
[ 1.40387603  1.9902945  -2.82863523]]
[[ 7.47168385  2.35654862  3.27221657]
[ 5.65582039  5.44416044  2.75192624]]]

ndarray 对象

NumPy 最重要的一个特点是其 N 维数组对象 ndarray,它是一系列同类型数据的集 合,以 0 下标为开始进行集合中元素的索引。 ndarray 对象是用于存放同类型元素的多维数组。 ndarray 中的每个元素在内存中都有相同存储大小的区域。 ndarray 内部由以下内容组成:

  • □ 一个指向数据(内存或内存映射文件中的一块数据)的指针。
  • □ 数据类型或 dtype,描述在数组中的固定大小值的格子。
  • □ 一个表示数组形状(shape)的元组,表示各维度大小的元组。
    NumPy 的数组中比较重要 ndarray 对象属性有:
属性说明
ndarray.ndim秩,即轴的数量或维度的数量
ndarray.shape数组的维度,对于矩阵,n 行 m 列
ndarray.size数组元素的总个数,相当于 .shape 中 n*m 的值
ndarray.dtypendarray 对象的元素类型
ndarray.itemsizendarray 对象中每个元素的大小,以字节为单位
ndarray.flagsndarray 对象的内存信息
ndarray.realndarray 元素的实部
ndarray.imagndarray 元素的虚部
ndarray.data包含实际数组元素的缓冲区,由于一般通过数组的索引获取元素,所以通常不需要使用这个属性。

ndarray 属性测试

import numpy as np
x1 = np.random.randint(10, size=6)
print(x1)
x2 = np.random.randint(10, size=(3, 4))
print(x2)
x3 = np.random.randn(3, 4, 5)
print("ndim属性".center(20, "*"))
print("ndim:", x1.ndim, x2.ndim, x3.ndim)
print("shape属性".center(20, "*"))
print("shape:", x1.shape, x2.shape, x3.shape)
print("dtype属性".center(20, "*"))
print("dtype:", x1.dtype, x2.dtype, x3.dtype)
print("size属性".center(20, "*"))
print("size:", x1.size, x2.size, x3.size)
print("itemsize属性".center(20, "*"))  # 一个字节是8位
print("itemsize", x1.itemsize, x2.itemsize, x3.itemsize)

运行结果为:

[7 2 6 6 2 8]
[[6 6 5 0]
 [4 6 3 7]
 [2 2 7 7]]
*******ndim属性*******
ndim: 1 2 3
******shape属性*******
shape: (6,) (3, 4) (3, 4, 5)
******dtype属性*******
dtype: int32 int32 float64
*******size属性*******
size: 6 12 60
*****itemsize属性*****
itemsize 4 4 8

其他方式创建

ndarray 数组除了可以使用底层 ndarray 构造器来创建外,也可以通过以下几种方式来创 建。
zeros 创建指定大小的数组,数组元素以 0 来填充:

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

zeros 创建

import numpy as np
x = np.zeros(5)
print(x)
#设置类型位整数
y = np.zeros((5,),dtype=int)
print(y)
z = np.zeros((2,2))
print(z)

运行结果如下:

[0. 0. 0. 0. 0.]
[0 0 0 0 0]
[[0. 0.]
 [0. 0.]]

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

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

ones 创建

import numpy as np
x = np.ones(5)
print(x)
y = np.ones((3,4),dtype=int)
print(y)

运行结果如下:

[1. 1. 1. 1. 1.]
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]

numpy.empty 方法用来创建一个指定形状(shape)、数据类型(dtype)且未初始化的 数组,里面的元素的值是之前内存的值:

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

empty 创建

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

运行结果如下:

[[6619222 7536754]
 [7274601     110]
 [6553673       0]]

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

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

linspace 创建等差数列

import numpy as np
x = np.linspace(1,10,10)
print(x)

运行结果如下:

[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]

linspace 创建等差数列 endpoint 设为 true

import numpy as np
x = np.linspace(10,20,5,endpoint=True)
print(x)

运行结果如下:

[10.  12.5 15.  17.5 20. ]

linspace 创建等差数列 endpoint 设为 true,retstep=True

import numpy as np
x = np.linspace(10,20,5,endpoint=True,retstep=True)
print(x)

运行结果如下:

(array([10. , 12.5, 15. , 17.5, 20. ]), 2.5)

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该值为 ture 时,数列中中包含 stop 值,反之不包含,默认是 True。
base对数 log 的底数。
dtypendarray 的数据类型

logspace 创建等比数列

import numpy as np
x = np.logspace(0,9,10,base=2)
print(x)

运行结果如下:

[  1.   2.   4.   8.  16.  32.  64. 128. 256. 512.]

eye()

import numpy as np
print(np.eye(5))
# 创建一个正方的N*N的单位矩阵,对角线值为1,其余为0

运行结果如下:

[[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.]]

ndarray的数据类型

  • bool 用一个字节存储的布尔类型(True或False)
  • inti 由所在平台决定其大小的整数(一般为int32或int64)
  • int8 一个字节大小,-128 至 127
  • int16 整数,-32768 至 32767
  • int32 整数,-2 ** 31 至 2 ** 32 -1
  • int64 整数,-2 ** 63 至 2 ** 63 - 1
  • uint8 无符号整数,0 至 255
  • uint16 无符号整数,0 至 65535
  • uint32 无符号整数,0 至 2 ** 32 - 1
  • uint64 无符号整数,0 至 2 ** 64 - 1
  • float16 半精度浮点数:16位,正负号1位,指数5位,精度10位
  • float32 单精度浮点数:32位,正负号1位,指数8位,精度23位
  • float64或float 双精度浮点数:64位,正负号1位,指数11位,精度52位
  • complex64 复数,分别用两个32位浮点数表示实部和虚部
  • complex128或complex 复数,分别用两个64位浮点数表示实部和虚部
别废话,拿你代码给我看。
原文地址:https://www.cnblogs.com/lvxueyang/p/13707514.html