python学习

区分python中的列表(list),数组(array),元组(tuple),矩阵(matrix),张量(tensor)

【1】列表——list(python概念)

python中的列表通常通过a = [ ] 创建,list内的元素可以为数字,字符串等,元素类型可不同。除非使用numpy将list转换array,否则python本身是没有数组(array)这种说法的; list中不同元素可以有不同的数据类型,而array中元素必须是同种类型,举例如下:
 

a = [1,2,'a','3']
print(a)
print(type(a))
print(np.shape(a))        #此处不可使用a.shape 因为 AttributeError: 'list' object has no attribute 'shape'
print(type(a[0]),type(a[1]),type(a[2]),type(a[3]))
print()
 
b = np.array(a)
print(b)
print(type(b))
print(b.shape)
print(type(b[0]),type(b[1]),type(b[2]),type(b[3]))  #将列表a变换成数组b后,整型元素全部变换成字符串类型
print(b.dtype)     #dtype只是属于array的一种Attribute,list没有此属性
 
#运行结果如下
[1, 2, 'a', '3']
<class 'list'>
(4,)
<class 'int'> <class 'int'> <class 'str'> <class 'str'>
 
['1' '2' 'a' '3']
<class 'numpy.ndarray'>
(4,)
<class 'numpy.str_'> <class 'numpy.str_'> <class 'numpy.str_'> <class 'numpy.str_'>
<U11   #dtype只是属于array的一种Attribute,U代表unicode,在python3中包括字符串类型。

【2】数组——array(numpy概念)

数组一般用a = np.array() 创建,要求元素类型一致。若同时包含数字和字符串,则数字自动转换成字符串。数组元素也可以是list,其他方面,数组和列表区别不大,可以进行索引,切片,加减乘除等运算。一般需要进行数据运算时推荐使用numpy array,比python本身的list格式更加方便和灵活。举例如下:
 


c= np.array([1,2,3,4])
print(c)
print(c.shape)
print(c.dtype)
print(c.ndim)
print()
 
d = np.array([[1,2,3,4]])
print(d)
print(d.shape)
print(d.ndim)
 
#运行结果如下:
 
[1 2 3 4]
(4,)
int32
1
 
[[1 2 3 4]]
(1, 4)
2
 
#继续举例如下:
 
e = np.array([[1,2,3,4],[5,6]])
print(e)
print(e.shape)
print(e.dtype)
print(e.ndim)
print()
 
f = np.array([[1,2,3,4],[5,6,7,8]])
print(f)
print(f.shape)
print(f.dtype)
print(f.ndim)
 
#运行结果如下:
 
[list([1, 2, 3, 4]) list([5, 6])]
(2,)
object
1
 
[[1 2 3 4]
 [5 6 7 8]]
(2, 4)
int32
2

【3】元组——tuple(python概念)

元组(tuple)区别于列表和数组的地方在于,元组一旦定义,不可以进行修改;而列表和数组是可以的。除此之外元组性质与列表类似。元组使用圆括号a = ()创建,列表使用方括号a = [ ]创建,例如:

tup1 = (1,2,3,4)
print(type(tup1))
print()
 
tup2 = ('w',1,2)
print(type(tup2))
print(type(tup2[0]),type(tup2[1]),type(tup2[2]))
print(np.shape(tup2))
tup3 = (55,)  #单个元素的元组,后面一定要加逗号
#tup2[0] = 0 此处若加上此行,则会返回 TypeError: 'tuple' object does not support item assignment
 
#输出结果如下:
 
<class 'tuple'>
 
<class 'tuple'>
<class 'str'> <class 'int'> <class 'int'>
(3,)

【4】矩阵——matrix

矩阵是二维的数组(array)。数组可以有1,2,3,.....,N维,矩阵是其中一种特例。数学课本中一般叫矩阵,程序语言中一般叫二维数组。可形象理解为数据表,相应的3维数组可形象理解为数据块。创建矩阵的方法很多,比如np.mat()和np.array(),np.mat(ones((2,4)))等,示例如下:

import numpy as np
c = np.mat([[1,2],[3,4]])
d = np.array([[1,2],[4,3]])
e = np.mat(ones((2,5))) #默认是float类型
f = np.random.randint(2,8,size=(3,5)) #从2到8(不包括8)随机生成3*5的矩阵,整型
print(c)
print(c.shape)
print(d)
print(d.shape)
print(e)
print(f)
 
结果如下:
 
[[1 2]
 [3 4]]
(2, 2)
[[1 2]
 [4 3]]
