numpy库

 ndarray对象的元素类型

数组的创建

直接创建

 

 当元组和列表个数相同,可以混合使用。这里用列表类型来讲解。利用numpy中的array函数直接创建,将列表输入进去

# 导入numpy库
import numpy as np

# 利用array创建一维数组
a = np.array([1, 2, 3, 4])
print(a)
#输出a的类型
print(type(a))

输出为:

[1 2 3 4]
<class 'numpy.ndarray'>

这里可以看出来,a是ndarray类型,不再是列表了。

同样可以直接创建二维和三维数组:

# 使用array创建二维数组
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(b)
print(type(b))

# 使用array创建三维数组
c = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
              [[10, 11, 12], [13, 14, 15], [16, 17, 18]]
              ])
print(c)
print(type(c))

在可以通过看第一个元素左边有几个中括号[,就是几维的。直接创建三维数组可能会括号打错发生混乱,建议换行输入。

输出为:

[[1 2 3]
 [4 5 6]
 [7 8 9]]
<class 'numpy.ndarray'>
[[[ 1  2  3]
  [ 4  5  6]
  [ 7  8  9]]

 [[10 11 12]
  [13 14 15]
  [16 17 18]]]
<class 'numpy.ndarray'>

b是一个3x3的二维数组也是(3,3),c是三维数组,表示这两个3x3的二维数组,(2,3,3)。

arange创建

np.arange(start,end,step),前两个参数是指定起点和终点,但是这是一个左闭右开的区间,step是间隔步长.起点默认为0,步长默认为1

import numpy
# 使用arange创建1:10的数组

a = np.arange(1, 11)
print(a)

输出为:

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

设置步长为2:

# 设置step
b = np.arange(1, 11, 2)
print(b)

输出为:

[1 3 5 7 9]

随机数创建数组

random(size),通过size创建不同维度且元素值都在(0,1)之间的数组。

# 使用random创建一维数组[0.0,1.0]
a = np.random.random(size=5)
print(a)

# 创建二维数组
b = np.random.random(size=(3, 4))
print(b)

# 创建三维数组
c = np.random.random(size=(2, 3, 4))
print(c)

输出为:

[0.81295005 0.26211644 0.0983631  0.76441132 0.52666839]
[[0.53753575 0.56707459 0.54967527 0.37409221]
 [0.14947433 0.61221604 0.23232046 0.86818498]
 [0.77168242 0.95812368 0.73724719 0.32415273]]
[[[0.65198857 0.85857002 0.11935117 0.64245054]
  [0.21085836 0.44841967 0.98408752 0.05181571]
  [0.16452253 0.79229204 0.60388721 0.92880163]]
 [[0.18512962 0.38950101 0.69914804 0.97384099]
  [0.1796985  0.11925715 0.46381496 0.75086011]
  [0.43043482 0.92820325 0.06414176 0.0913117 ]]]

创建随机整数数组

方法randint(start, end,size),同样可通过size来控制生成数组的维度。

#生成0-5之间的一维随机整数数组
a = np.random.randint(0, 6, size=10)
print(a)
[1 1 4 4 5 4 3 2 0 2]

创建正态分布的随机数组

标准正态分布:期望为0,方差为1。

可以利用方法.randn(size)来创建标准正态分布的随机数组

a = np.random.randn(4)
print(a)
[-1.66912917 -1.18672404 -0.08829372 -0.5349922 ]
 b = np.random.randn(2, 3)
 print(b)
[[ 2.48638924 -0.39313099 -0.33734043]
 [-0.54246475  0.18858891 -0.52679328]]
c = np.random.randn(2, 3, 4)
print(c)
[[[ 0.84255903 -1.10581233 -0.31836397 -0.61911284]
  [ 0.299983    1.29965526  0.21037945 -0.56917515]
  [-1.38356535 -1.45358747  1.50504868 -0.29309671]]
 [[ 0.44626718  1.50504638 -1.24621047  0.89565254]
  [ 0.86175815  0.45952569  0.45326632  1.80312732]
  [ 0.53960571  0.62890115 -0.08663609 -0.37772961]]]

 利用方法.normal(loc, scale, size)创建任意期望与方差的数组

a = np.random.normal(size=5)  #默认期望loc=0  方差scale=1.0
print(a)

b = np.random.normal(loc=2, scale=3, size=(3, 4))
print(b)
[ 1.22236925 -1.15141137 -0.44629148  0.08207421 -0.46415118]
[[ 3.33540207  0.74668393  4.48647267 -1.77196322]
 [ 5.99010619  3.937793    0.63242066  1.56456969]
 [ 4.6726774   1.21674365  7.15237791 10.20999035]]

其他的创建方法

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

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

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

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

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

x = np.ones(5)
print(x)

y = np.ones((2, 5), dtype=int)
print(y)
[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')

等差数列

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

numpy.linspace(start, stop,num=50,endpoint=True,restep=False,dtype=None )】

endpoint = True时,右边是闭区间,是包含终止值的。

# 生成一个1到10的且包含十个元素的等差数组
a = np.linspace(1, 10, 10)
print(a)
[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]

 等比数列

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

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

b = np.logspace(0, 9, 4, base=2)
print(b)

生成了一个起始值是2的0次方,终止值是2的9次方,四个元素的等比数列,输出结果如下:
 

[  1.   8.  64. 512.]

ndarray对象的属性

ndarray.ndim秩,即轴的数量和维度的数量 
ndarray.shapendarray对象的尺度,对于矩阵,n行m列
ndarray.sizendarray对象元素的个数,相当于n*m的值
ndarray.dtypendarray对象的元素类型
ndarray.itemsizendarray对象中元素的大小,以字节为单位

ndarray.real

元素的实部

ndarray.imag

元素的虚部

首先创建三个不同维度的数组a、b、c,将数组打印出来

import numpy as np

a = np.array([1, 2, 3])
print(a)

b = np.random.randint(4, 10, size=(2, 3))
print(b)

c = np.random.randn(2, 3, 4)
print(c)

[1 2 3]

[[6 8 4]
 [4 6 5]]

[[[ 1.55973016e-01 -2.79099851e-01  1.36327429e+00  2.01011931e+00]
  [-4.94497630e-01  2.04447439e-02 -5.43406659e-01  1.59665805e+00]
  [-4.35071785e-01 -6.79714311e-01 -1.31922110e+00 -1.36713460e-01]]
 [[-1.79907593e+00  5.15248489e-01 -1.27974894e+00 -1.41908972e+00]
  [ 5.71216827e-01  1.64451203e-03  1.09834920e+00 -1.63715773e+00]

查看每个数组的属性 

# ndim属性:数组的维度
print(f"a's ndim:{a.ndim}\tb's ndim:{b.ndim}\tc's ndim:{c.ndim}")

# shape属性
print(f"a's shape:{a.shape}\tb's shape:{b.shape}\tc's shape:{c.shape}")

# dtype属性:当前元素的类型
print(f"a's dtype:{a.dtype}\tb's dtype:{b.dtype}\tc's dtype:{c.dtype}")

# size:元素的总个数
print(f"a's size:{a.size}\tb's size:{b.size}\tc's size:{c.size}")
a's ndim:1	b's ndim:2	c's ndim:3
a's shape:(3,)	b's shape:(2, 3)	c's shape:(2, 3, 4)
a's dtype:int32	b's dtype:int32	c's dtype:float64
a's size:3	b's size:6	c's size:24

一维数组的切片和索引

与列表一样,数组也可以切片或索引进行修改。

正索引

# 创建一维数组
a = np.arange(10)
print(a)
[0 1 2 3 4 5 6 7 8 9]
# 索引访问,0开始
print('索引0处元素:', a[0])
print("索引5出元素", a[5])

 第一个元素索引为0开始。我们输出第一个和第六个元素:

索引0处元素: 0
索引5出元素 5

负索引

 最后一个元素索引为-1开始,我们输出最后一个和倒数第三个的元素:

# 负索引
print('访问最后一个元素:', a[-1])
print('访问倒数第三个元素:', a[-3])
访问最后一个元素: 9
访问倒数第三个元素: 7

切片正向索引操作

有三个参数,start,stop,step,以冒号:分隔,如[start:stop:step]

# 从头到尾输出a
print(a[:])
[0 1 2 3 4 5 6 7 8 9]
# 从第1个索引到末尾
print(a[1:])
[1 2 3 4 5 6 7 8 9]
# 从索引1到索引2,因为右边是开区间
print(a[1:3])
[1 2]
# 从第1个索引始到第5个索引,间隔两个
print(a[1:6:2])
[1 3 5]

 切片反向索引操作

# 从最后一个开始反向输出
print(a[::-1])
[9 8 7 6 5 4 3 2 1 0]
print(a[-5:-2])
[5 6 7]

二维数组的切片和索引

索引

 利用arange生成1,12的一维数组,利用reshape函数转换为4行3列的二维数组给变量a。

x = np.arange(1, 13)
a = x.reshape(3, 4)
print('二维数组a:\n', a)
二维数组a:
 [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
# 获取第三行
print('第三行元素为:', a[2])

# 获取第3行第2列
print('第3行第2列元素为:', a[2, 2])
第三行元素为: [ 9 10 11 12]
第3行第2列元素为: 11

切片

# 切片的使用 [行切片,列切片]
# 获取所有行所有列
print(a[:, :])
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
# 获取所有行部分列
# 获取所有行第2列
print(a[:, 1])

第二列就是2,6,10这个三个元素。 

[ 2  6 10]

 获取所有行的第1列和第2列

# 获取所有行的第1列和第2列
print(a[:, 0:2])
[[ 1  2]
 [ 5  6]
 [ 9 10]]

使用坐标获取数组[x,y]

a=[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
# 获取第3行第2列
print(a[2][1])  # 先获取行后获取列
print(a[2, 1])

 第3行第2的元素是10

10
10

同时获取第3行第2列和第2行第3列

# 同时获取第3行第2列和第2行第3列
print(a[2][1], a[1][2]) # 返回的是值
print(np.array([a[2][1], a[1][2]])) #此时才是返回数组
print(a[(2, 1), (1, 2)]) #直接返回数组

 第3行第2列是10,第2行第3列是7 

10 7
[10  7]
[10  7]

 数组的复制

import numpy as np
a = np.arange(1, 13).reshape(3, 4)
print(a)

# 对a数组进行切片处理,获取1,2行 1,2列
sub_a = a[:2, :2]
print(sub_a)

# 对sub_a中第1行第1列的值进行修改
sub_a[0][0] = 100
print(sub_a)
print(a)

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

[[1 2]
 [5 6]]

[[100   2]
 [  5   6]]

[[100   2   3   4]
 [  5   6   7   8]
 [  9  10  11  12]]

我们发现修改了sub_a中的元素值也会导致原来的数组a的值的变化,因为通过切片获得的新数组即使赋值给了其他变量,也还是原来的数组,会影响到原来的数组。

可以使用copy方法可以解决上述问题

sub_b = np.copy(a[:2, :2])

sub_b[0][0] = 100
print(sub_b)
print(a)
[[100   2]
 [  5   6]]

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

我们发现使用copy方法将切片数组赋值给新变量,修改新数组不会影响到原来的数组。

数组维度的修改

 reshape()将一维度转换为多维

首先生成一个一维数组a:

a = np.arange(1, 25)
print(a)
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]
 [17 18 19 20 21 22 23 24]]

reshape将一维转换为二维数组

可以在里面直接传入行,列的参数,也可以以元组(行,列)的形式传入,我们分别用两种情况生成4行6列与3行8列的二维数组

b = a.reshape(4, 6)
print(b)
b = a.reshape((3, 8))
print(b)

 输出如下

[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]
 [13 14 15 16 17 18]
 [19 20 21 22 23 24]]

[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]
 [17 18 19 20 21 22 23 24]]

 reshape将一维转换为三维数组

与上述一样,可以直接输入,也可以以元组形式输入。

c = a.reshape((2, 3, 4))
print(c)
[[[ 1  2  3  4]
  [ 5  6  7  8]
  [ 9 10 11 12]]
 [[13 14 15 16]
  [17 18 19 20]
  [21 22 23 24]]]

通过 np.reshape(object对象,元组())方法也可以修改,但是想要转变的维度必须通过元组形式传入:

b = np.reshape(a, (8, 3))
print(b)

c = np.reshape(a, (3, 2, 4))
print(c)

 输出为:

[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]
 [13 14 15]
 [16 17 18]
 [19 20 21]
 [22 23 24]]


[[[ 1  2  3  4]
  [ 5  6  7  8]]
 [[ 9 10 11 12]
  [13 14 15 16]]
 [[17 18 19 20]
  [21 22 23 24]]]

reshape将多维转换为一维

# 将多维数组修改为一维
a = b.reshape(24)
print(a)
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]

 还可以使用reshape(-1)将其转化为1维,不用指定元素总个数。

