NumPy 总结
flyfish
按条件筛选数据
import numpy as np
a=np.arange(16).reshape(4,4)
print(a)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]
# [12 13 14 15]]
b= a[np.where(a[:,0]>=3)] #输出第0列大于等于3的数据
print(b)
# [[ 4 5 6 7]
# [ 8 9 10 11]
# [12 13 14 15]]
满足条件元素的索引
import numpy as np
x = np.arange(6).reshape(2, 3)
print(x)
# [[0 1 2]
# [3 4 5]]
print(np.argwhere(x >= 1))
# [[0 1]
# [0 2]
# [1 0]
# [1 1]
# [1 2]]
求每一行或者每一列中的最大值
import numpy as np
x = np.arange(12)
x= x.reshape((3, 4))
print(x)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print(np.max(x, axis=0))
# [ 8 9 10 11]
print(np.max(x, axis=1))
# [ 3 7 11]
#np.min,np.sum一样
添加一行
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = np.array([100, 200, 300])
c = np.vstack((a, b))
print(c)
# [[ 1 2 3]
# [ 4 5 6]
# [100 200 300]]
按行合并或者按列合并
import numpy as np
a=np.arange(8).reshape(2,-1)
print(a)
# [[0 1 2 3]
# [4 5 6 7]]
b=np.arange(8,16).reshape(2,-1)
print(b)
# [[ 8 9 10 11]
# [12 13 14 15]]
test1=np.row_stack((a,b))#行合并
test2=np.column_stack((a,b))#列合并
print(test1)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]
# [12 13 14 15]]
print(test2)
# [[ 0 1 2 3 8 9 10 11]
# [ 4 5 6 7 12 13 14 15]]
numpy中的array与pytorch中的tensor互转
import numpy as np
import torch
a= np.array([1 ,2 ,3 ,4])
#array转tensor
b=torch.from_numpy(a)
print(b)
#tensor转array
c=b.numpy()
print(c)
查找数组的唯一元素。相当于去重。
np.unique
# 返回数组的排序后的唯一元素。
import numpy as np
A = [12,1,3,5,7,9,11,11,9,11]
print(np.unique(A))
# [ 1 3 5 7 9 11 12]
# 结果去重并排序
print(np.unique(A, return_index=True))
# (array([ 1, 3, 5, 7, 9, 11, 12]), array([1, 2, 3, 4, 5, 6, 0]))
# 结果去重并排序,并输出在源数组中的索引
print(np.unique(A, return_index=True, return_inverse=True))
# (array([ 1, 3, 5, 7, 9, 11, 12]), array([1, 2, 3, 4, 5, 6, 0]), array([6, 0, 1, 2, 3, 4, 5, 5, 4]))
print(np.unique(A, return_index=True, return_inverse=True,return_counts=True))
#(array([ 1, 3, 5, 7, 9, 11, 12]), array([1, 2, 3, 4, 5, 6, 0]), array([6, 0, 1, 2, 3, 4, 5, 5, 4, 5]), array([1, 1, 1, 1, 2, 3, 1]))
searchsorted的一种使用示例
一个数组的元素,在另一个数组中的索引
import numpy as np
a = np.array([1.11, 3.33,3.33, 2.22, 4.44,4.44])
b= np.unique(a)#将数组a的元素去重
#[1.11 2.22 3.33 4.44]
c = np.sort(b)
print(c)
print(np.searchsorted(c, a))#数组a的元素,在数组c中的索引
另一种用法
返回将在其中插入指定值以维持搜索顺序的索引。
import numpy as np
arr= np.array([5, 6, 7, 8, 9])
#查找应在其arr中插入10的索引
x1 = np.searchsorted(arr, 10)
print(x1)#5
#查找应在其arr中插入4的索引
x2 = np.searchsorted(arr, 4)
print(x2)#0
简单运算
import numpy as np
a = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])
b = np.array([2, 2, 2])
print(a+b)
#[[ 3 4 5]
# [ 6 7 8]
# [ 9 10 11]]
print(a-b)
#[[-1 0 1]
# [ 2 3 4]
# [ 5 6 7]]
print(a*b)
#[[ 2 4 6]
# [ 8 10 12]
# [14 16 18]]
squeeze的例子
Remove single-dimensional entries from the shape of an array.
从数组的shape中删除维度是1的条目。
x = np.array([[[0], [1], [2]]])
print(x)
#[[[0]
# [1]
# [2]]]
print(x.shape)#(1, 3, 1)
print(np.squeeze(x, axis=0).shape)#(3, 1)
print(np.squeeze(x, axis=2).shape)#(1, 3)
print(np.squeeze(x).shape)#(3,)
随机数的例子
import numpy as np
import matplotlib.pyplot as plt
#不设置seed,每次会生成不同的随机数
print(np.random.rand(3,2))
'''
[[0.63611338 0.81176128]
[0.64378796 0.81751788]
[0.11384994 0.79912718]]
'''
print(np.random.rand(3,2))
'''
[[0.31166034 0.59363663]
[0.45223468 0.01044228]
[0.46475564 0.74117943]]
'''
#相同的seed相同的输出
np.random.seed(0)
print(np.random.rand(5))
#[0.5488135 0.71518937 0.60276338 0.54488318 0.4236548 ]
np.random.seed(0)
print(np.random.rand(5))
#[0.5488135 0.71518937 0.60276338 0.54488318 0.4236548 ]
#正态分布 histogram
x=np.random.randn(500)
plt.hist(x,500)
plt.show()
print(np.random.randn(4,2))
Python numpy.linalg.norm(矩阵范数Matrix norm)
https://blog.csdn.net/flyfish1986/article/details/79596389
np.where的使用示例
import numpy as np
np.random.seed(0)#不设置seed,每次会生成不同的随机数
al=np.random.rand(3,2)
print(al)
#[[0.5488135 0.71518937]
# [0.60276338 0.54488318]
# [0.4236548 0.64589411]]
keep_prob=0.8
dl =np.where( np.random.rand(al.shape[0],al.shape[1])<=keep_prob,1,0)
#where算是三目运算符,where(条件,真,假)
print(dl)
#[[1 0]
# [0 1]
# [1 1]]
#第l层经过dropout,随机删减20%的神经元,只保留80%的神经元,其输出为:
al = np.multiply(al,dl)
print(al)
#[[0.5488135 0. ]
# [0. 0.54488318]
# [0.4236548 0.64589411]]
#最后,还要对al进行scale up处理,即:
al /= keep_prob
print(al)
#[[0.68601688 0. ]
# [0. 0.68110398]
# [0.5295685 0.80736764]]
[:,:]元素切片的例子
import numpy as np
s= np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(s[2,0])# 获取第二行第0个元素
print(s[:,0])# 获取每行第0个元素[1 4 7]
print(s[0:2,0])# [1 4]
print(s[0:2,0:2])
#[[1 2]
# [4 5]]
print(s[1:3,0:2])#行索引 从1到2,列索引从0到1
#[[4 5]
# [7 8]]
print(s[::2]) # ,以 2 为步长 取元素。
#[[1 2 3]
# [7 8 9]]
# 第一个冒号代表获取行的起止行号
# 第二个冒号代表获取列的起止行号
排序的例子
import numpy as np
#np.random.permutation与np.random.shuffle不同之处
#如果传给permutation一个矩阵,它会返回一个洗牌后的矩阵副本;
# 而shuffle只是对一个矩阵进行洗牌,无返回值。
#如果传入一个整数,它会返回一个洗牌后的arange。
arr1 = np.array([1, 4, 9, 12])
print(np.random.shuffle(arr1))#None
print(arr1)#[12 4 9 1]
arr2 = np.array([1, 4, 9, 12])
print(np.random.permutation(arr2))#[ 1 9 12 4]
print(arr2)#[ 1 4 9 12]
从一维或者多维数组中随机选择数据
import numpy as np
np.random.seed(1);
a=np.random.random((13,4))
b=np.random.random((13,1))
print(a)
print(a.shape)
print(b)
print(a.shape)
rand_index = np.random.choice(13,size=(3))
print("rand_index:\n")
print(rand_index)
a1 = a[rand_index]
b1 = b[rand_index]
print("从a中随机选择数据\n")
print(a1)
print(a1.shape)
print("从b中随机选择数据\n")
print(b1)
print(b1.shape)
将一个全部为int的列表,转化为全部为str的列表
list(map(str,[1,2,3,4,5]))
numpy中的三个点
import numpy
a = numpy.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]])
print(a)
# [[ 1 2 3 4 5]
# [ 6 7 8 9 10]
# [11 12 13 14 15]
# [16 17 18 19 20]]
print(a[...,2]) # ...表示遍历行,2表示取索引为2的列数据。
# [ 3 8 13 18]
print(a[...,:2])# ...表示遍历行,:2表示取索引为<2(0,1)的列数据。
# [[ 1 2]
# [ 6 7]
# [11 12]
# [16 17]]
print(a[...,::2])#...表示遍历行,2表示步长,取索引为0,2,4的列数据。
# [[ 1 3 5]
# [ 6 8 10]
# [11 13 15]
# [16 18 20]]
print(a[...,1::2])#...表示遍历行,从索引1开始2表示步长,取索引为1,1+2的列数据。
# [[ 2 4]
# [ 7 9]
# [12 14]
# [17 19]]
print(a[...,1::3])#...表示遍历行,从索引1开始3表示步长,取索引为1,1+3的列数据。
# [[ 2 5]
# [ 7 10]
# [12 15]
# [17 20]]