numpy_数组的基本运算

数组的基本运算

数组的形状和类型修改

  1. np.reshape(a,newshape,order='C'):原数组size不变的前提下,改变原数组的形状
  2. np.resize(a,new_shape):改变原数组的形状和大小,与reshape不同的是可以改变数组的size。如果新数组大于原始数组,则新数组将填充a的重复副本。
  3. .T:将原shape为(n,m)的数组转置为(m,n),把数组的行和列进行互换,一维数组转置不变。
  4. ndarray.astype(type):强制转换为指定的类型。
 
#1、reshape
    import numpy as np
    arr = np.random.randint(3,15,size=(3,2))
    print(arr)
    print('*'*30)
    arr_1 = np.reshape(arr,(2,3))
    print(arr_1)
 
[[ 6  6]
 [ 7 12]
 [ 8  7]]
******************************
[[ 6  6  7]
 [12  8  7]]
 
#2、resize
    import numpy as np
    arr = np.random.randint(3,15,size=(3,2))
    print(arr)
    print('*'*30)
    arr_1 = np.resize(arr,(3,3))
    print(arr_1)
 
[[ 4  8]
 [14 10]
 [ 5 14]]
******************************
[[ 4  8 14]
 [10  5 14]
 [ 4  8 14]]
 
#3、转置
    import numpy as np
    arr = np.random.randint(3,15,size=(3,2))
    print(arr)
    print('*'*30)
    arr_1 = arr.T
    print(arr_1)
 
[[13  6]
 [ 8  7]
 [10  7]]
******************************
[[13  8 10]
 [ 6  7  7]]
 
#4、astype强制转换为制定的类型
    import numpy as np
    arr = np.random.randint(3,15,size=(3,2))
    print(arr.dtype)
    print('*'*30)
    arr_1 = arr.astype('float16')
    print(arr_1.dtype)
    #如果将浮点数转为整数,那么小数部分会被截断
    n2 = np.random.uniform(1,5,size=(3,4))
    print(n2)
    print('*'*30)
    print(n2.astype(np.int64))
 
int32
******************************
float16
[[2.13976361 4.81762022 4.10068156 2.34154714]
 [2.86796845 4.5901374  1.55180308 2.23347534]
 [3.63034111 4.92578335 4.87870914 3.25134124]]
******************************
[[2 4 4 2]
 [2 4 1 2]
 [3 4 4 3]]
 

Numpy数组合并,分割,元素重复

  1. np.concatenate 合并 #默认axis=0
  2. np.vstack 合并 #沿着轴0合并数组
  3. np.hstack 合并 #沿着轴1合并数组
  4. np.split 分割
  5. np.array_split:和split的区别是,可以不均等分割
  6. np.hsplit 分割 沿着轴1
  7. np.vsplit 分割 沿着轴0
  8. repeat:按照给定的次数,对数组的元素进行复制
 
#`合并` np.concatenate,np.vstack,np.hstack
    import numpy as np
    ar1 = np.arange(1,7).reshape(2,3)
    print(ar1,end='\n**************\n')
    ar2 = np.arange(8,14).reshape(2,3)
    print(ar2,end='\n**************\n')
    print(np.concatenate((ar1,ar2)),end='\n**************\n')#默认axis=0
    print(np.concatenate((ar1,ar2),axis = 1),end='\n**************\n')#指定axis=1
    print(np.vstack((ar1,ar2)),end='\n**************\n')
    print(np.hstack((ar1,ar2)),end='\n**************\n')
 
[[1 2 3]
 [4 5 6]]
**************
[[ 8  9 10]
 [11 12 13]]
**************
[[ 1  2  3]
 [ 4  5  6]
 [ 8  9 10]
 [11 12 13]]
**************
[[ 1  2  3  8  9 10]
 [ 4  5  6 11 12 13]]
**************
[[ 1  2  3]
 [ 4  5  6]
 [ 8  9 10]
 [11 12 13]]
