四、数组操作

1、修改数组形状

reshape()函数

原型:reshape(shape,order=‘C’)

作用:不改变数据的条件下修改形状

参数描述
shape形状
order‘C’–按行;‘F’–按列;‘A’–按原顺序,‘k’–元素在内存中的出现顺序

flat

数组元素迭代器

import numpy as np

arr = np.arange(1, 100, 5).reshape((4, 5))
print(arr)
# arr.flat相当于np.nditer(order='C')迭代
for element in arr.flat:
    print(element, end=",")

[[ 1 6 11 16 21]
[26 31 36 41 46]
[51 56 61 66 71]
[76 81 86 91 96]]
1,6,11,16,21,26,31,36,41,46,51,56,61,66,71,76,81,86,91,96,

flatten()函数

原型:flatten(order=‘C’)

作用:返回一份数组拷贝,对拷贝所做的修改不会影响原数组

import numpy as np
arr1 = np.arange(12).reshape((3, 4))
print(arr1.flatten(order='F'))
print(arr1)

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

ravel()函数

原型:numpy.ravel(order=‘C’)

作用:展平的数组元素,顺序通常是“C风格“,返回的是数组视图;修改会影响原始数组

此处的影响原始数组:原因在于,新的数组和原始数组公用同一组地址;并且只有在order='C’时才会影响原始数组。

2、翻转数组

转置T和转置函数transpose()

import numpy as np
print(f"使用转置T :\n {arr.T}")
print(f"使用转置方法np.transpose():\n"
      f"{np.transpose(arr)}")
for x in np.nditer(np.transpose(arr)):
    print(x, end=",")
print("")
# 使用copy进行行优先内存排列
b = arr.T.copy(order='C')
for x in np.nditer(np.transpose(b)):
    print(x, end=",")
print("")

使用转置T :
[[ 2 12]
[ 4 14]
[ 6 16]
[ 8 18]
[10 20]]
使用转置方法np.transpose():
[[ 2 12]
[ 4 14]
[ 6 16]
[ 8 18]
[10 20]]
2,4,6,8,10,12,14,16,18,20,
2,12,4,14,6,16,8,18,10,20,

rollaxis()函数

原型:rollaxis(a,axis,start=0)

作用:向后滚动特定的轴到一个特定位置

参数说明
a需要操作的数组
axis要向后滚动的轴,其他轴的相对位置不会改变
start默认为零,表示完整的滚动
会滚动到特定的位置

axis为要变动的轴

start为要交换的轴

import numpy as np
arr1 = np.arange(24).reshape(2, 3, 4)
print(arr1)
print("+++++++++++++++++++++++++++")
b = np.rollaxis(arr1,2,1)
print(b)
print(b.shape)

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

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

[[12 16 20]
[13 17 21]
[14 18 22]
[15 19 23]]]
(2, 4, 3)

swapaxes()函数

原型:swapaxes(a,axis1,axis2)

作用:交换数组的两个轴

参数说明
a要操作的数组
axis1对应第一个轴的整数
axis2对应第二个轴的整数
arr1 = np.arange(24).reshape(2, 3, 4)
print(arr1)
print("+++++++++++++++++++++++++++")
c = np.swapaxes(arr1,1,2)
print(c)
print(c.shape)

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

[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]

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

[[12 16 20]
[13 17 21]
[14 18 22]
[15 19 23]]]
(2, 4, 3)

3、修改数组维度

broadcast()

用于模仿广播的对象,它返回一个对象,该对象封装了将一个数组广播到另一个数据的结果。

x = np.array([[1], [2], [3]])
y = np.array([4, 5, 6])
print("原始x:")
print(x)
print("原始y:")
print(y)

print("对y广播x形成b对象:")
b = np.broadcast(x, y)
print(b)

r, c = b.iters
print(next(r), next(c))
print(b.shape)

原始x:
[[1]
[2]
[3]]
原始y:
[4 5 6]
对y广播x形成b对象:
<numpy.broadcast object at 0x0000013AFEDADCB0>
1 4

(3, 3)

expand_dims()

原型 :numpy.expend_dims(arr,axis=None)

作用:通过在指定位置插入新的轴来扩展数组形状

参数说明
arr输入数组
axis新轴插入的位置

squeeze()

原型:numpy.squeeze(arr,axis)

作用:从给定数组的形状中删除一维的条目

报错:cannot select an axis to squeeze out which has size not equal to one

只有当要挤压的轴的大小为1时才有效,否则会丢失数据

4、连接数组

concatenate()

原型:numpy.concatenate((a1,a2,…),axis)

作用:用于沿指定轴连接相同形状的两个或多个数组

参数说明
a1,a2,…相同类型的数组
axis沿着它连接数组的轴,默认为0
x = np.arange(1, 5).reshape((1, 2, 2))
y = np.arange(5, 9).reshape((1, 2, 2))

print(x[0][1])
print(y[0][1][1])
z = np.concatenate((x, y), axis=2)
print(z)

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

stack()

原型:numpy.stack(arrays,axis)

作用:用于沿新轴连接数组序列;组合后维度(ndim)增加1.

参数说明
arrays相同形状的数组序列
axis数组中的轴,输入数据沿着它来堆叠
x = np.arange(1, 5).reshape((2, 2))
y = np.arange(5, 9).reshape((2, 2))
print(x)
print(y)
print("+++++++ axis = 0 ++++++++")
z = np.stack((x, y), axis=0)
print(z)
print(z.ndim, z.shape)
print("+++++++ axis = 1 ++++++++")
z = np.stack((x, y), axis=1)
print(z)
print(z.ndim, z.shape)

