• 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.])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    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.]])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    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]]
    
    • 1
    • 2
    • 3
    • 4

    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()) # 你好
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    数据转型

    bool转int(可用于数组)

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

    numpy转torch.tensor

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

    奇妙用法

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

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

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

    文件打开

    h5

    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(,...)}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    ORACLE连接不上 Linux网络 端口 问题判断
    自学数据库-MySQL
    C# OpenCvSharp 通道分离
    JMeter 调试取样器(Debug Sampler)简介
    csv和excel文件操作
    公司如何禁止拷贝文件
    永远无法实现的“诚实“
    如何搭建数据驱动自动化测试框架?
    CentOS 7 安装新版本的Nginx
    加密磁盘密钥设置方案浅析 — TKS1
  • 原文地址:https://blog.csdn.net/double_yellow/article/details/133187163