接一章 python:numpy的索引和切片(1)
python:numpy的索引和切片(1)
- 1、numpy中数值的修改
In [31]:t[2,3]
Out[31]: 11
In [32]:t[2,3] = 22 # 修改某一个值
In [33]:t[2,3]
Out[33]: 22
In [34]:t < 10
Out[34]:
array([[ True, True, True, True],
[ True, True, True, True],
[ True, True, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False]])
In [36]:t[t < 10] = 0 # 修改某一部分值 修改t<10的值
In [37]:t
Out[37]:
array([[ 0, 0, 0, 0],
[ 0, 0, 0, 0],
[ 0, 0, 10, 22],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31],
[32, 33, 34, 35],
[36, 37, 38, 39],
[40, 41, 42, 43],
[44, 45, 46, 47],
[48, 49, 50, 51],
[52, 53, 54, 55],
[56, 57, 58, 59],
[60, 61, 62, 63],
[64, 65, 66, 67],
[68, 69, 70, 71],
[72, 73, 74, 75],
[76, 77, 78, 79]])
- 2、numpy的三元运算符 np.where
In [41]:np.where(t<10,0,10) #numpy的三元运算符 np.where,t<10赋值为0,否则赋值为10
Out[41]:
array([[ 0, 0, 0, 0],
[ 0, 0, 0, 0],
[ 0, 0, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10]])
3、numpy中的clip(裁剪)操作
In [42]:t = np.arange(120).reshape(20,6)
In [43]:t
Out[43]:
array([[ 0, 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, 27, 28, 29],
[ 30, 31, 32, 33, 34, 35],
[ 36, 37, 38, 39, 40, 41],
[ 42, 43, 44, 45, 46, 47],
[ 48, 49, 50, 51, 52, 53],
[ 54, 55, 56, 57, 58, 59],
[ 60, 61, 62, 63, 64, 65],
[ 66, 67, 68, 69, 70, 71],
[ 72, 73, 74, 75, 76, 77],
[ 78, 79, 80, 81, 82, 83],
[ 84, 85, 86, 87, 88, 89],
[ 90, 91, 92, 93, 94, 95],
[ 96, 97, 98, 99, 100, 101],
[102, 103, 104, 105, 106, 107],
[108, 109, 110, 111, 112, 113],
[114, 115, 116, 117, 118, 119]])
In [45]: # 将某个值赋值为NAN
In [46]:t[19,3] = np.nan #在赋值给20行4列为nan时候,出错,"不能将floatNAN转换为int,nan为float类型
Traceback (most recent call last):
File "", line 1, in
t[19,3] = np.nan
ValueError: cannot convert float NaN to integer
In [47]:t = t.astype(float) # 先将t转换为float类型
In [48]:t[19,3] = np.nan # 再将20行4列赋值nan
In [49]:t # 输出的20行4列就成功赋值为nan了
Out[49]:
array([[ 0., 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., 27., 28., 29.],
[ 30., 31., 32., 33., 34., 35.],
[ 36., 37., 38., 39., 40., 41.],
[ 42., 43., 44., 45., 46., 47.],
[ 48., 49., 50., 51., 52., 53.],
[ 54., 55., 56., 57., 58., 59.],
[ 60., 61., 62., 63., 64., 65.],
[ 66., 67., 68., 69., 70., 71.],
[ 72., 73., 74., 75., 76., 77.],
[ 78., 79., 80., 81., 82., 83.],
[ 84., 85., 86., 87., 88., 89.],
[ 90., 91., 92., 93., 94., 95.],
[ 96., 97., 98., 99., 100., 101.],
[102., 103., 104., 105., 106., 107.],
[108., 109., 110., 111., 112., 113.],
[114., 115., 116., nan, 118., 119.]])