# numpy使用

numpy.ndarray

In [2]: import numpy as np
In [3]: data = np.arange(15)

In [4]: data
Out[4]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
# 查看数组的形状
In [5]: data.shape
Out[5]: (15,)
# 修改数组的形状 3行5列 二位数组
In [6]: data.reshape(3,5)
Out[6]:
array([[ 0,  1,  2,  3,  4],
[ 5,  6,  7,  8,  9],
[10, 11, 12, 13, 14]])
In [8]: data = data.reshape(3,5)

In [9]: data.shape
Out[9]: (3, 5)
# 返回一维数组
In [10]: data.flatten()
Out[10]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])

1. 指定创建的数据类型
In [12]: a = np.array([1,0,1,0],dtype=np.bool)
In [13]: a
Out[13]: array([ True, False,  True, False])

1. 修改数组的数据类型
In [15]: a.astype('i1')
Out[15]: array([1, 0, 1, 0], dtype=int8)

1. 修改浮点型的小数位数
保留两位小数
In [16]: import random
In [17]: d = np.array([random.random() for i in range(10)])
In [18]: d
Out[18]:
array([0.75953379, 0.64835586, 0.70574706, 0.39846516, 0.1770987 ,
0.97968839, 0.26600079, 0.87671019, 0.62948521, 0.396063  ])

In [19]: np.round(d,2)
Out[19]: array([0.76, 0.65, 0.71, 0.4 , 0.18, 0.98, 0.27, 0.88, 0.63, 0.4 ])
# 保留三位小数
In [21]: "%.3f"%random.random()
Out[21]: '0.618'


# 轴的概念(axis)

0轴是行，1轴是列

shape(3,2,3)分别是0轴(块)，1轴(行)，2轴(列)

In [23]: data = np.arange(1,19)
In [24]: data
Out[24]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
18])
# reshape(3,2,3)分别是0轴(块)，1轴(行)，2轴(列)
# 3个块，每一块中是2行3列
In [27]: data.reshape(3,2,3)
Out[27]:
array([[[ 1,  2,  3],
[ 4,  5,  6]],

[[ 7,  8,  9],
[10, 11, 12]],

[[13, 14, 15],
[16, 17, 18]]])


unpack 是转置效果

# 转置操作

1. transpose()
2. T
3. swapaxes(1,0) 0轴和1轴交换
In [28]: data = np.arange(24).reshape(4,6)

In [29]: data
Out[29]:
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]])

In [30]: data.transpose()
Out[30]:
array([[ 0,  6, 12, 18],
[ 1,  7, 13, 19],
[ 2,  8, 14, 20],
[ 3,  9, 15, 21],
[ 4, 10, 16, 22],
[ 5, 11, 17, 23]])
In [31]: data.T
Out[31]:
array([[ 0,  6, 12, 18],
[ 1,  7, 13, 19],
[ 2,  8, 14, 20],
[ 3,  9, 15, 21],
[ 4, 10, 16, 22],
[ 5, 11, 17, 23]])
In [32]: data.swapaxes(1,0)
Out[32]:
array([[ 0,  6, 12, 18],
[ 1,  7, 13, 19],
[ 2,  8, 14, 20],
[ 3,  9, 15, 21],
[ 4, 10, 16, 22],
[ 5, 11, 17, 23]])



# 索引和切片

data[行索引，列索引]

## 1. 取行

In [33]: data
Out[33]:
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]])

# 取第一行
In [34]: data[0]
Out[34]: array([0, 1, 2, 3, 4, 5])

# 取第四行
In [35]: data[3]
Out[35]: array([18, 19, 20, 21, 22, 23])

# 从索引1开始到索引3结束，不包括索引3
In [36]: data[1:3]
Out[36]:
array([[ 6,  7,  8,  9, 10, 11],
[12, 13, 14, 15, 16, 17]])

# 2是间隔
In [38]: data[0::2]
Out[38]:
array([[ 0,  1,  2,  3,  4,  5],
[12, 13, 14, 15, 16, 17]])

# 取不连续的多行 这里取的是0行和3行
In [39]: data[[0,3]]
Out[39]:
array([[ 0,  1,  2,  3,  4,  5],
[18, 19, 20, 21, 22, 23]])


## 2. 取列

In [33]: data
Out[33]:
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]])
# 取2行3列数据
In [41]: data[2,3]
Out[41]: 15

# 取2行所有列数据 后面冒号代表所有列
In [42]: data[2,:]
Out[42]: array([12, 13, 14, 15, 16, 17])

# 取所有行，0列数据
In [43]: data[:,0]
Out[43]: array([ 0,  6, 12, 18])

# 取2-所有行，0列数据
In [44]: data[2:,0]
Out[44]: array([12, 18])

# 取连续多列 取1列和2列数据
In [46]: data[:,1:3]
Out[46]:
array([[ 1,  2],
[ 7,  8],
[13, 14],
[19, 20]])

# 取连续多列 取2列到最后一列所有数据
In [48]: data[:,2:]
Out[48]:
array([[ 2,  3,  4,  5],
[ 8,  9, 10, 11],
[14, 15, 16, 17],
[20, 21, 22, 23]])

# 取不连续多列 取1,3,5列
In [49]: data[:,[1,3,5]]
Out[49]:
array([[ 1,  3,  5],
[ 7,  9, 11],
[13, 15, 17],
[19, 21, 23]])


## 3. 取多行多列

In [33]: data
Out[33]:
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]])

# 取连续多行多列
In [50]: data[1:3,2:4]
Out[50]:
array([[ 8,  9],
[14, 15]])

# 取不连续数据
# [0,1,2]代表行索引，[0,2,4]代表列索引
In [51]: data[[0,1,2],[0,2,4]]
Out[51]: array([ 0,  8, 16])


# 参考链接

1. NumPy 教程

01-25 5893

06-19 457

07-17 48

07-13 9692

01-17 17万+

07-02 1145

09-06 1万+

11-12 1万+

05-25 58

05-26 749

#### numpy中一些用法汇总

©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。