numpy 数据维度修改[增加,顺序调整等]
1 #!/usr/bin/env python 2 # -*- encoding: utf-8 -*- 3 ''' 4 @File : test.py 5 @Time : 2022/09/07 15:04:59 6 @Author : qfr 7 @Version : 1.0 8 @Contact : qfr@whu.edu.cn 9 @License : (C)Copyright 2021-2025, qfr&lz 10 @Desc : 对数据的维度进行调整 11 原维度: 1000 * 6 12 输出: 10 * 100 * 1 * 6 13 下面有三种方式进行调整,都可实现 14 ''' 15 16 # here put the import lib 17 import numpy as np 18 import tensorflow as tf 19 20 x = np.ones((1000,6)) 21 x[:,0] = x[:,0] * 0.2 22 x[:,1] = x[:,1] * 0.5 23 x[:,2] = x[:,2] * 9.8 24 25 x[:,3] = x[:,3] * 1.2 26 x[:,4] = x[:,4] * 1.5 27 x[:,5] = x[:,5] * 3.8 28 29 x[1,0] = x[1,0] + 100 30 31 # 数组维度调整方式一 32 # 直接在最后加一个维度,然后进行轴系的调整 33 print(x.shape) 34 y = np.reshape(x, (10, 100,6)) 35 tmp = y[:,:,:,np.newaxis] 36 tmp1 = np.transpose(tmp, [0,1,3,2]) # (!!!!强大的操作!!!!) 37 print(tmp1.shape) 38 print(tmp1[0]) 39 40 # 数组维度调整方式二 41 # 先把数据抽出来,扩充成4维度,然后再在最后一个维度进行拼接即可 42 for i in range(6): 43 tmp1 = y[:,:,i][..., tf.newaxis][..., tf.newaxis] 44 if i: 45 final = tf.concat([final, tmp1], axis=3) 46 else: 47 final = tmp1 48 # final = final.numpy() 49 print(final.shape) 50 print(final[0]) 51 52 # 数组维度调整方式三(!!!!强大的功能!!!!) 53 # 直接在对应位置插入1维坐标轴(numpy支持该操作) 54 y = np.reshape(x, (10, 100,6)) 55 tmp1 = y[:,:,np.newaxis,:,] 56 print(tmp1.shape) 57 print(tmp1[0])
记录每天生活的点点滴滴,呵呵呵呵呵呵

浙公网安备 33010602011771号