python数据类型&奇妙用法

数据类型

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 = <built-in method size of Tensor object at 0x000002864CF4D770>

# 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}

取值时key要加引号’',比如a[‘1’]

str r/f/b/…‘abc’

str前是r表示raw,即不转义,将引号中字符串的转义字符忽略
f表示format(应该),可以解析{}中的内容
b表示byte

import os

n = r'\n\n\n\n' # 不表示换行,而是\n\n\n\n字符串

template = r'C:\Users\{}\Documents\myfile.txt'
username = os.getlogin()
filepath = template.format(username)
print(filepath)  # 'C:\\Users\\username\\Documents\\myfile.txt'

print("你好".encode(encoding="utf-8")) # b'\xe4\xbd\xa0\xe5\xa5\xbd'
print(b'\xe4\xbd\xa0\xe5\xa5\xbd'.decode()) # 你好

数据转型

bool转int(可用于数组)

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

numpy转torch.tensor

# 这里还把tensor变成flout型了
V_feat = torch.from_numpy(self.all_V[int2name][()]).float()

奇妙用法

1.dic[‘a’][()]

其实[()]相当于没加,另外这里需要注意的是[ ]中的key要是字符串,即要打’ ’ 符号

import numpy as np
a = np.array([[1,2],[3,4]])
dic = {'a':a}
print(dic['a'])
print(dic['a'][()])

文件打开

h5

self.all_V = \
            h5py.File(os.path.join(config['feature_root'], 'V', f'{self.V_type}.h5'), 'r')
# self.all_A = <HDF5 file "comparE.h5" (mode r)>


# 查看数据的例子之一
def h5_to_dict(self, h5f):
        ret = {}
        for key in h5f.keys():
            ret[key] = h5f[key][()] # 这个[()]好像加不加都行
        return ret
elf.all_A = self.h5_to_dict(self.all_A)
# len(self.all_A.keys()) = 5531
# self.all_A = {'Ses02F_impro04_M018': array([[ 0.00000000e+00,  6.75401259e-02,  0.00000000e+00, ...,
#     -1.20360625e+00, -4.94986379e-01, -8.83533782e-01],
#    ...,
#    [ 0.00000000e+00,  5.76842853e-01,  0.00000000e+00, ...,
#     -5.83715754e-01, -3.27415824e-01,  7.86298778e-01]]), 'Ses02F_impro04_M019': array(,...)}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值