活动地址:CSDN21天学习挑战赛
上一篇地址点这里:数据分析—Numpy(一)
数组属性–轴数
ndarray.ndim(数组的轴数)
什么是轴?
在numpy中可以理解为方向,使用0,1,2数字表示
- 对于1维数组,只有0轴;
- 对于2维数组(shape(2,2))有0轴和1轴;
- 对于3维数组(shape(2,2,3))有0,1,2轴。
数组索引和切片
1、ndarray对象的内容可以通过索引或切片来访问和修改,与 Python 中 list 的索引和切片操作一样
2、ndarray 数组可以基于 0 ~ len(ndarray) - 1的下标进行索引
3、切片对象可以通过内置的 slice 函数,设置 start, stop 及 step 参数,从原数组中切割出一个新数组
4、数组切片的参数设置与列表切片参数类似
5、数组对象[起始位置:终止位置:步长, …] (左闭右开,默认步长为1)
数组高级索引
- 整数数组索引
import numpy as np
a = np.arange(1,13).reshape(3,4)
b = a[[0, 1, 2], [0, 1, 0]]
print(b)
# 获取数组中(0,0),(1,1)和(2,0)位置处的元素
import numpy as np
a = np.arange(1, 13).reshape(3, 4)
rows = np.array([[0, 0], [2, 2]])
cols = np.array([[0, 2], [0, 2]])
b = a[rows, cols]
print(b)
- 布尔索引
布尔索引通过布尔运算(如:比较运算符)来获取符合指定条件的元素的数组
import numpy as np
a = np.arange(1, 13).reshape(3, 4)
b = a[a > 8]
print(b)
- 花式索引
花式索引根据索引数组的值作为目标数组的某个轴的下标来取值
import numpy as np
x=np.arange(32).reshape((8,4))
print (x[[4,2,1,7]])
数组操作
1、数组形状修改
- 就地变维:直接改变原数组对象的维度,不返回新数组
ndarray.shape = ()
ndarray.resize() - 复制变维(数据独立)
ndarray.flatten()(ndarray.tolist()) - 视图变维(数据共享)
ndarray.reshape() 与 np.ravel(a,order=“”)
2、数组数值修改
用索引或切片获取数据后直接赋值修改
import numpy as np
t = np.arange(24).reshape(4,6)
t[1,:]=0 # 修改某一行的值
t[:,1]=0 # 修改某一列的值
t[1:3,:]=0 # 修改连续多行
t[:,1:4]=0 # 修改连续多列
t[1:4,2:5]=0 # 修改多行多列
t[[0,1],[0,3]]=0 # 修改多个不相邻的点
3、添加数组元素
(1)numpy.append(arr, values, axis=None) 将值附加到数组的末尾
- arr:输入数组
- values:要向arr添加的值,需要和arr形状相同(除了要添加的轴)
- axis:默认为 None
- 当axis无定义时,横向拼接,返回一维数组
- 当axis=0,列数要相同,行增加
- 当axis=1,行数要相同,列增加
(2)numpy.insert(arr, obj, values, axis=None)
- arr:输入数组
- obj:在其之前插入值的索引
- values:要插入的值
- axis:轴,如果未提供,则输入数组会被展开
4、删除数组元素
numpy.delete(arr, obj, axis=None)
- arr:输入数组
- obj:要沿指定轴删除的子数组的索引,接收切片,整数或整数数组
- axis:沿着它插入的轴,如果未提供,则输入数组会被展开
5、数组元素去重
numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)
- arr:输入数组,如果不是一维数组则会展开
- return_index:如果为true,返回新列表元素在旧列表中的位置(下标),并以列表形式储
- return_inverse:如果为true,返回旧列表元素在新列表中的位置(下标),并以列表形式储
- return_counts:如果为true,返回去重数组中的元素在原数组中的出现次数
- axis:轴,如果为None,则ar将被展平
import numpy as np
array = np.array([5,2,6,2,7,5,6,8,2,9])
u,index,counts=np.unique(array,return_index=True,return_counts=True)
print(u,'\n',index,'\n',counts)
6、裁剪数组元素
numpy.clip(a, a_min, a_max, out = None, ** kwargs)
裁剪(限制)数组中的值
- a:要裁剪元素的数组
- a_min:最小值。如果为“无”,则不会在较低间隔的边缘进行裁剪
- a_max:最大值。如果为“无”,则不会在间隔的上限边缘执行剪裁
- out:结果将放置在此数组中
import numpy as np
array = np.arange(24).reshape((4,6))
np.clip(array,10,18,array)
print(array)
7、数组行列交换
利用python交换赋值原理(x, y = y, x)
- 行交换
例如:t[[1, 2], :] = t[[2, 1], :] - 列交换
例如:t[: ,[0, 2]] = t[: ,[2, 0]]
8、数组转置
array.T
9、连接数组
- numpy.concatenate((a1, a2, …), axis) 按照axis轴向连接数组
a1, a2, …:相同类型的数组
axis:轴向,默认为 0
- numpy.column_stack(*args, **kwargs)
一维或二维数组按照列合并成二维数组
- numpy.row_stack(*args, **kwargs)
一维或二维数组按照行合并成二维数组
- numpy.stack(arrays, axis = 0, out = None)
沿新轴连接一系列数组
- numpy.hstack((a1,a2))
水平堆叠生成数组
- numpy.vstack((a1,a2))
垂直堆叠生成数组
- numpy.dstack((a1,a2))
深度堆叠生成数组(3维)
- numpy.pad(array, pad_width, mode=‘constant’, **kwargs)
长度不等的数组可以通过np.pad()函数填充长度
10、分割数组
numpy.split(ary, indices_or_sections, axis=0)
将数组拆分为多个子数组,作为ary的视图
numpy.hsplit(ary,in dexs_or_sections)
水平(按列)将一个数组拆分为多个子数组
numpy.vsplit(ary,indexs_or_sections)
垂直(行)将一个数组拆分为多个子数组
numpy.dsplit(ary,indexs_or_sections)
沿第3轴(深度)将数组拆分为多个子数组