笔记:深度学习中常用的numpy函数

-import numpy as np-numpy.array

创造一个类别为ndarray的矩阵,ndarray为numpy.array的数据类型。

-在list中索引不连续的元素

import numpy as np
a = [1,2,3,4,5,6,7,8]
a_ndarray = np.array(a)
b = a_ndarray[[0,2,5]]

(以上代码为引用千行百行

-np.bincount

import numpy as np

#获取平铺后每个索引位置值在原始数列中出现的次数
counts = np.bincount(nums)

#返回众数 返回最大值在数列中的索引位置
np.argmax(counts)

即 [0,4,5,8,8] ——> bincount 返回 长度为8的列表

[1,0,0,0,1,1,0,0,2] 也就是把0-8平铺到列表里,然后对0-8的每个数字计数

(引用于jamiehmy

-numpy.append

np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])
array([1, 2, 3, ..., 7, 8, 9])

np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0)
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0)
Traceback (most recent call last):
    ...
ValueError: all the input arrays must have same number of dimensions, but
the array at index 0 has 2 dimension(s) and the array at index 1 has 1
dimension(s)

 -np.sum

np.sum([0.5, 1.5])
2.0
np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32)
1
np.sum([[0, 1], [0, 5]])
6
np.sum([[0, 1], [0, 5]], axis=0)
array([0, 6])
np.sum([[0, 1], [0, 5]], axis=1)
array([1, 5])
np.sum([[0, 1], [np.nan, 5]], where=[False, True], axis=1)
array([1., 5.]

-numpy.array_split

numpy.array_split(ary, indices_or_sections, axis=0)

x = np.arange(8.0)
np.array_split(x, 3)
[array([0.,  1.,  2.]), array([3.,  4.,  5.]), array([6.,  7.])]
x = np.arange(9)
np.array_split(x, 4)
[array([0, 1, 2]), array([3, 4]), array([5, 6]), array([7, 8])]

-numpy.argsort (有arg的输出基本都是indices)

One dimensional array:

x = np.array([3, 1, 2])
np.argsort(x)
array([1, 2, 0])
Two-dimensional array:

x = np.array([[0, 3], [2, 2]])
x
array([[0, 3],
       [2, 2]])
ind = np.argsort(x, axis=0)  # sorts along first axis (down)
ind
array([[0, 1],
       [1, 0]])
np.take_along_axis(x, ind, axis=0)  # same as np.sort(x, axis=0)
array([[0, 2],
       [2, 3]])
ind = np.argsort(x, axis=1)  # sorts along last axis (across)
ind
array([[0, 1],
       [0, 1]])
np.take_along_axis(x, ind, axis=1)  # same as np.sort(x, axis=1)
array([[0, 3],
       [2, 2]])

-numpy.argmax

a = np.arange(6).reshape(2,3) + 10
a
array([[10, 11, 12],
       [13, 14, 15]])
np.argmax(a)
5
np.argmax(a, axis=0)
array([1, 1, 1])
np.argmax(a, axis=1)
array([2, 2])

Indexes of the maximal elements of a N-dimensional array:

ind = np.unravel_index(np.argmax(a, axis=None), a.shape)
ind
(1, 2)
a[ind]
15
b = np.arange(6)
b[1] = 5
b
array([0, 5, 2, 3, 4, 5])
np.argmax(b)  # Only the first occurrence is returned.
1

-np.delete()

  • numpy.delete(arr, obj, axis=None)
    • arr: Input array
    • obj: Row or column number to delete
    • axis: Axis to delete
arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
np.delete(arr, 1, 0)
array([[ 1,  2,  3,  4],
       [ 9, 10, 11, 12]])
np.delete(arr, np.s_[::2], 1)
array([[ 2,  4],
       [ 6,  8],
       [10, 12]])
np.delete(arr, [1,3,5], None)
array([ 1,  3,  5,  7,  8,  9, 10, 11, 12])

numpy.hstack and vstack 一个加列一个加行

a = np.array((1,2,3))
b = np.array((4,5,6))
np.hstack((a,b))

array([1, 2, 3, 4, 5, 6])

a = np.array([[1],[2],[3]])
b = np.array([[4],[5],[6]])
np.hstack((a,b))

array([[1, 4],
       [2, 5],
       [3, 6]])

-numpy.maximum 和 numpy.amax

前者是对比两个矩阵,后者是一个矩阵本身

np.maximum([2, 3, 4], [1, 5, 2])
array([2, 5, 4])

np.maximum(np.eye(2), [0.5, 2]) # broadcasting
array([[ 1. ,  2. ],
       [ 0.5,  2. ]])

np.maximum([np.nan, 0, np.nan], [0, np.nan, np.nan])
array([nan, nan, nan])
np.maximum(np.Inf, 1)
inf

-numpy.random.choice

random.choice(asize=Nonereplace=Truep=None)

Whether the sample is with or without replacement. Default is True, meaning that a value of a can be selected multiple times.

Generate a uniform random sample from np.arange(5) of size 3:
np.random.choice(5, 3)
array([0, 3, 4]) # random
#This is equivalent to np.random.randint(0,5,3)

Generate a non-uniform random sample from np.arange(5) of size 3:
np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0]) # random

