torch&&pandas&&numpy学习笔记

pandas

pandas dataframe,行索引index,列索引columns,可通过df.index和df.columns获取,通过.values可以获取numpy ndarray类型

  1. 选择DataFrame的一部分

iloc loc iat at

  • 直接用[]选择时,只能选择行或者列,用loc/iloc可以同时选择行和列

  • loc可以用行列名字(string)或者整数(int:int)来选择行列,iloc只能用整数

loc选择时两端都是闭区间,iloc选择时左闭右开

  • at选择一个单元格,可以用label和位置坐标混合;iat用位置整数坐标选择一个单元格

  • data[],其中[]里可以是列名,列名组成的列表,bool类型的序列。前两者用来筛选列,后者用来筛选行。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    data['colum1'] # 筛选一列
    data[['colum1','colum3']] # 筛选两列
    data[data['colum1']>100] # boolean选择多行
    data[(data['colum1']>100) & (data['colum2']<50)] # boolean条件组合,必须加括号
    data[0:5] # 筛选前 5 行
    # 筛选多行多列
    data.loc[:,'NumRooms':'Alley'] # Alley列也包含进去了
    data.loc[:1, 'age'] # 返回Series
    data.loc[:1, ['age']] # 返回一个一列的DataFrame
    data.loc[0:2, :] # 0,1,2 三行
    data.iloc[0:2,:] # 0,1 两行

注意:如果要修改值,必须使用.loc或者.iloc,直接用[]得到的可能是一个副本,无法修改原值

  1. dataframe转成numpy ndarray

    1
    data.values

  2. dafaframe转成tensor

    1
    torch.tensor(data.values)

  3. Nan处理

    1
    2
    data = data.fillna(data.mean()) # 把nan用该列的均值替换掉
    pd.get_dummies(data,dummy_na=True) # 把字符串值one-hot编码

pytorch

  1. torch浅拷贝的坑
    1
    2
    3
    x = torch.arange(12)
    y = x.reshape(3,4)
    y[:] = x + y

此时x和y都变化了,可以理解为reshape只是创建了一个矩阵头,数据区共享

numpy

  1. numpy ndarray
    1
    2
    np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)],
    dtype=[("a", "i4"), ("b", "i4"), ("c", "i4")])

给每列都起了个名字

创建普通array:

1
2
>>> np.array([1,2,3])
array([1, 2, 3])

指定区间创建:

arange([start,] stop[, step,], dtype=None, *, like=None)

1
2
3
4
5
6
7
8
9
# arange(stop)
>>> np.arange(8)
array([0, 1, 2, 3, 4, 5, 6, 7])
# arange(start, stop)
>>> np.arange(3, 10)
array([3, 4, 5, 6, 7, 8, 9])
# arange(start, stop, step)
>>> np.arange(3, 10, 2)
array([3, 5, 7, 9])

全0:

1
2
3
4
5
6
7
8
>>> np.zeros(5)
array([0., 0., 0., 0., 0.])
>>> np.zeros((5,2))
array([[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.]])

全1:

1
2
3
4
>>> np.ones((3,3))
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])

单位矩阵:

def eye(N, M=None, k=0, dtype=float, order='C', *, like=None):

N:行数, M:列数, k:对角线位置(正数往上挪,负数向下)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
>>> np.eye(4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
>>> np.eye(4, 3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.]])
>>> np.eye(4, 3, 1)
array([[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.],
[0., 0., 0.]])
>>> np.eye(4, k=1)
array([[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
[0., 0., 0., 0.]])

numpy random

以下尚未经完整测试,从别人博客抄来的,很可能有错误(前4个测试过了)

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
random函数
1.np.random.random(shape)
参数是以一个元组/整数
生成形状为shape,介于[0,1)之间的均匀分布
2.np.random.random_sample()
同np.random.random
2.numpy.random.rand(a0, a1, ..., an)
同random,但是参数不是元组
形状为(a0,a1,...,an),无参数则返回一个python native浮点数
3.numpy.random.randn(a0, a1, ..., an)
同上,但是标准正态分布
4.numpy.random.standard_normal(size=None)
同上,标准正态分布,但参数为元组
生产一个浮点数或N维浮点数组,取数范围:标准正态分布随机样本
5.numpy.random.randint(low, high=None, size=None, dtype='l')
生成一个整数或N维整数数组,取数范围:若high不为None时,取[low,high)之间随机整数,否则取值[0,low)之间随机整数。
6.numpy.random.random_integers(low, high=None, size=None)
生成一个整数或一个N维整数数组,取值范围:若high不为None,则取[low,high]之间随机整数,否则取[1,low]之间随机整数。
7.numpy.random.random_sample(size=None)
生成一个[0,1)之间随机浮点数或N维浮点数组。
8.numpy.random.choice(a, size=None, replace=True, p=None)
从序列中获取元素,若a为整数,元素取值为np.range(a)中随机数;若a为数组,取值为a数组元素中随机元素。
9.numpy.random.shuffle(x)
对X进行重排序,如果X为多维数组,只沿第一条轴洗牌,输出为None。
10.numpy.random.permutation(x)
与numpy.random.shuffle(x)函数功能相同,两者区别:peumutation(x)不会修改X的顺序。


torch&&pandas&&numpy学习笔记
https://jcdu.top/2021/12/14/torch&&pandas&&numpy学习笔记/
作者
horizon86
发布于
2021年12月14日
许可协议