(2, 2)
[[1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]]
[[5 5 3 7 3]
 [3 5 3 7 6]
 [6 3 5 2 4]]

【5】张量——tensor(来自pytroch)

张量可以理解为从0维到n维数组的抽象表示。0阶张量为标量,1阶张量为向量,2阶张量为矩阵,3阶张量为3维数组,......,n阶张量为n维数组。创建方法如下:

t = torch.Tensor([[1,2,3],[4,5,6]])
print(t)
a = t.t()
print(t)
print(a)

输出:
tensor([[1., 2., 3.],
        [4., 5., 6.]])
tensor([[1., 2., 3.],
        [4., 5., 6.]])
tensor([[1., 4.],
        [2., 5.],
        [3., 6.]])

b = t.view(3,2)
print(b)

输出:
tensor([[1., 2.],
        [3., 4.],
        [5., 6.]])

t = torch.zeros(3, 3)
输出:
tensor([[ 0.,  0.,  0.],

        [ 0.,  0.,  0.],

        [ 0.,  0.,  0.]])

t = torch.randn(3, 3)#从正态分布中随机取数创建张量

输出:
tensor([[ 1.0274, -1.3727, -0.2196],

        [-0.7258, -2.1236, -0.8512],

        [ 0.0392,  1.2392,  0.5460]])



可以像切片ndarrays,list一样切片PyTorch张量:

t = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(t[:, -1])
print(t[:2, :])
print(t[-1:, -1:])
print(t[:])
print(t[0:1])


输出:
tensor([ 3.,  6.,  9.])

tensor([[ 1.,  2.,  3.],

        [ 4.,  5.,  6.]])

tensor([[ 9.]])
tensor([[1., 2., 3.],
        [4., 5., 6.],
        [7., 8., 9.]])
tensor([[1., 2., 3.]])

PyTorch张量和Numpy ndarray之间转换

ndarray转为张量

a = np.random.randn(3, 5)
t = torch.from_numpy(a) #也可以 t = torch.Tensor(a)
print(a)
print(t)
print(type(a))
print(type(t))

输出:
[[-0.31597812  0.59444924 -0.28180797  0.29340648  2.91619531]
 [ 0.54381745 -0.06739863 -1.62313861 -0.98301535 -1.03057312]
 [ 1.1861191  -0.62940758 -0.71607079  0.56771096  1.1599937 ]]
tensor([[-0.3160,  0.5944, -0.2818,  0.2934,  2.9162],
        [ 0.5438, -0.0674, -1.6231, -0.9830, -1.0306],
        [ 1.1861, -0.6294, -0.7161,  0.5677,  1.1600]], dtype=torch.float64)
<class 'numpy.ndarray'>
<class 'torch.Tensor'>

张量转为ndarray

t = torch.randn(3, 5)
a = t.numpy()
print(t)
print(a)
print(type(t))
print(type(a))
输出:
tensor([[-0.0788,  0.1190,  0.6463,  0.5811,  1.9486],
        [ 1.1599,  1.4715, -0.8229, -0.0091, -0.6938],
        [ 0.7533, -0.5048,  0.6901,  0.5150, -1.7958]])
[[-0.0788022   0.11903631  0.6462765   0.58112884  1.9486253 ]
 [ 1.1598927   1.471528   -0.82289606 -0.00913649 -0.6937597 ]
 [ 0.75330013 -0.5048048   0.69007     0.5149534  -1.7957904 ]]
<class 'torch.Tensor'>
<class 'numpy.ndarray'>

reshape、view以及resize_之间的区别,这几个都是处理张量的。

permute与view的用法:

tensor的维度换位。参数是一系列的整数,代表原来张量的维度。比如三维就有0,1,2这些dimension。t.permute(1,0)代表把1的维度和0的维度交换。

t = torch.Tensor([[1,2,3],[4,5,6]])
a = t.permute(1,0)
print(a)
b = t.view(3,2)
print(b)

输出:
tensor([[1., 4.],
        [2., 5.],
        [3., 6.]])
tensor([[1., 2.],
        [3., 4.],
        [5., 6.]])

reshape与view的区别在于reshape可以使不连续的张量改变形状,view不可以。

reshape、view和resize_之间的区别之间的区别就比较明显,resize后面有个下划线。前者在改变形状的时候,总的数据个数不能变,而后者在改变形状的时候是可以只截取一部分数据的。看下面的例子:

t = torch.Tensor([[1,2,3],[4,5,6]])
d = t.resize_(2,2)
print(d)
输出:
tensor([[1., 2.],
        [3., 4.]])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值