python数据类型

文章目录

  • 数据类型
    • np.array
    • torch.tensor
    • list(没有shape)
    • pd.DataFrame
    • set {'1', '2', '3'}
    • dict {"1":1, "2":2}
  • 数据转型
    • bool转int(可用于数组)

数据类型

np.array

a = np.array([[1,2,3],
              [4,5,6]])
print(f"a = {a}")
# a = [[1 2 3]
#     [4 5 6]]
print(f"a.size = {a.size}")
print(f"a.shape = {a.shape}")
# a.size = 6
# a.shape = (2, 3)
print(f"a.size() = {a.size()}")
# TypeError: 'int' object is not callable

# np.zeros
# dtype默认' float '
np.zeros((2,3),dtype='int')
---------------
array([[0, 0, 0],
[0, 0, 0]])

np.zeros(5)
-----------------
array([0., 0., 0., 0., 0.])

array有shape和size,其中size是总大小,没有size()
Python中size和shape区别

torch.tensor

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

x = torch.tensor(a)
# x = tensor([[1, 2, 3],
#        [4, 5, 6]], dtype=torch.int32)
print(f"x.size() = {x.size()}")
print(f"x.shape = {x.shape}")
# x.size() = torch.Size([2, 3])
# x.shape = torch.Size([2, 3])
print(f"x.size = {x.size}")
# x.size = 

# torch.ones
>>> torch.ones(2, 3)
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])

>>> torch.ones(5)
tensor([ 1.,  1.,  1.,  1.,  1.])

# torch.ones_like
>>> input = torch.empty(2, 3)
>>> torch.ones_like(input)
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])

tensor有size()和shape,不需要用size
深入浅出Pytorch函数——torch.ones_like

list(没有shape)

b = [[1,2,3],
     [4,5,6]]
print(f"b = {b}")
# b = [[1, 2, 3], [4, 5, 6]]

list没有shape

pd.DataFrame

创建pd.DataFrame的方法. pd.DataFrame函数详解

set {‘1’, ‘2’, ‘3’}

dict {“1”:1, “2”:2}

数据转型

bool转int(可用于数组)

array用.astype(int)
tensor用.int()

你可能感兴趣的:(python,开发语言)