Generate a uniform random sample from np.arange(5) of size 3 without replacement:
np.random.choice(5, 3, replace=False)
array([3,1,0]) # random
#This is equivalent to np.random.permutation(np.arange(5))[:3]

Generate a non-uniform random sample from np.arange(5) of size 3 without replacement:
np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
array([2, 3, 0]) # random

-numpy.random.rand

0到1的均匀分布随机矩阵

np.random.rand(3,2)
array([[ 0.14022471,  0.96360618],  #random
       [ 0.37601032,  0.25528411],  #random
       [ 0.49313049,  0.94909878]]) #random

You can do this with any tuple. The * unpacks a tuple into multiple input arguments. The code is creating a random matrix the same shape as H1 using the shape attribute (which is a tuple) as the dimension inputs to np.random.rand. 不加星号就会报错

U1 = np.random.rand(*H1.shape) < p # first dropout mask
np.random.rand(*(2,3))     # The same as np.random.rand(2,3)
# Creates a 2 x 3 array

-numpy.random.normal

random.normal(loc=0.0scale=1.0size=None)

np.random.normal(3, 2.5, size=(2, 4))

创造一个平均值为3, 标准差为2.5,大家为2*4的正态(高斯)分布矩阵

-numpy.pad

numpy.pad(arraypad_widthmode='constant'**kwargs)

分别是pad的对象,宽度,pad的模式, pad的值(根据模式决定是否有)

a = [[1, 2], [3, 4]]
b = np.pad(a, 1, 'constant', constant_values=0)
print(b)

>>[[0 0 0 0]
 [0 1 2 0]
 [0 3 4 0]
 [0 0 0 0]]


a = [[[1, 2], [3, 4]],[[1, 2], [3, 4]]]
b = np.pad(a, ((0,),(1,),(1,)), 'constant', constant_values=0)
print(a)
print(b)

>>[[[1, 2], [3, 4]], [[1, 2], [3, 4]]]
[[[0 0 0 0]
  [0 1 2 0]
  [0 3 4 0]
  [0 0 0 0]]

 [[0 0 0 0]
  [0 1 2 0]
  [0 3 4 0]
  [0 0 0 0]]]

(以上引用于numpy — NumPy v1.21 Manual

-numpy.reshape

numpy.reshape(anewshapeorder='C')

 array.reshape(1,-1)中, -1 的意思是-1所在的维度长度不确定,让numpy根据对象数组和另一个维度的设置自行决定。

-numpy.moveaxis

numpy.moveaxis(asourcedestination)

x = np.zeros((3, 4, 5))
np.moveaxis(x, 0, -1).shape
(4, 5, 3)
np.moveaxis(x, -1, 0).shape
(5, 3, 4)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值