Summary of basic knowledge recently used in python (5)

1. Variable type conversion in pytorch

将numpy矩阵转换为Tensor张量:sub_ts = torch.from_numpy(sub_img)   #sub_img为numpy类型
将Tensor张量转化为numpy矩阵:sub_np1 = sub_ts.numpy()             #sub_ts为tensor张量
将numpy转换为Variable:sub_va = Variable(torch.from_numpy(sub_img))
将Variable张量转化为numpy:sub_np2 = sub_va.data.numpy()

(1) Conversion between CPU or GPU tensor

Generally, just add long(), int(), double(), float(), byte() and other functions after Tensor to convert Tensor;

For example: Torch.LongTensor--->Torch.FloatTensor, just use data.float() directly

You can also use the type() function, data is the Tensor data type, and data.type() is the type of the given data. If you use data.type(torch.FloatTensor), it will be forcibly converted to a torch.FloatTensor type tensor.

When you don't know what type you want to convert, but you need the product of two tensors a1 and a2, you can use a1.type_as(a2) to convert a1 to the same type as a2.

(2) CPU tensor----> GPU tensor, use data.cuda()

(3) GPU tensor ----> CPU tensor uses data.cpu()

(4) Variable variable is converted to ordinary Tensor, in fact, it can be understood that Variable is a Wrapper, and the data inside is Tensor. If Var is Variable, use Var.data to get Tensor variable

(5) Conversion between Tensor and Numpy Array

Tensor---->Numpy can use data.numpy(), data is Tensor variable

Numpy ----> Tensor can use torch.from_numpy(data), data is numpy variable

reference: https://blog.csdn.net/hustchenze/article/details/79154139 ,https://blog.csdn.net/ pengge0433/article/details/79459679.

2. Encountered a problem:

pbb为<type 'tuple'>: (234, 5),

pbb1=pbb[0] is <type'tuple'>: (5,) becomes an array and reduces the dimensionality,

pbb2=pbb[0:1] is <type'tuple'>: (1, 5) or a two-dimensional matrix

pbb3=np.delete(pbb2,[0],axis=0) is <type'tuple'>: (0, 5) is still a two-dimensional matrix.

Summary: Note that A[0] and A[0:1] of matrix A are different.

3. (1) The type typeremains unchanged, and the value is valuerounded. (Rounding matrix)
, taken integer part np.trunc
rounded up np.ceil
rounded down np.floor
rounded to the nearest integer np.rint

(2) Type typechange

AA = np.array
AA.astype(np.int)

ps: This doesn't seem to be too useful. Maybe I used it wrong, or the version is wrong?

Use the following to work:

a=np.array(a, dtype=np.int16)

(3) The difference caused by using list and np.array to store data respectively

# 为了看不同点,生成一个不变的数组

# 如果用list,那么astype就有点麻烦

In [245]: customersAge = [70 * np.random.rand(20)]

In [250]: np.trunc(customersAge)
Out[250]:
array([[ 62.,  33.,  47.,  25.,  57.,  64.,   0.,  50.,  66.,  34.,  44.,
         45.,  14.,  40.,  48.,  45.,   5.,  50.,  29.,  35.]])

In [251]: np.ceil(customersAge)
Out[251]:
array([[ 63.,  34.,  48.,  26.,  58.,  65.,   1.,  51.,  67.,  35.,  45.,
         46.,  15.,  41.,  49.,  46.,   6.,  51.,  30.,  36.]])

In [252]: np.floor(customersAge)
Out[252]:
array([[ 62.,  33.,  47.,  25.,  57.,  64.,   0.,  50.,  66.,  34.,  44.,
         45.,  14.,  40.,  48.,  45.,   5.,  50.,  29.,  35.]])

In [253]: np.rint(customersAge)
Out[253]:
array([[ 62.,  33.,  47.,  25.,  58.,  64.,   0.,  50.,  67.,  35.,  44.,
         45.,  14.,  41.,  49.,  45.,   6.,  51.,  29.,  36.]])

# 但这样list不能直接用astype,要把格式换成array...呵呵呵

In [254]: customersAge.astype(np.int)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-254-a648fd813d6e> in <module>()
----> 1 customersAge.astype(np.int)

AttributeError: 'list' object has no attribute 'astype'

In [256]: np.array(customersAge).astype(np.int)
Out[256]:
array([[62, 33, 47, 25, 57, 64,  0, 50, 66, 34, 44, 45, 14, 40, 48, 45,  5,
        50, 29, 35]])

# 既然用numpy,最好就是np.array用到底

In [264]: customersAge = np.array( 70 * np.random.rand(20))

In [265]: customersAge.astype(np.int)
Out[265]:
array([57, 31, 59,  0, 27,  6, 25, 23, 54, 18, 33, 17, 67, 66, 24, 57, 45,
       64, 62, 47])

Reference: https://www.jianshu.com/p/23a9224780e8

4.Python takes some discontinuous rows and columns in the numpy matrix

The original matrix and the target you want to select are as follows:

C_A = c[[0,2]]    #先取出想要的行数据
C_A = C_A[:,[2,3]] #再取出要求的列数据
print(C_A) #输出最终结果

Reference: https://blog.csdn.net/qq_34734303/article/details/80631831

Guess you like

Origin blog.csdn.net/qq_36401512/article/details/89927326