NumPy_数据处理详解—矩阵运算-矩阵拼接

发布时间 2023-05-24 15:43:11作者: 辰令

基础内容

  坐标轴 axis
  维度   ndim
  和形状   shape 以及元素各个轴元素 的个数 
  索引--单个元素  切片--多个元素[start:end:step]不包括终点的值
                  当start是0时,可以省略;当end是列表的长度时,可以省略.
 				 trans_matrix[:3,:3]
 				 trans_matrix[0:3,3]
 	
 	 元组中只有一个元素时,元素后面需要加逗号(1,)
 	 元组通过圆括号中用逗号分割的项目定义
 ()圆括号 --01.作为函数的一种结构,02.作为元祖的数据类型,eg: np.zero((N,4))
 []方括号 --01.作为索引的表示      02.作为列表的数据类型
 索引 循环 切片 遍历 index loop slice  traversal iterate  recursion

坐标轴--axis

   shape属性返回值是一个tuple
   shape[0] 对应第 0个 axis 的元素的个数
   shape[1] 对应第 1个 axis 的元素的个数
   ndim =len(arr.shape)
   
  二维: axis = 0 行方向
         axis = 1 列方向
       (N,M) 		 
  三维:len(arr.shape)=3 =ndim
       (N,M,L) axis = 0 axis = 1 axis = 3
	  
  axis  axis = 0	      axis = 1	  
  行  :row           列:column	
  水平: horizontal  垂直: vertical 
  横向              纵向
  纬度 latitude 纬度线水平-标识南北  经度 Longitude :经度线垂直-标识东西
  横纬竖经 纬线 纬度起在赤道,和赤道平行,
           经线 连接南北两极, 
 01.##方式01.    
   np.r_ == hstack
   np.c  == np.vstack
 02.多维数组拼接--将不同维数的数组连接一起  
  np.r_ 混合切片语法--['a,b,c'] eg: '0,0,-1'
  np.r_['0,0,-1',arr_a,arr_b]

数组拼接函数

#!/usr/bin/python3
# -*- coding: utf-8 -*-#

import numpy as np

if __name__ == '__main__':
    arra_3 = np.array([[5,6,7], [7,8,9],[3,4,5], [0,1,2], [1,2,9]])
    arra_4 = np.array([[2], [4], [6], [8], [9]])
    print(arra_3.shape[0], arra_4.shape[0])
    print(arra_3.shape, arra_4.shape, np.hstack((arra_3, arra_4)).shape)

    arra_1 = np.array([[5,6,7,8], [7,8,9,0]])
    arra_2 = np.array([[1,2,3,4], [5,6,9,1], [5,6,7,1]])
    print(arra_1.shape[1], arra_2.shape[1])
    print(arra_1.shape, arra_2.shape, np.vstack((arra_1, arra_2)).shape)

具体应用

vstack( (list1, list2) )。注意是两个括号
np.hstack
np.vstack()
   eg:
   pc=o3d.t.io.read_point_cloud(pcd)
   pcd_xyz=pc.point["positions"].numpy()
   pcd_intensity = pc.point["intensity"].numpy()	   
   points = np.hstack([pcd_xyz,pcd_intensity ])
##数组的ndim必须相同  (N,3)(N,1)---> (N,4)  axis=0方向上元素相同, axis=1 方向上拼接 
 horizontal merger 是横向合并的意思,即:保持行数不变,扩展列数

all_points = np.zero((0,4), dtype=np.float32)
for data in enumerate(datas):
    all_points= np.vstack([all_points, data] )
 数组的ndim必须相同  (0,4)(N,4) (M,4)---> (N+M,4)  axis=0方向上元素拼接, axis=1 方向相同shape[1]的值

连接多为数组的对象

   np.r_
 np.c_

创建数值序列工具

importe numpy  as np
###间隔起始点、终止端,以及指定分隔值总数(包括起始点和终止点);最终函数返回间隔类均匀分布的数值
## 输出数组个数 --线性等距数列
np.linspace(start = 0, stop = 100, num = 5)
 ## 实际调用时无需显示指定参数名称,可以通过参数位置直接匹配
choose_num_index = np.linspace(0, file_k-1, choose_total_num, dtype=int)

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
   start:返回样本数据开始点
   stop:返回样本数据结束点
   num:生成的样本数据量,默认为50
   endpoint:True则包含stop;False则不包含stop
   retstep:If True, return (samples, step), where step is the spacing between samples.(即如果为True则结果会给出数据间隔)
   dtype:输出数组类型
   axis:0(默认)或-1

创建数值序列工具-np.arange

项与项之间的差为固定值的数列--作为索引值使用
类似于python中的range eg: 创建0-9之间的元素
 [x for x in range(10)]
 linspace能够精确控制终止值终值,而arange能够更直接地控制序列中值之间的增量	   
    np.arange(start, stop, step,dtype=None) # (参数:起始,结尾,步长)   

numpy.logspace函数生成等比数列

 10位的时间戳,其精度是秒(s);
 13位的时间戳,其精度是毫秒(ms);

分组-根据key值进行分组

##001.利用传统的dict 
 d = {}
 for key, value in pairs:
     if key not in d:
         d[key] = []
     d[key].append(value)
 	
###002.利用defaultdict的代码如下:
 from collections import defaultdict 
 d = defaultdict(list)
 for key, value in pairs:
     d[key].append(value)
	 
	 
类似--根据时间戳进行分组--根据key进行分组
image_group = {}
for image_file in image_list:
    # 提取时间戳到秒- 1619173359.123156.jpg
    time_sec = get_sec_time(image_file)
    if time_sec not in image_group:
        image_group[time_sec] = []
    image_group[time_sec].append(image_file)

from collections import defaultdict 
d = defaultdict(list)
for img_file in img_list:
    time_sec =get_sec_time(image_file)
    d[time_sec].append(img_file)

抽帧--每一组的处理

for time_sec in img_group:
    img_sig_collect = image_group[time_sec]
	element_cnt = len(img_sig_collect)
    if element_cnt < step:
	    new_sample.extend(img_sig_collect)
	else:
	    count = t 
	    offset = round(element_cnt/step)
		for i in range(0,element_cnt,offset)
		    new_sample.append(img_sig_collect[i])
			count = count +1
			if count == step:
			    break
print(new_sample)

分组-根据元素个数进行分组 range(stop) range(start, stop[, step]) step:步长,默认为 1

image_list.sort()
frame_ps = 2
cur_idx = 0
while(cur_idx < len(image_list)):
    target_img = image_list[cur_idx]
	selected_id = range(max(0,cur_idx), min(cur_idx+frame_ps, len(image_list)) , 1)
	img_window = [image_list[i] for i in selected_id ]
	###打印每组的元素和每组元素中第一个值
	print(target_img)
	for img_data in img_window:
	     print(img_data)   

参考

1.6 字典中的键映射多个值 https://python3-cookbook.readthedocs.io/zh_CN/latest/c01/p06_map_keys_to_multiple_values_in_dict.html