其他方法

ravel()与flatten()方法都可以降维处理。

# ravel()
a = b.ravel()
print(a)

# flatten
a = b.flatten()
print(a)
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]

 数组的拼接

 数组的水平组合hstack()

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[10, 11, 12], [13, 14, 15]])
print(a)
print(b)
[[1 2 3]
 [4 5 6]]

[[10 11 12]
 [13 14 15]]
# 水平拼接
r = np.hstack((a, b))
print(r)
[[ 1  2  3 10 11 12]
 [ 4  5  6 13 14 15]]

垂直组合vstack()

# 垂直拼接
r = np.vstack((a, b))
print(r)
[[ 1  2  3]
 [ 4  5  6]
 [10 11 12]
 [13 14 15]]

沿着轴拼接concatenate()

print('默认axis=0: 相当于vstack')
# 二维只有两个轴 axis=0, axis=1
r1 = np.concatenate((a, b), axis=0)
print(r1)

print('axis=1:相当于hstack')
r2 = np.concatenate((a, b), axis=1)
print(r2)
默认axis=0: 相当于vstack
[[ 1  2  3]
 [ 4  5  6]
 [10 11 12]
 [13 14 15]]
axis=1:相当于hstack
[[ 1  2  3 10 11 12]
 [ 4  5  6 13 14 15]]

