文章目录
将值为1的索引以列表的形式取出
mislabeled_indices = np.asarray(np.where(a == 1))
令<=0的元素为0
# When z <= 0, you should set dz to 0 as well.
dZ[Z <= 0] = 0
打乱数组
#第一步:打乱顺序
permutation = list(np.random.permutation(m)) #它会返回一个长度为m的随机数组,且里面的数是0到m-1
shuffled_X = X[:,permutation] #将每一列的数据按permutation的顺序来重新排列。
shuffled_Y = Y[:,permutation].reshape((1,m))
合并列表和数组
列表合并
a = [[1,2,3],[4,5,6]]
b = [[1,1,1],[2,2,2]]
a+b
>>>[[1, 2, 3], [4, 5, 6], [1, 1, 1], [2, 2, 2]]
a.extend(b)
a
>>>[[1, 2, 3], [4, 5, 6], [1, 1, 1], [2, 2, 2]]
数组合并
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = np.array([[1,1,1],[2,2,2]])
纵向合并
c = np.vstack((a,b))
c
>>>array([[1, 2, 3],
[4, 5, 6],
[1, 1, 1],
[2, 2, 2]])
c = np.r_[a,b]
c
>>>array([[1, 2, 3],
[4, 5, 6],
[1, 1, 1],
[2, 2, 2]])
数组横向合并
d = np.hstack((a,b))
d
>>>array([[1, 2, 3, 1, 1, 1],
[4, 5, 6, 2, 2, 2]])
d = np.c_[a,b]
d
>>>array([[1, 2, 3, 1, 1, 1],
[4, 5, 6, 2, 2, 2]])
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
np.concatenate((a, b), axis=0)
>>>array([[1, 2],
[3, 4],
[5, 6]])
np.concatenate((a, b.T), axis=1)
>>>array([[1, 2, 5],
[3, 4, 6]])
判断有无相等的值
[[1,2],[1,2]]==[1,2] 列表会作为一个整体进行比较
>>>False
np.asarray([[1,2],[1,2]])==np.asarray([1,1])
>>>array([[ True, False],
[ True, False]])
np.where(np.asarray([[1,2],[1,2]])==np.asarray([1,1]))
>>>(array([0, 1]), array([0, 0]))
numpy.where() 用法详解
注意:索引得是tuple
创建多维随机数组
np.random.rand(2, 3)
>>>array([[0.1733986 , 0.98218523, 0.00832304],
[0.73289522, 0.39713299, 0.72641348]])
np.random.random((2,3))
>>>array([[0.27718037, 0.84159993, 0.40267099],
[0.82867263, 0.79519155, 0.18532267]])
np.random.randint(0,10,size=(2,3))
>>>array([[7, 4, 6],
[3, 9, 1]])
调整数组列元素次序
import torch as t
a = t.randn((1,5))
a
>>>tensor([[-0.5184, -0.4428, -1.1630, -1.3399, 0.6762]])
a[:, [0, 2, 1, 4, 3]]
>>>tensor([[-0.5184, -1.1630, -0.4428, 0.6762, -1.3399]])
扩展数组
import torch as t
a = t.randn((3,))
b = t.randn((3,3))
a.unsqueeze(0)
>>>tensor([[-1.0501, -1.9088, 1.8951]])
a.unsqueeze(1)
>>>tensor([[-1.0501],
[-1.9088],
[ 1.8951]])
a[None, :]
>>>tensor([[-1.0501, -1.9088, 1.8951]])
a[:, None]
>>>tensor([[-1.0501],
[-1.9088],
[ 1.8951]])
a.expand_as(b)
>>>tensor([[-1.0501, -1.9088, 1.8951],
[-1.0501, -1.9088, 1.8951],
[-1.0501, -1.9088, 1.8951]])
数组挑选元素(不保持原数组维度)
a = np.random.randint(0,10,size=[2,3])
a
>>>array([[1, 6, 9],
[1, 1, 7]])
mask = a > 1
mask
>>>array([[False, True, True],
[False, False, True]])
a[mask]
>>>array([6, 9, 7]) 维度已经变了
划分网格操作
p_n_x, p_n_y = torch.meshgrid(
torch.arange(-1, 2),
torch.arange(-2, 3)
)
print(torch.arange(-1, 2))
print("=="*20)
print(torch.arange(-2, 3))
>>>tensor([-1, 0, 1])
========================================
tensor([-2, -1, 0, 1, 2])
print(p_n_x)
print("=="*20)
print(p_n_y)
>>>tensor([[-1, -1, -1, -1, -1],
[ 0, 0, 0, 0, 0],
[ 1, 1, 1, 1, 1]])
========================================
tensor([[-2, -1, 0, 1, 2],
[-2, -1, 0, 1, 2],
[-2, -1, 0, 1, 2]])
去除重复元素
打乱数据集顺序
#第一步:打乱顺序
permutation = list(np.random.permutation(m)) #它会返回一个长度为m的随机数组,且里面的数是0到m-1
shuffled_X = X[:,permutation] #将每一列的数据按permutation的顺序来重新排列。
shuffled_Y = Y[:,permutation].reshape((1,m))