numpy使用教程

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)

在numpy中可以理解为方向,使用0,1,2…数字表示,对于一个一维数组,只有一个0轴,对于二维数组(shape(2,2))有0轴和1轴,对于三维数组(shape(2,2,3)),有0,1,2三个轴

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. 取行

行和列索引都是从0开始

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. 取列

行和列索引都是从0开始

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 教程

©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值