创建两个三维数组,其形状是(1,2,6) 

# 三维数组有三个轴
a = np.arange(1, 13).reshape((1, 2, 6))
print(a, a.shape)
b = np.arange(101, 113).reshape((1, 2, 6))
print(b, b.shape)
[[[ 1  2  3  4  5  6]
  [ 7  8  9 10 11 12]]] (1, 2, 6)
[[[101 102 103 104 105 106]
  [107 108 109 110 111 112]]] (1, 2, 6)

 分别从三个轴进行拼接:

print('三维axis=0:')
r1 = np.concatenate((a, b), axis=0)
print(r1, r1.shape)

print('三维axis=1:')
r2 = np.concatenate((a, b), axis=1)
print(r2, r2.shape)

print('三维axis=2:')
r3 = np.concatenate((a, b), axis=2)
print(r3, r3.shape)

 对形状都是(1,2,6)的a,b进行axis=0轴的拼接的数组形状是(1+1,2,6),对axis=1轴的拼接数组形状是(1,2+2,6),对axis=2轴的拼接数组形状是(1,2,6+6)

三维axis=0:
[[[  1   2   3   4   5   6]
  [  7   8   9  10  11  12]]
 [[101 102 103 104 105 106]
  [107 108 109 110 111 112]]] (2, 2, 6)
