博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python运算学习之Numpy ------ 数组操作:连接数组、拆分数组 、广播机制、结构化数组、文件贮存与读写、np.where、数组去重...
阅读量:4699 次
发布时间:2019-06-09

本文共 4488 字,大约阅读时间需要 14 分钟。

数组的连接:

1 # 连接数组 2 A = np.zeros((3, 4)) 3 B = np.ones_like(A) 4 print(A, "\n-------分割符--------\n", B) 5 print("np.vstack的效果:\n", np.vstack((A, B)))  # 这是多维数组按列拼接,如果A的shape是(3,4,5),拼接之后为(6,4,5) 6 print("np.hstack的效果:\n", np.hstack((A, B)))  # 这是多维数组按行拼接,如果A的shape是(3,4,5),拼接之后为(3,8,5) 7 a = np.array([1, 1.2, 1.3]) 8 b = np.array([2, 2.2, 2.3]) 9 c = np.array([3, 3.2, 3.3])10 print("np.column_stack的效果:\n", np.column_stack((A, B)))11 print("np.row_stack的效果:\n", np.row_stack((A, B)))12 print("np.column_stack的效果:\n", np.column_stack((a, b, c)))13 print("np.row_stack的效果:\n", np.row_stack((a, b, c)))14 Out[1]:15 [[0. 0. 0. 0.]16  [0. 0. 0. 0.]17  [0. 0. 0. 0.]] 18 -------分割符--------19  [[1. 1. 1. 1.]20   [1. 1. 1. 1.]21   [1. 1. 1. 1.]]22 np.vstack的效果:23  [[0. 0. 0. 0.]24   [0. 0. 0. 0.]25   [0. 0. 0. 0.]26   [1. 1. 1. 1.]27   [1. 1. 1. 1.]28   [1. 1. 1. 1.]]29 np.hstack的效果:30  [[0. 0. 0. 0. 1. 1. 1. 1.]31   [0. 0. 0. 0. 1. 1. 1. 1.]32   [0. 0. 0. 0. 1. 1. 1. 1.]]33 np.column_stack的效果:34  [[0. 0. 0. 0. 1. 1. 1. 1.]35   [0. 0. 0. 0. 1. 1. 1. 1.]36   [0. 0. 0. 0. 1. 1. 1. 1.]]37 np.row_stack的效果:38  [[0. 0. 0. 0.]39   [0. 0. 0. 0.]40   [0. 0. 0. 0.]41   [1. 1. 1. 1.]42   [1. 1. 1. 1.]43   [1. 1. 1. 1.]]44 np.column_stack的效果:45  [[1.  2.  3. ]46   [1.2 2.2 3.2]47   [1.3 2.3 3.3]]48 np.row_stack的效果:49  [[1.  1.2 1.3]50   [2.  2.2 2.3]51   [3.  3.2 3.3]]

拆分数组:

1 A = np.arange(0, 12).reshape(2, 6) 2 print("二维数组A:\n", A) 3 [B, C, D] = np.hsplit(A, 3)  # hsplit(ary, indices_or_sections), np.hsplit(A, 3)为默认按列均分数组 4 print(B, "\n--------*---------\n", C, "\n") 5 [E, F] = np.vsplit(A, 2)  # 默认按行均分数组 6 print(E, "\n--------*---------\n", F, "\n") 7 [A1, A2, A3] = np.split(A, [1, 3], axis=1)  # axis=1按列切分,axis=0按行切分 8 print(A1, "\n--------*---------\n", A2, "\n") 9 Out[2]:10 二维数组A:11  [[ 0  1  2  3  4  5]12   [ 6  7  8  9 10 11]]13  [[0 1]14   [6 7]] 15 --------*---------16  [[2 3]17   [8 9]] 18 19  [[0 1 2 3 4 5]] 20 --------*---------21  [[ 6  7  8  9 10 11]] 22 23  [[0]24   [6]] 25 --------*---------26  [[1 2]27   [7 8]]

数组的广播机制:

1 A = np.arange(0, 16).reshape(4, 4) 2 b = np.array([1.2, 2.3, 3, 4]) 3 print(A + b) 4 m = np.arange(6).reshape((3, 2, 1)) 5 n = np.arange(6).reshape((3, 1, 2)) 6 print("----*----\n", m, "\n----*----\n", n) 7 print("m + n 的广播:\n", m + n) 8 Out[3]: 9 [[ 1.2  3.3  5.   7. ]10  [ 5.2  7.3  9.  11. ]11  [ 9.2 11.3 13.  15. ]12  [13.2 15.3 17.  19. ]]13 ----*----14  [[[0]15   [1]]16 17  [[2]18   [3]]19 20  [[4]21   [5]]] 22 ----*----23  [[[0 1]]24 25   [[2 3]]26 27   [[4 5]]]28 m + n 的广播:29  [[[ 0  1]30    [ 1  2]]31 32   [[ 4  5]33    [ 5  6]]34 35   [[ 8  9]36    [ 9 10]]]

   示意图如下:

  