[[1 2]
[3 4]]
[[5 6]
[7 8]]
+++++++ axis = 0 ++++++++
[[[1 2]
[3 4]] //在此处不同

[[5 6]
[7 8]]]
3 (2, 2, 2)
+++++++ axis = 1 ++++++++
[[[1 2]
[5 6]] //在此处不同

[[3 4]
[7 8]]]
3 (2, 2, 2)

hstack()

numpy.stack函数的变体,它通过水平堆叠来生成数组。

即,np.concatenate((x, y), axis=1)相同;axis=1轴的大小相加

vstack()

numpy.stack函数的变体,它通过垂直堆叠来生成数组。

即,np.concatenate((x, y), axis=0)相同;axis=0轴的大小相加

5、分割数组

split()

原型:numpy.split(ary,indices_or_sections,axis=0)

作用:函数沿特定的轴将数组分割为子数组

参数说明
ary被分割的数组
indices_or_sections是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置(左开右闭)
axis沿着哪个维度进行切向,默认为0,横向切分。为1时,纵向切分。
x = np.arange(0, 10).reshape(2, 5)
print(x)
a = np.split(x, 2, axis=0)
print(a)
for i in a:
    print(i.flatten())  # 将二维数组降为一维

[[0 1 2 3 4]
[5 6 7 8 9]]
[array([[0, 1, 2, 3, 4]]), array([[5, 6, 7, 8, 9]])]
[0 1 2 3 4]
[5 6 7 8 9]

hsplit()

用于水平分割数组,通过制定要返回的相同形状的数组数量来拆分元数组。

水平:axis=1;垂直:axis=0;

x = np.arange(0, 12).reshape(2, 6)
print(x)
a = np.split(x, 2, axis=1)
print(a)
b = np.hsplit(x, 2)
print(b)

[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]
[array([[0, 1, 2],
[6, 7, 8]]), array([[ 3, 4, 5],
[ 9, 10, 11]])]
[array([[0, 1, 2],
[6, 7, 8]]), array([[ 3, 4, 5],
[ 9, 10, 11]])]

vsplit()

沿着垂直轴分割,其分割方式与hsplit用法相同

即:np.split(x, 2, axis=0)

6、数组元素的添加与删除

resize()

原型:numpy.resize(arr,shape)

作用:返回指定大小的新数组; 根据内存的顺序来修改

参数说明
arr要修改大小的数组
shape返回数组的新形状
x = np.arange(6).reshape(2, 3)
print(x)

y = np.resize(x, (3, 2))
print(y)

[[0 1 2]
[3 4 5]]
[[0 1]
[2 3]
[4 5]]

append()

原型:numpy.append(arr,values,axis=None)

作用:在数组的末尾添加值;追加操作会分配整个数组,并把原来的数组赋值到新数组中;输入数组的维度必须匹配否则将生成ValueError

参数说明
arr输入数组
values要向arr添加的值,需要和arr形状相同,(除了要添加的轴)
axis默认为None。当axis无定义时,时横向加成,返回总是以为数组!当axis有定义的时候,分别为0和1的时候。为0的时候(列数要相同)。当axis为1时,数组是加在右边(行数需相同)

insert()

原型:numpy.insert(arr,obj,values,axis)

作用:在给定索引之前,沿给定轴在输入数组中插入值

注意:如果值的类型转换为要插入,则它与输入数组不同。插入没有原地的,函数会返回一个新数组。此外,如果未提供轴,则输入数组会被展开。

参数说明
arr输入数组
obj在其之前插入值的索引
values要插入的值
axis沿着它插入的轴,如果没有提供,则输入的数组会被展开
x = np.arange(6).reshape(2, 3)
print(x)
y = np.insert(x, 3, [11, 12])
print(y)
z = np.insert(x, x.shape[0], [13, 21, 43], axis=0)
print(z)

[[0 1 2]
[3 4 5]]
[ 0 1 2 11 12 3 4 5]
[[ 0 1 2]
[ 3 4 5]
[13 21 43]]

delete()

原型:numpy.delete(arr,obj,axis)

作用:返回从输入数组中删除指定子数组的新数组。与insert()函数的情况一样,如果未提供轴参数,则输入数组将展开

参数说明
arr输入数组
obj可以被切片,整数或者整数数组,表面要从输入数组删除的子数组
axis沿着它删除给定子数组的轴,如果未提供,则输入数组会被展开

unique()

原型:numpy.unique(arr,return_index,return_inverse,return_counts)

作用:去除数组中的重复元素

参数说明
arr输入数组,如果不是一维数组则会展开
return_index如果为true,返回新列表元素在旧列表中的位置(下标),并以列表形式存储
return_inverse如果为true,返回旧列表元素在新列表中的位置(下标),并以列形式存储
return_counts如果为true,返回去重数组中的元素在原数组中出现的次数
x = np.arange(6).reshape(2, 3)
x = np.resize(x, (3, 3))
print(x)
# return_counts:返回数值重复次数
y, z = np.unique(x, return_counts=True)
print(y)
print(z)

[[0 1 2]
[3 4 5]
[0 1 2]]
[0 1 2 3 4 5]
[2 2 2 1 1 1]

------ 以上内容均为学习笔记仅供参考,如有不准确或错误内容,望您批评指教。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值