三维axis=1:
[[[  1   2   3   4   5   6]
  [  7   8   9  10  11  12]
  [101 102 103 104 105 106]
  [107 108 109 110 111 112]]] (1, 4, 6)
三维axis=2:
[[[  1   2   3   4   5   6 101 102 103 104 105 106]
  [  7   8   9  10  11  12 107 108 109 110 111 112]]] (1, 2, 12)

数组的分割

numpy.split()沿着特定的轴分割数组

numpy.split(ary, indices_or_sections, axis)

ary:被分割的数组

indices_or_sections:如果是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置。

axis: 沿着哪个维度进行切向,默认为0,横向切分。为1时,纵向切分。

split分割一维数组

a = np.arange(1, 13)
print("传递整数  平均分割")
r = np.split(a, 4,axis=0)
print(r)

print("传递数组  按位置分割")
r = np.split(a, [4, 6])
print(r)
传递整数  平均分割
[array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9]), array([10, 11, 12])]
传递数组  按位置分割
[array([1, 2, 3, 4]), array([5, 6]), array([ 7,  8,  9, 10, 11, 12])]

split分割二维数组

# split分割二维数组
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15 ,16]])
print(a)
print("axis=0 垂直方向 平均分割")
r, w = np.split(a, 2, axis=0)
print(r)
print(w)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]
axis=0 垂直方向 平均分割
[[1 2 3 4]
 [5 6 7 8]]
[[ 9 10 11 12]
 [13 14 15 16]]
print("axis=0 垂直方向 位置分割")
r, w, k = np.split(a, [2, 3], axis=0)
print(r)
print(w)
print(k)
axis=0 垂直方向 位置分割
[[1 2 3 4]
 [5 6 7 8]]

[[ 9 10 11 12]]

[[13 14 15 16]]
print("axis=1 水平方向 平均分割")
r, w = np.split(a, 2, axis=1)
print(r)
print(w)
[[ 1  2]
 [ 5  6]
 [ 9 10]
 [13 14]]

[[ 3  4]
 [ 7  8]
 [11 12]
 [15 16]]
print("axis=1 水平方向 位置分割")
r, w, k = np.split(a, [2, 3], axis=1)
print(r)
print(w)
print(k)
axis=1 水平方向 位置分割
[[ 1  2]
 [ 5  6]
 [ 9 10]
 [13 14]]

[[ 3]
 [ 7]
 [11]
 [15]]

[[ 4]
 [ 8]
 [12]
 [16]]

python中还有hsplit()与vsplit(),不需要传入axis参数,同样可以通过传入整数或者数组实现整数分割或者位置分割。

水平分割hsplit()

# 使用hsplit()
r, w = np.hsplit(a, 2)
print(r)
print(w)
[[ 1  2]
 [ 5  6]
 [ 9 10]
 [13 14]]
[[ 3  4]
 [ 7  8]
 [11 12]
 [15 16]]

垂直分割vsplit()

r, w = np.vsplit(a, 2)
print(r)
print(w)
[[1 2 3 4]
 [5 6 7 8]]
[[ 9 10 11 12]
 [13 14 15 16]]

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值