NumPy  einsum nditer bincount unpackbit np.lib.stride_tricks.as_strided

NumPy 迭代数组 nditer

 https://www.runoob.com/numpy/numpy-terating-over-array.html

 

numpy.bincount详解

 https://blog.csdn.net/xlinsist/article/details/51346523

numpy.bincount详解

 大致说bin的数量比x中的最大值大1,每个bin给出了它的索引值在x中出现的次数。

# 我们可以看到x中最大的数为7,因此bin的数量为8,那么它的索引值为0->7
x = np.array([0, 1, 1, 3, 2, 1, 7])
# 索引0出现了1次,索引1出现了3次......索引5出现了0次......
np.bincount(x)
#因此,输出结果为:array([1, 3, 1, 1, 0, 0, 0, 1])

# 我们可以看到x中最大的数为7,因此bin的数量为8,那么它的索引值为0->7
x = np.array([7, 6, 2, 1, 4])
# 索引0出现了0次,索引1出现了1次......索引5出现了0次......
np.bincount(x)
#输出结果为:array([0, 1, 1, 0, 1, 0, 1, 1])

weights这个参数。文档说,如果weights参数被指定,那么x会被它加权,也就是说,如果值n发现在位置i,那么out[n] += weight[i]而不是out[n] += 1.**因此,我们weights的大小必须与x相同,否则报错。

w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6])
# 我们可以看到x中最大的数为4,因此bin的数量为5,那么它的索引值为0->4
x = np.array([2, 1, 3, 4, 4, 3])
# 索引0 -> 0
# 索引1 -> w[1] = 0.5
# 索引2 -> w[0] = 0.3
# 索引3 -> w[2] + w[5] = 0.2 - 0.6 = -0.4
# 索引4 -> w[3] + w[4] = 0.7 + 1 = 1.7
np.bincount(x,  weights=w)
# 因此,输出结果为:array([ 0. ,  0.5,  0.3, -0.4,  1.7])

最后,我们来看一下minlength这个参数。文档说,如果minlength被指定,那么输出数组中bin的数量至少为它指定的数(如果必要的话,bin的数量会更大,这取决于x)

# 我们可以看到x中最大的数为3,因此bin的数量为4,那么它的索引值为0->3
x = np.array([3, 2, 1, 3, 1])
# 本来bin的数量为4,现在我们指定了参数为7,因此现在bin的数量为7,所以现在它的索引值为0->6
np.bincount(x, minlength=7)
# 因此,输出结果为:array([0, 2, 1, 2, 0, 0, 0])

# 我们可以看到x中最大的数为3,因此bin的数量为4,那么它的索引值为0->3
x = np.array([3, 2, 1, 3, 1])
# 本来bin的数量为4,现在我们指定了参数为1,那么它指定的数量小于原本的数量,因此这个参数失去了作用,索引值还是0->3
np.bincount(x, minlength=1)
# 因此,输出结果为:array([0, 2, 1, 2])

 

np.add.at 

函数含义举例
np.add.accumulate()累加
(每一个位置的元素和前面的所有元素加起来求和)
>>> np.add.accumulate([1,2,3])
array([1, 3, 6], dtype=int32)
np.add.reduce()连加
(将所有元素加在一起求和)
>>> np.add.reduce([1,2,3,4,5])
15
np.add.at()将传入的数组中制定下标位置的元素加上指定的值>>> x=np.array([1,2,3,4])
#将x中下标为0和为2的元素加上了3
>>> np.add.at(x,[0,2],3)
>>> x
array([4, 2, 6, 4])
------------------------------------
>>> a = np.array([1, 2, 3, 4])
#在a[0]的位置上加1,在a[1]的位置上加2
>>> b = np.array([1, 2])
>>> np.add.at(a, [0, 1], b)
>>> print(a)
array([2, 4, 3, 4])
 
np.add.outer()将第一个列表或数组中的每个元素依次加到
第二个列表或数组中的每个元素,得到每一行
>>> np.add.outer([1,2,3],[4,5,6,7])
array([[ 5, 6, 7, 8],[ 6, 7, 8, 9], [ 7, 8, 9, 10]])
np.add.reduceat(x,list)在各切片上作reduce运算,
根据传入的list(第二个参数)作指定的变化,传入的list中的数字是成对出现的

>>> x=np.arange(8)
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> np.add.reduceat(x,[0,4,1,5,2,6,3,7]) #在各切片上作reduce运算
array([ 6, 4, 10, 5, 14, 6, 18, 7], dtype=int32)

’’’
x中0,4部分切片作np.add.reduce()运算(也就是连加),放在第一个位置,然后第二个位置就是下标4在x中的值,也就是4,第三个位置是 x中1,5部分切片作np.add.reduce()运算
’’’

 

获得点积 dot prodcut的对角线?

(提示: np.diag)

In [ ]:

# A = np.random.uniform(0,1,(5,5))

# B = np.random.uniform(0,1,(5,5))

# # slow version

# np.diag(np.dot(A, B))

In [ ]:

## 方法2

# # Fast version

# np.sum(A * B.T, axis=1)

In [ ]:

## 方法3

# # Faster version

# np.einsum("ij,ji->i", A, B)

 

NumPy  einsum

https://cloud.tencent.com/developer/article/1369762

 

numpy.roll()

numpy.roll(ashiftaxis=None)

函数解释:沿着给定轴滚动数组元素。超出最后位置的元素将会滚动到第一个位置。

(将a,沿着axis的方向,滚动shift长度)

参数:

a : (array_like)
输入数组
shift : (int or tuple of ints)
滚动的长度。如果是提供元组的话,下面的轴参数的维度也应该跟shift维度一样。
axis : (int or tuple of ints, optional)
滚动的维度,0为垂直滚动,1为水平滚动(默认情况下,即参数为None时,数组在移位之前会被变成扁平化,之后会恢复原始形状。)
返回值:    

