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区别
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
b = [[1,2,3],
[4,5,6]]
print(f"b = {b}")
# b = [[1, 2, 3], [4, 5, 6]]
list没有shape
创建pd.DataFrame的方法. pd.DataFrame函数详解
取值时key要加引号’',比如a[‘1’]
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()) # 你好
array用.astype(int)
tensor用.int()
# 这里还把tensor变成flout型了
V_feat = torch.from_numpy(self.all_V[int2name][()]).float()
其实[()]相当于没加,另外这里需要注意的是[ ]中的key要是字符串,即要打’ ’ 符号
import numpy as np
a = np.array([[1,2],[3,4]])
dic = {'a':a}
print(dic['a'])
print(dic['a'][()])
self.all_V = \
h5py.File(os.path.join(config['feature_root'], 'V', f'{self.V_type}.h5'), 'r')
# self.all_A =
# 查看数据的例子之一
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(,...)}