Loading

数据处理

1. python中列表(list)操作

1.1列表的创建

list1 = ['Google', 'Runoob', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]
list4 = ['red', 'green', 'blue', 'yellow', 'white', 'black']
list5 = list((1,2,3,4))

1.2列表扩充

  用`append()`,在列表末尾追加新元素
  用`insert()`,在列表中指定位置添加元素
  用`+`运算符,将两个列表拼接在一起
  用`extend()`,在一个列表后面拼接进另一个列表
list = [1,2,3]
list.append(1)#list = [1, 2, 3, 1]
list.insert(0,5)#list = [5, 1, 2, 3]  
list1 = [1, 2, 3]
list2 = [3, 4, 5]
list3 = list1 + list2#list3 == [1, 2, 3, 3, 4, 5]

1.3列表删减

  `del list[i]`,删除列表**索引值**为i的元素
  `list.remove(j)`,删除列表中**值**为j的元素     
list1 = list2 = list3 = list4 = list5 = [1, 2, 3]
del list1[0] #list1 = = [2, 3]                
list2.remove(3)#list2 = [1, 2]
list3.pop()#list3 = [1, 2]
list4.clear()#list4 = []
del list5 #提示错误信息

1.4列表切片

list = [[1, 2, 3, 4, 5, 6, 7, 8]
list[0:3]#打算取list的前三个元素
list[::2]#隔俩个元素取一个值
list[:1]#如果是从头开始取,取到后面某个下标结束,那么开头的下标可以不写
list[2:]#如果从某个下标开始取,取到末尾结束,那么末尾的下标可以省略不写
list[:]#取全部元素
list1[::-1] #反转list,不改变原来list的值
lis1.reverse() #反转list,并将饭庄后的值赋给list,改变了原来list的值

2.numpy中的数组(array)操作numpy.ndarray

2.1数组的创建

import numpy as np
a = np.zeros((3,4), dtype = float, order = 'C')#定义一个3*4的全为0的数组,行优先,逐行存储
b = np.ones((3,4), dtype = float, order = 'F')#定义一个3*4的全为1的数组,列优先,逐列存储
c = np.arrange(5)#np.arange(start, stop, step, dtype)#在 start(默认为0)-stop的范围,以step的步长生成数据类型为dtype的数组
d = np.linspace(np.linspace(1,10,10))
# np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)在start与stop之间生成num(默认为50)个点
# endpoint为 true 时,数列中包含stop值,反之不包含,默认是True
# retstep为 True 时,生成的数组中会显示间距,反之不显示

2.2数组的扩充

numpy.concatenate((a1, a2, ...), axis)将数据两个或多个相同形状的数组沿指定轴连接,增加数据的量,但不改变数据的维度
参考链接:> https://blog.csdn.net/qq_17550379/article/details/78934529
> https://blog.csdn.net/Riverhope/article/details/78922006?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param

a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
c = np.concatenate((a, b))

  numpy.stack(arrays, axis)将数据进行堆叠

a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
numpy.stack((a,b), axis=0)#直接将a,b堆叠在一起,物理堆叠,组成一个三维数组
numpy.stack((a,b), axis=1)#抽出来a,b的的对应行组成数组,然后将重组后的数组进行物理堆叠
numpy.stack((a,b), axis=2)#抽出来a,b的的对应对应行竖排组成数组,然后将重组后的数组进行物理堆叠。

(此处存疑,以后遇见深究)

2.3数组的删减

numpy.split(ary, indices_or_sections, axis)把一个数组从左向右拆分成indices_or_sections个数组
a = np.array[1, 2, 3, 4, 5, 6, 7, 8]
np.split(a, 2)#分成了两个数组,并放在一个数组内[array([1, 2, 3, 4]), array([5, 6, 7, 8])]
numpy.hsplit(ary, indices_or_sections, axis)从左向右,按列拆分成indices_or_sections个数组

a = np.array([[1, 2, 3, 4],
              [5, 6, 7, 8]])
np.hsplit(a, 2)
#[array([[1, 2],[5, 6]]), array([[3, 4],[7, 8]])]

numpy.vsplit(ary, indices_or_sections, axis)从上向下,按行拆分indices_or_sections个数组
numpy.resize(arr, shape)将数组arr修改为shape大小的数组,可以改变数据量
arr.reshape(shape)将数组arr修改为shape大小的数组,但不改变数据量,裁剪
numpy.append(arr, values, axis=None)在数组arr的末尾添加值
以下参考> https://www.runoob.com/numpy/numpy-array-manipulation.html#numpy_oparr6
numpy.insert(arr, obj, values, axis)
Numpy.delete(arr, obj, axis)
numpy.unique(arr, return_index, return_inverse, return_counts)

2.4数组的切片

a = np.array([1, 2, 3, 4,5, 6, 7, 8])
b = np.array([[1, 2, 3, 4],[ 5, 6, 7, 8]])
a[0:3]#打算取数组a的前三个元素
a[2:7:1]#[start:stop:step]从索引值2开始到索引值7结束,间隔为 1切割。对矩阵就是从第start行开始到stop行结束,间隔为step取出
b[...,1]#取第二列元素
b[1,...]#去第二行元素
b[...,1:]# 取第2列及剩下的所有元素

3.在torch中可以定义一个张量(Tensor)来表示数据

3.1张量的创建

import torch
a = torch.FloatTensor(3,4)  # 随机生成3行4列,数据类型为浮点型的二维张量
a = torch.IntTensor([2,3,4,5])# 指定生成一个一行四列,数据类型为整型的一维张量
a = torch.randn(2,2)#随机生成的浮点数的取值满足均值为0,方差为1的正太分布的2维张量
a = torch.rand(2,3)#随机生成的浮点数在0-1区间均匀分布的2维张量
a = torch.range(1,20,1)#参数有三个:起始值、结束值、步长,随机生成的浮点数从1-20,步长为1的张量
a = torch.zeros(1,1)#生成一个1*1的全为0的二维张量

3.2张量的扩充

a = torch.randn(33, 55)#torch.Size([33, 55])
torch.repeat(x, y, z)在指定维度上重复

a.repeat(1, 1).size()#torch.Size([33, 55])
a.repeat(2,1).size()#torch.Size([66, 55])
tensor.reshape(r, c, k)# 将张量tensor各个维度的大小改变为(r, c, k)
tensor.squeeze() #表示将张量tensor中大小为1的维度去掉
tensor.unsqueeze(dim) #表示在张量tensor指定的维度dim上增加一个大小为1的维度
tensor.permute(2, 0, 1,3) #表示对张量tensor的维度进行重新排列
tensor.transpose(0, 2) #表示将张量tensor指定的两个维度0 和 2 进行互换
tensor.index_select(1, [1, 4, 5]) #表示将张量tensor第二个维度,索引为1,4,5的数据挑选出来,其余数据丢掉
tensor.masked_select(mask) #其中mask是一个与tensor大小相同的张量,且其所有元素为1或者0,该函数的作用是将mask张量中值为1的位置的数据挑选出来,将其余数据丢掉,返回值由挑选出来的元素组成的1维张量。

torch.cat([t1, t2], k) #表示将张量他t1和t2在维度k上进行拼接,注意:拼接完后张量的维度并没有变化。类似numpy里的np.stack((a,b),axis=0)物理堆叠的形式
torch.stack([t1, t2], k) #表示将张量t1和t2在维度k上进行拼接,拼接之后维度会增加1。 这种方式要求被拼接的张量t1, t2必须大小形状相同,增加的维度的大小等于拼接的张量的个数。

  • 3.3torch运算机制

4.list、array、tensor之间的转变

np.array(list)#list-->array 
torch.tensor(list)#list-->tensor
torch.from_numpy(data)#array-->tensor ,data的类型为numpy.ndarray
data.numpy()#tensor-->array ,data的类型为torch.Tensor
array.tolist()#array-->list
posted @ 2020-11-01 00:15  Guang'Jun  阅读(163)  评论(0编辑  收藏  举报