res : 输出数组(维度和a一样)

>> x = np.arange(10)  # x例子
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

>> np.roll(x, 2)  # axis为None,则会先进行扁平化,然后再向水平滚动2个位置
array([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])

>> x2 = np.reshape(x, (2,5))  # x2例子
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

>> np.roll(x2, 1)  # axis为None,则会先进行扁平化,然后再向水平滚动1个位置
array([[9, 0, 1, 2, 3],
       [4, 5, 6, 7, 8]])

>> np.roll(x2, 1, axis=0)  # 5列同时向垂直方向滚动1个位置
array([[5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4]])

>> np.roll(x2, 1, axis=1)  # 2行同时向水平方向滚动1个位置
array([[4, 0, 1, 2, 3],
       [9, 5, 6, 7, 8]])

ndarray.view()

ndarray.view() 方会创建一个新的数组对象,该方法创建的新数组的维数更改不会更改原始数据的维数。

import numpy as np 
 
# 最开始 a 是个 3X2 的数组
a = np.arange(6).reshape(3,2)  
print ('数组 a:')
print (a)
print ('创建 a 的视图:')
b = a.view()  
print (b)
print ('两个数组的 id() 不同:')
print ('a 的 id():')
print (id(a))
print ('b 的 id():' )
print (id(b))
# 修改 b 的形状,并不会修改 a
b.shape =  2,3
print ('b 的形状:')
print (b)
print ('a 的形状:')
print (a)
数组 a:
[[0 1]
 [2 3]
 [4 5]]
创建 a 的视图:
[[0 1]
 [2 3]
 [4 5]]
两个数组的 id() 不同:
a 的 id():
4314786992
b 的 id():
4315171296
b 的形状:
[[0 1 2]
 [3 4 5]]
a 的形状:
[[0 1]
 [2 3]
 [4 5]]

简单的赋值不会创建数组对象的副本。 相反,它使用原始数组的相同id()来访问它。 id()返回 Python 对象的通用标识符,类似于 C 中的指针。

此外,一个数组的任何变化都反映在另一个数组上。 例如,一个数组的形状改变也会改变另一个数组的形状。

使用切片创建视图修改数据会影响到原始数组

副本或深拷贝

ndarray.copy() 函数创建一个副本。 对副本数据进行修改,不会影响到原始数据,它们物理内存不在同一位置。

 

repeat

(1)numpy.repeat(a,repeats,axis=None); 

(2)object(ndarray).repeat(repeats,axis=None):

参数的意义:axis=None,时候就会flatten当前矩阵,实际上就是变成了一个行向量

                      axis=0,沿着y轴复制,实际上增加了行数

                      axis=1,沿着x轴复制,实际上增加列数

                      repeats可以为一个数,也可以为一个矩阵,具体区别我们从以下实例中就会发现
 

>>> c=np.array(([1,2],[3,4]))
>>> print(c)
[[1 2]
 [3 4]]
>>> np.repeat(c,2)
array([1, 1, 2, 2, 3, 3, 4, 4])

axis=0情况下可以看出讲数据安装在磁盘存储格式(先行后列)逐个元素复制repeats次,形成一个行向量

>>> np.repeat(c,2,axis=1)
array([[1, 1, 2, 2],
       [3, 3, 4, 4]])
>>> np.repeat(c,2,axis=0)
array([[1, 2],
       [1, 2],
       [3, 4],
       [3, 4]])
>>> np.repeat(c,[2,3],axis=0)
array([[1, 2],
       [1, 2],
       [3, 4],
       [3, 4],
       [3, 4]])

 

np.lib.stride_tricks.as_strided 详解

https://blog.csdn.net/weixin_46559271/article/details/105188300

Numpy之itemsize

itemsize输出array元素的字节数

 

np.add

函数含义举例
np.add.accumulate()累加
(每一个位置的元素和前面的所有元素加起来求和)
>>> np.add.accumulate([1,2,3])
array([1, 3, 6], dtype=int32)
np.add.reduce()连加
(将所有元素加在一起求和)
>>> np.add.reduce([1,2,3,4,5])
15
np.add.at()将传入的数组中制定下标位置的元素加上指定的值>>> x=np.array([1,2,3,4])
#将x中下标为0和为2的元素加上了3
>>> np.add.at(x,[0,2],3)
>>> x
array([4, 2, 6, 4])
------------------------------------
>>> a = np.array([1, 2, 3, 4])
#在a[0]的位置上加1,在a[1]的位置上加2
>>> b = np.array([1, 2])
>>> np.add.at(a, [0, 1], b)
>>> print(a)
array([2, 4, 3, 4])
 
np.add.outer()将第一个列表或数组中的每个元素依次加到
第二个列表或数组中的每个元素,得到每一行
>>> np.add.outer([1,2,3],[4,5,6,7])
array([[ 5, 6, 7, 8],[ 6, 7, 8, 9], [ 7, 8, 9, 10]])
np.add.reduceat(x,list)在各切片上作reduce运算,
根据传入的list(第二个参数)作指定的变化,传入的list中的数字是成对出现的

>>> x=np.arange(8)
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> np.add.reduceat(x,[0,4,1,5,2,6,3,7]) #在各切片上作reduce运算
array([ 6, 4, 10, 5, 14, 6, 18, 7], dtype=int32)

’’’
x中0,4部分切片作np.add.reduce()运算(也就是连加),放在第一个位置,然后第二个位置就是下标4在x中的值,也就是4,第三个位置是 x中1,5部分切片作np.add.reduce()运算
’’’
 

 

unpackbits函数可以把整数转化成2进制数。

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值