**************
[[ 1  2  3  8  9 10]
 [ 4  5  6 11 12 13]]
**************
 
#分割,split,hsplit,vsplit,array_split
    import numpy as np
    arry = np.arange(1,10).reshape(3,3)
    print(arry,end='\n**************\n')
    ar1 = np.split(arry,[1,3])
    print(ar1,end='\n**************\n')
    print(np.split(arry,3),end='\n**************\n')#5表示把数组分割成5组,只能是均等分割
    print(np.array_split(arry,2),end='\n**************\n')#和split的区别是,可以不均等分割
    print(np.hsplit(arry,[2,1]),end='\n**************\n')
    print(np.vsplit(arry,[1,2]),end='\n**************\n')
 
[[1 2 3]
 [4 5 6]
 [7 8 9]]
**************
[array([[1, 2, 3]]), array([[4, 5, 6],
       [7, 8, 9]]), array([], shape=(0, 3), dtype=int32)]
**************
[array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])]
**************
[array([[1, 2, 3],
       [4, 5, 6]]), array([[7, 8, 9]])]
**************
[array([[1, 2],
       [4, 5],
       [7, 8]]), array([], shape=(3, 0), dtype=int32), array([[2, 3],
       [5, 6],
       [8, 9]])]
**************
[array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])]
**************
 

广播机制

广播:广播描述了算法如何在不同形状的数组之间的运算。

1. 操作两个数组,先比较数组的shape:`a.维度相等,b.相对应的轴长度为1`
2. 所有输入数组都向其中shape最长的数组看齐,shape中不足的部分都通过在前面加1补齐
3. 输出数组的shape是输入数组shape的各个轴上的最大值
 

Numpy排序和唯一值

1. sort():排序,和python列表类型类似,Numpy数组可以使用sort方法排序

 
    import numpy as np
    a = np.random.randint(30,size=(3,3))
    print(a,end='\n***********\n')
    a.sort(axis=0)
    print(a)
 
[[ 9  7 18]
 [11 27 11]
 [20 11  8]]
***********
[[ 9  7  8]
 [11 11 11]
 [20 27 18]]
 

2. np.unique:返回的是数组中唯一值排序后的数组

 
    #去重,只取出唯一值,按照一维数组返回结果。
    import numpy as np
    a = np.random.randint(30,size=(3,3))
    print(a)
    print(np.unique(a))
 
[[16  7 29]
 [ 4 12 28]
 [10  9  6]]
[ 4  6  7  9 10 12 16 28 29]
 

3. np.in1d:检查一个数组中的值是否在另外一个数组中,并返回一个布尔数组.

 
    import numpy as np
    a = np.random.randint(30,size=(2,2))
    b = np.random.randint(30,size=(2,2))
    print(a,b,end='\n***********\n')
    print(np.in1d(a,b))
 
[[17 21]
 [ 4 14]]
***********
[[ 6 14]
 [ 6  5]]
[False False False  True]
 

Numpy数学统计运算

基础数组统计方法(axis=0 按列,axis=1,按行)

方法 描述
sum 总和
mean 平均数
std 标准差
var 方差
min 最小值
max 最大值
argmin 最小值的位置
argmax 最大值的位置
 
    import numpy as np
    arr2 = np.random.randint(1,8,size=(4,3))
    print(arr2,end='\n***********\n')
    print('求和(按列):',arr2.sum(axis=0)) #axis=0 按列求和,axis=1,按行求和
    print('求和(按行):',arr2.sum(axis=1))
    print('求平均(按列):',arr2.mean(axis=0))
    print('求平均(按行):',arr2.mean(axis=1))
    print('求标准差(按列):',arr2.std(axis=0))
    print('求标准差(按列):',arr2.std(axis=1))
    print('求最小值(按行):',arr2.min(axis=0))
    print('求最小值(按列):',arr2.min(axis=1))
    print('求最大值(按行):',arr2.max(axis=0))
    print('求最大值(按列):',arr2.max(axis=1))

 