结构化数组:

1 structure_array = np.array([(1, 'First', 0.5, 1+2j), (2, 'Second', 1.3, 2-2j), (3, 'Third', 0.8, 1+3j)]) 2 print(structure_array) 3 structure_array_1 = np.array([(1, 'First', 0.5, 1+2j), (2, 'Second', 1.3, 2-2j), (3, 'Third', 0.8, 1+3j)], 4                              dtype=[('id', '

 

 文件贮存与读写:

1 A = np.arange(12).reshape(3, 4) 2 np.save('save_data', A) 3 load_data = np.load('save_data.npy') 4 print("Numpy默认保存的格式:\n", load_data) 5 # 保存为csv格式 6 # savetxt(fname,X,fmt='%.18e',delimiter=' ',newline='\n',header='',footer='',comments='# ', encoding=None) 7 np.savetxt('data_csv.csv', A) 8 txt_csv = np.loadtxt('data_csv.csv') 9 print("Numpy导入csv的格式:\n", txt_csv)10 # np.genfromtxt()导入数据11 data = np.genfromtxt('data_csv.csv', delimiter=' ')12 print("genfromtxt导入csv的格式:\n", data)13 Out[5]:14 Numpy默认保存的格式:15  [[ 0  1  2  3]16   [ 4  5  6  7]17   [ 8  9 10 11]]18 Numpy导入csv的格式:19  [[ 0.  1.  2.  3.]20   [ 4.  5.  6.  7.]21   [ 8.  9. 10. 11.]]22 genfromtxt导入csv的格式:23  [[ 0.  1.  2.  3.]24   [ 4.  5.  6.  7.]25   [ 8.  9. 10. 11.]]

 

 np.where:

  np.where实际上是 x if condition else y 的矢量化版本

1 x = np.array([2, 3, 4, 5, 6]) 2 y = np.array([10, 11, 12, 13, 14]) 3 condition = np.array([True, False, True, True, False]) 4 z = np.where(condition, x, y) 5 print(z) 6 data = np.array([[1, 2, np.nan, 4], [np.nan, 2, 3, 4]]) 7 print(np.isnan(data)) 8 print(np.where(np.isnan(data), 0, data)) 9 Out[6]:10 [ 2 11  4  5 14]11 [[False False  True False]12  [ True False False False]]13 [[1. 2. 0. 4.]14  [0. 2. 3. 4.]]

数组去重:

1 print(np.unique([1, 1, 2, 3, 4, 4, 6]))2 print(np.unique(np.array([[1, 1, 2, 3, 4, 4, 6], [1, 5, 9, 4, 7, 2, 1]])))3 test = np.unique([[1, 1, 2, 3, 4, 4, 6], [1, 5, 9, 4, 7, 2, 1]])4 print(test, type(test))5 Out[7]:6 [1 2 3 4 6]7 [1 2 3 4 5 6 7 9]8 [1 2 3 4 5 6 7 9] 

转载于:https://www.cnblogs.com/dan-baishucaizi/p/9389338.html

你可能感兴趣的文章
tomcat 和MySQL的安装
查看>>
git常用操作
查看>>
京东SSO单点登陆实现分析
查看>>
u-boot启动第一阶段
查看>>
MySQL批量SQL插入性能优化
查看>>
定义列属性:null,default,PK,auto_increment
查看>>
用户画像展示
查看>>
C#中StreamReader读取中文出现乱码
查看>>
使用BufferedReader的时候出现的问题
查看>>
批处理文件中的路径问题
查看>>
hibernate出现No row with the given identifier exists问题
查看>>
为什么wait()和notify()属于Object类
查看>>
配置NRPE的通讯
查看>>
匹配两个空格之间的字符。。。
查看>>
CSS 文字溢出 变成省略号 ...
查看>>
Spring事务
查看>>
java编程基础(三)流程控制语句
查看>>
让数据库跑的更快的7个MySQL优化建议
查看>>
jquery 取id模糊查询
查看>>
解决在vue中,自用mask模态框出来后,下层的元素依旧可以滑动的问题
查看>>