numpy -- numpy高阶应用

numpy高阶应用

随机数

类型 说明
seed 确定随机数生成器的种子
permutation 返回一个序列的随机排列或返回一个随机排列的返回
shuffle 对一个序列就地随机乱序
rand 产生均匀分布的样本值
randint 从给定的上下限范围内随机选取整数
randn 产生正态分布(平均值为0,标准差为1)
binomial 产生二项分布的样本值
normal 产生正态(高斯)分布的样本值
beta 产生Beta分布的样本值
chisquare 产生卡方分布的样本值
gamma 产Gamma分布的样本值
uniform 产生在[0, 1]中均匀分布的样本值

import numpy as np
arr = np.random.normal(size = 10)
print(arr)
[ 0.20551433 -0.47894623  0.1723548  -2.52035522  0.90416095 -0.0748014
  0.2370496  -1.08262676 -0.29973029  0.48172872]

数组重塑

将一维数组转化为二维数组

arr = np.arange(15)
print(arr.reshape(3,5))
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]

获取维度信息并应用

other_arr = np.arange(15).reshape(5,3)

print(other_arr)
print(arr.reshape(other_arr.shape))
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]]
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]]

数组拉平

arr = arr.reshape(other_arr.shape)
print(arr)
print(arr.ravel())
print(arr.ravel().reshape(other_arr.shape))
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]]

数组连接

类型 说明
concatenate 最一般化的连接,沿一条轴连接一组数组
vstack,row_stack 以面向行的方式对数组进行堆叠(沿轴0)
hstack, 以面向行的方式对数组进行堆叠(沿轴1)
column_stack 类似于hstack,但是会先将一维数组转换为二维列向量。
dstack 以面向"深度"的方式对数组进行堆叠(沿轴2)
split 沿指定轴在指定的位置拆分数组
hsplit,vsplit,dsplit split的便捷化函数,分别沿着轴0、轴1和轴2进行拆分。
arr1 = np.array([[1,2,3,4],[5,6,7,8]])
arr2 = np.array([[9,10,11,12],[13,14,15,16]])
print(np.concatenate([arr1,arr2],axis = 0))  #按行,向下连接
print(np.concatenate([arr1,arr2],axis = 1))  #按列,向右连接
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]
[[ 1  2  3  4  9 10 11 12]
 [ 5  6  7  8 13 14 15 16]]

数组拆分

arr = np.random.randn(5,5)
print(arr)

a,b,c = np.split(arr,[1,3],axis = 0)

print(a)
print(b)
print(c)

#help(np.split)
[[ 0.65808685 -1.87367158 -0.44571392  0.20153713  1.07337974]
 [ 0.56055164  0.81087505 -0.92973517 -0.54649625 -0.251764  ]
 [-1.05954976  0.89089206 -1.47122268 -0.88814653 -0.35668902]
 [ 1.16943732 -0.30638432 -1.09877266  0.33839846  1.43588489]
 [-0.94883322  0.81643331 -0.3468645  -0.63871218 -1.26688086]]
[[ 0.65808685 -1.87367158 -0.44571392  0.20153713  1.07337974]]
[[ 0.56055164  0.81087505 -0.92973517 -0.54649625 -0.251764  ]
 [-1.05954976  0.89089206 -1.47122268 -0.88814653 -0.35668902]]
[[ 1.16943732 -0.30638432 -1.09877266  0.33839846  1.43588489]
 [-0.94883322  0.81643331 -0.3468645  -0.63871218 -1.26688086]]

堆叠水平和垂直

print(arr)

arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[7, 8, 9], [10, 11, 12]])

print(np.vstack([arr1,arr2]))
print(np.hstack([arr1,arr2]))
[[ 0.65808685 -1.87367158 -0.44571392  0.20153713  1.07337974]
 [ 0.56055164  0.81087505 -0.92973517 -0.54649625 -0.251764  ]
 [-1.05954976  0.89089206 -1.47122268 -0.88814653 -0.35668902]
 [ 1.16943732 -0.30638432 -1.09877266  0.33839846  1.43588489]
 [-0.94883322  0.81643331 -0.3468645  -0.63871218 -1.26688086]]
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
[[ 1  2  3  7  8  9]
 [ 4  5  6 10 11 12]]

堆叠辅助类 r_ c_

print(np.r_[arr1,arr2])
print(np.c_[arr1,arr2])
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
[[ 1  2  3  7  8  9]
 [ 4  5  6 10 11 12]]

元素的重复操作

arr = np.random.randn(2,2)
print(arr.repeat(2,axis = 0))
print(arr.repeat(2,axis = 1))
[[-0.44388113  0.13987511]
 [-0.44388113  0.13987511]
 [ 1.42439852  2.53537756]
 [ 1.42439852  2.53537756]]
[[-0.44388113 -0.44388113  0.13987511  0.13987511]
 [ 1.42439852  1.42439852  2.53537756  2.53537756]]

tile

print(np.tile(arr,2))
print(np.tile(arr,[2,1]))
[[-0.44388113  0.13987511 -0.44388113  0.13987511]
 [ 1.42439852  2.53537756  1.42439852  2.53537756]]
[[-0.44388113  0.13987511]
 [ 1.42439852  2.53537756]
 [-0.44388113  0.13987511]
 [ 1.42439852  2.53537756]]

花式索引

arr = np.arange(10) * 100
inds = [7, 1, 2, 6]

print(arr[inds])
[700 100 200 600]

等效的索引

print(arr.take(inds))
arr.put(inds,[0,0,0,0])
print(arr)

arr = np.random.rand(3,4)
inds = [2, 1, 2, 2]

print(arr.take(inds,axis = 1))  #按列查找
[700 100 200 600]
[  0   0   0 300 400 500   0   0 800 900]
[[ 0.6876503   0.08996047  0.6876503   0.6876503 ]
 [ 0.60100624  0.18482635  0.60100624  0.60100624]
 [ 0.77159319  0.04447536  0.77159319  0.77159319]]

你可能感兴趣的:(numpy -- numpy高阶应用)