[[4 6 7]
 [4 2 2]
 [1 3 6]
 [2 1 7]]
***********
求和(按列): [11 12 22]
求和(按行): [17  8 10 10]
求平均(按列): [2.75 3.   5.5 ]
求平均(按行): [5.66666667 2.66666667 3.33333333 3.33333333]
求标准差(按列): [1.29903811 1.87082869 2.06155281]
求标准差(按列): [1.24721913 0.94280904 2.05480467 2.62466929]
求最小值(按行): [1 1 2]
求最小值(按列): [4 2 1 1]
求最大值(按行): [4 6 7]
求最大值(按列): [7 4 6 7]
 

Numpy逻辑操作和np.where

1. 逻辑运算

1. 比较运算符 >, >=, <, <=, ==, != ,比较运算符,返回的是一个布尔数组。

 
    import numpy as np
    a = np.random.rand(3,3)
    b = np.modf(a*30)[1]
    print(b,end='\n***********\n') # 生成一个三行三列0-1的随机数*30,modf取出整数和小数部分,留整数部分
    print(b<20)
 
[[17. 28.  4.]
 [ 5. 27. 20.]
 [28. 22. 13.]]
***********
[[ True False  True]
 [ True False False]
 [False False  True]]
 

2.1 逻辑运算符 与:&, 或:|, 非:~

2.2 二元通用函数与:logical_and, 或:logical_or, 非:lodical_not

 
    import numpy as np
    a = np.arange(1,10).reshape(3,3)
    b = np.random.randint(5,10,size=(3,3))
    print(a,b,end='\n***********\n',sep='\n***********\n')
    print((a>5)&(b>5))
 
[[1 2 3]
 [4 5 6]
 [7 8 9]]
***********
[[5 8 5]
 [5 6 7]
 [9 7 7]]
***********
[[False False False]
 [False False  True]
 [ True  True  True]]
 

2. np.where(condition, x, y):

是三元表达式 x if condition else y的向量化。如果是True,输出x,相反,False,输出y。传递给np.where的数组可以是同等大小的数组,也可以是标量。
np.where(arr1>0) #只有条件 (condition),没有x和y,则输出满足条件 (即非0) 元素的坐标。这里的坐标以tuple的形式给出,通常原数组有多少维,输出的tuple中就包含几个数组,分别对应符合条件元素的各维坐标。
 
    import numpy as np
    a = np.arange(1,10).reshape(3,3)
    print(a,end='\n***********\n',sep='\n***********\n')
    print(np.where(a>5,11,0),end='\n***********\n')
    print(np.where(a>5))
 
[[1 2 3]
 [4 5 6]
 [7 8 9]]
***********
[[ 0  0  0]
 [ 0  0 11]
 [11 11 11]]
***********
(array([1, 2, 2, 2], dtype=int64), array([2, 0, 1, 2], dtype=int64))
 

3. any(),all()方法:

函数名称 描述
np.any 验证任何一个元素是否为真
np.all 验证所有元素是否为真

这两个方法可以快速检查布尔数组,any():检查数组中是否至少有一个True, all():检查是否每个值都为True.

 
    import numpy as np
    a = np.arange(0,9).reshape(3,3)
    print(a,end='\n***********\n',sep='\n***********\n')
    #比如想看下数组中是不是所有元素都为0,那么可以通过以下代码来实现:
    print(np.all(a==0))
    #比如我们想要看数组中是否有等于0的数,那么可以通过以下代码来实现:
    print(np.any(a==0))
 
[[0 1 2]
 [3 4 5]
 [6 7 8]]
***********
False
True
志同道合一起学习,欢迎加入QQ群:878749917
原文地址:https://www.cnblogs.com/gujianjian/p/15321443.html