python-numpy的基本用法03
2017-08-04 17:30
465 查看
python-numpy的基本用法03
线性代数运算
2、随机数产生
3、数组合并拆分
#coding:utf-8 import numpy as np from arrayIndexAndSlicing import arr arr1 = np.array([[1, 2, 3], [4, 5, 6]]) arr2 = np.array([[7, 8, 9], [10, 11, 12]]) print np.concatenate([arr1, arr2], axis = 0) # 按行连接 print np.concatenate([arr1, arr2], axis = 1) # 按列连接 print np.vstack((arr1, arr2)) # 垂直堆叠=按行连接 print np.hstack((arr1, arr2)) # 水平堆叠=按列连接 print '*' * 50 arr = np.arange(25).reshape(5,5) print arr first, second, third = np.split(arr, [1,3], axis = 0) #水平拆分 print first #[[0 1 2 3 4]] print second # [[ 5 6 7 8 9] # [10 11 12 13 14]] print third # [[15 16 17 18 19] # [20 21 22 23 24]] # 堆叠辅助类 arr = np.arange(6) arr1 = arr.reshape((3, 2)) arr2 = np.random.randn(3, 2) print np.r_[arr1, arr2] # [[ 0. 1. ] # [ 2. 3. ] # [ 4. 5. ] # [ 1.47892319 -0.14980914] # [-0.63959067 1.56742361] # [ 1.47719175 -0.72199307]] print 'c_用于按列堆叠' print np.c_[np.r_[arr1, arr2], arr] # c_用于按列堆叠 # [[ 0. 1. 0. ] # [ 2. 3. 1. ] # [ 4. 5. 2. ] # [ 1.47892319 -0.14980914 3. ] # [-0.63959067 1.56742361 4. ] # [ 1.47719175 -0.72199307 5. ]] print '切片直接转为数组' print np.c_[1:6, -10:-5] # 切片直接转为数组 # [[ 1 -10] # [ 2 -9] # [ 3 -8] # [ 4 -7] # [ 5 -6]]
4、repet元素
#coding:utf-8 import numpy as np import numpy.random as np_random print 'Repeat: 按元素' arr = np.arange(3) print arr.repeat(3) #[0 0 0 1 1 1 2 2 2] print arr.repeat([2, 3, 4]) # 3个元素,分别复制2, 3, 4次。长度要匹配! # [0 0 1 1 1 2 2 2 2] print print 'Repeat,指定轴' arr = np_random.randn(2, 2) print arr # [[-1.28463953 0.19053388] # [ 1.10101803 -0.18598974]] print arr.repeat(2, axis = 0) # 按行repeat # [[-1.28463953 0.19053388] # [-1.28463953 0.19053388] # [ 1.10101803 -0.18598974] # [ 1.10101803 -0.18598974]] print arr.repeat(2, axis = 1) # 按列repeat # [[-1.28463953 -1.28463953 0.19053388 0.19053388] # [ 1.10101803 1.10101803 -0.18598974 -0.18598974]] print print 'Tile: 参考贴瓷砖' print np.tile(arr, 2) # [[-1.28463953 0.19053388 -1.28463953 0.19053388] # [ 1.10101803 -0.18598974 1.10101803 -0.18598974]] print np.tile(arr, (2, 3)) # 指定每个轴的tile次数 # [[-1.28463953 0.19053388 -1.28463953 0.19053388 -1.28463953 0.19053388] # [ 1.10101803 -0.18598974 1.10101803 -0.18598974 1.10101803 -0.18598974] # [-1.28463953 0.19053388 -1.28463953 0.19053388 -1.28463953 0.19053388] # [ 1.10101803 -0.18598974 1.10101803 -0.18598974 1.10101803 -0.18598974]]
相关文章推荐
- Python:一篇文章掌握Numpy的基本用法
- python-numpy的基本用法01
- python-pandas的基本用法03
- python-numpy的基本用法02
- python numpy基础(一)基本用法
- Python中矩阵库Numpy基本操作
- Python在信息学竞赛中的运用及Python的基本用法(详解)
- python中计时工具timeit模块的基本用法
- python-pandas的基本用法09
- Python numpy模块中transpose函数以及swapaxes函数用法
- Mysql基本用法-存储引擎-03
- Python函数基本用法
- python中map的基本用法示例
- python:3:列表基本用法及相关函数(1)
- python和numpy的基本操作速查
- Python爬虫(2):Requests的基本用法
- 【Python3】03、基本语法
- Python:numpy中dot,outer,*用法
- Python中numpy的基本统计学
- numpy的基本用法(一)——基本运算